Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Reader

public abstract class Reader
extends Object implements Readable, Closeable

java.lang.Object
   ↳ java.io.Reader
Known Direct Subclasses
Known Indirect Subclasses


The base class for all readers. A reader is a means of reading data from a source in a character-wise manner. Some readers also support marking a position in the input and returning to this position later.

This abstract class does not provide a fully working implementation, so it needs to be subclassed, and at least the read(char[], int, int) and close() methods needs to be overridden. Overriding some of the non-abstract methods is also often advised, since it might result in higher efficiency.

Many specialized readers for purposes like reading from a file already exist in this package.

See also:

Summary

Fields

protected Object lock

The object used to synchronize access to the reader.

Protected constructors

Reader()

Constructs a new Reader with this as the object used to synchronize critical sections.

Reader(Object lock)

Constructs a new Reader with lock used to synchronize critical sections.

Public methods

abstract void close()

Closes this reader.

void mark(int readLimit)

Sets a mark position in this reader.

boolean markSupported()

Indicates whether this reader supports the mark() and reset() methods.

int read()

Reads a single character from this reader and returns it as an integer with the two higher-order bytes set to 0.

abstract int read(char[] buffer, int offset, int count)

Reads up to count characters from this reader and stores them at offset in the character array buffer.

int read(CharBuffer target)

Reads characters and puts them into the target character buffer.

int read(char[] buffer)

Reads characters from this reader and stores them in the character array buffer starting at offset 0.

boolean ready()

Indicates whether this reader is ready to be read without blocking.

void reset()

Resets this reader's position to the last mark() location.

long skip(long charCount)

Skips charCount characters in this reader.

Inherited methods

From class java.lang.Object
From interface java.lang.Readable
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Fields

lock

Added in API level 1
Object lock

The object used to synchronize access to the reader.

Protected constructors

Reader

Added in API level 1
Reader ()

Constructs a new Reader with this as the object used to synchronize critical sections.

Reader

Added in API level 1
Reader (Object lock)

Constructs a new Reader with lock used to synchronize critical sections.

Parameters
lock Object: the Object used to synchronize critical sections.
Throws
NullPointerException if lock is null.

Public methods

close

Added in API level 1
void close ()

Closes this reader. Implementations of this method should free any resources associated with the reader.

Throws
IOException if an error occurs while closing this reader.

mark

Added in API level 1
void mark (int readLimit)

Sets a mark position in this reader. The parameter readLimit indicates how many characters can be read before the mark is invalidated. Calling reset() will reposition the reader back to the marked position if readLimit has not been surpassed.

This default implementation simply throws an IOException; subclasses must provide their own implementation.

Parameters
readLimit int: the number of characters that can be read before the mark is invalidated.
Throws
IllegalArgumentException if readLimit < 0.
IOException if an error occurs while setting a mark in this reader.

See also:

markSupported

Added in API level 1
boolean markSupported ()

Indicates whether this reader supports the mark() and reset() methods. This default implementation returns false.

Returns
boolean always false.

read

Added in API level 1
int read ()

Reads a single character from this reader and returns it as an integer with the two higher-order bytes set to 0. Returns -1 if the end of the reader has been reached.

Returns
int the character read or -1 if the end of the reader has been reached.
Throws
IOException if this reader is closed or some other I/O error occurs.

read

Added in API level 1
int read (char[] buffer, 
                int offset, 
                int count)

Reads up to count characters from this reader and stores them at offset in the character array buffer. Returns the number of characters actually read or -1 if the end of the reader has been reached.

Parameters
buffer char
offset int
count int
Returns
int
Throws
IOException if this reader is closed or some other I/O error occurs.

read

Added in API level 1
int read (CharBuffer target)

Reads characters and puts them into the target character buffer.

Parameters
target CharBuffer: the destination character buffer.
Returns
int the number of characters put into target or -1 if the end of this reader has been reached before a character has been read.
Throws
IOException if any I/O error occurs while reading from this reader.
NullPointerException if target is null.
ReadOnlyBufferException if target is read-only.

read

Added in API level 1
int read (char[] buffer)

Reads characters from this reader and stores them in the character array buffer starting at offset 0. Returns the number of characters actually read or -1 if the end of the reader has been reached.

Parameters
buffer char
Returns
int
Throws
IOException if this reader is closed or some other I/O error occurs.

ready

Added in API level 1
boolean ready ()

Indicates whether this reader is ready to be read without blocking. Returns true if this reader will not block when read is called, false if unknown or blocking will occur. This default implementation always returns false.

Returns
boolean always false.
Throws
IOException if this reader is closed or some other I/O error occurs.

See also:

reset

Added in API level 1
void reset ()

Resets this reader's position to the last mark() location. Invocations of read() and skip() will occur from this new location. If this reader has not been marked, the behavior of reset() is implementation specific. This default implementation throws an IOException.

Throws
IOException always thrown in this default implementation.

See also:

skip

Added in API level 1
long skip (long charCount)

Skips charCount characters in this reader. Subsequent calls of read methods will not return these characters unless reset is used. This method may perform multiple reads to read charCount characters.

Parameters
charCount long
Returns
long the number of characters actually skipped.
Throws
IllegalArgumentException if charCount < 0.
IOException if this reader is closed or some other I/O error occurs.

See also:

This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.