public abstract class Reader extends Object implements Readable, Closeable
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.
Writer| Modifier and Type | Field and Description |
|---|---|
protected Object |
lock
The object used to synchronize access to the reader.
|
| Modifier | Constructor and Description |
|---|---|
protected |
Reader()
Constructs a new
Reader with this as the object used to
synchronize critical sections. |
protected |
Reader(Object lock)
Constructs a new
Reader with lock used to synchronize
critical sections. |
| Modifier and Type | Method and Description |
|---|---|
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. |
static Reader |
nullReader()
Returns a new
Reader that reads no characters. |
int |
read()
Reads a single character from this reader and returns it as an integer
with the two higher-order bytes set to 0.
|
int |
read(char[] buffer)
Reads characters from this reader and stores them in the character array
buffer starting at offset 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. |
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. |
long |
transferTo(Writer out)
Reads all characters from this reader and writes the characters to the
given writer in the order that they are read.
|
protected Object lock
protected Reader()
Reader with this as the object used to
synchronize critical sections.protected Reader(Object lock)
Reader with lock used to synchronize
critical sections.lock - the Object used to synchronize critical sections.NullPointerException - if lock is null.public static Reader nullReader()
Reader that reads no characters. The returned
stream is initially open. The stream is closed by calling the
close() method. Subsequent calls to close() have no
effect.
While the stream is open, the read(), read(char[]),
read(char[], int, int), read(Charbuffer), ready(), skip(long), and transferTo() methods all
behave as if end of stream has been reached. After the stream has been
closed, these methods all throw IOException.
The markSupported() method returns false. The
mark() and reset() methods throw an IOException.
The object used to synchronize operations on the
returned Reader is not specified.
Reader which reads no characterspublic abstract void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableIOException - if an error occurs while closing this reader.public void mark(int readLimit)
throws IOException
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.
readLimit - the number of characters that can be read before the mark is
invalidated.IllegalArgumentException - if readLimit < 0.IOException - if an error occurs while setting a mark in this reader.markSupported(),
reset()public boolean markSupported()
mark() and
reset() methods. This default implementation returns
false.false.public int read()
throws IOException
IOException - if this reader is closed or some other I/O error occurs.public int read(char[] buffer)
throws IOException
buffer starting at offset 0. Returns the number of characters
actually read or -1 if the end of the reader has been reached.IOException - if this reader is closed or some other I/O error occurs.public abstract int read(char[] buffer,
int offset,
int count)
throws IOException
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.IOException - if this reader is closed or some other I/O error occurs.public boolean ready()
throws IOException
true if this reader will not block when read is
called, false if unknown or blocking will occur. This default
implementation always returns false.false.IOException - if this reader is closed or some other I/O error occurs.read(),
read(char[]),
read(char[], int, int)public void reset()
throws IOException
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.IOException - always thrown in this default implementation.mark(int),
markSupported()public long skip(long charCount)
throws IOException
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.IllegalArgumentException - if charCount < 0.IOException - if this reader is closed or some other I/O error occurs.mark(int),
markSupported(),
reset()public int read(CharBuffer target) throws IOException
target character buffer.read in interface Readabletarget - the destination character buffer.target or -1 if the end
of this reader has been reached before a character has been read.IOException - if any I/O error occurs while reading from this reader.NullPointerException - if target is null.ReadOnlyBufferException - if target is read-only.public long transferTo(Writer out) throws IOException
This method may block indefinitely reading from the reader, or writing to the writer. The behavior for the case where the reader and/or writer is asynchronously closed, or the thread interrupted during the transfer, is highly reader and writer specific, and therefore not specified.
If an I/O error occurs reading from the reader or writing to the writer, then it may do so after some characters have been read or written. Consequently the reader may not be at end of the stream and one, or both, streams may be in an inconsistent state. It is strongly recommended that both streams be promptly closed if an I/O error occurs.
out - the writer, non-nullIOException - if an I/O error occurs when reading or writingNullPointerException - if out is null