Closeable
, AutoCloseable
, Readable
BufferedReader
, CharArrayReader
, FilterReader
, InputStreamReader
, PipedReader
, StringReader
public abstract class Reader extends Object implements Readable, Closeable
Modifier and Type | Field | Description |
---|---|---|
protected Object |
lock |
The object used to synchronize operations on this stream. |
Modifier | Constructor | Description |
---|---|---|
protected |
Creates a new character-stream reader whose critical sections will synchronize on the reader itself. |
|
protected |
Creates a new character-stream reader whose critical sections will synchronize on the given object. |
Modifier and Type | Method | Description |
---|---|---|
abstract void |
close() |
Closes the stream and releases any system resources associated with it. |
void |
mark |
Marks the present position in the stream. |
boolean |
markSupported() |
Tells whether this stream supports the mark() operation. |
static Reader |
nullReader() |
Returns a new Reader that reads no characters. |
int |
read() |
Reads a single character. |
int |
read |
Reads characters into an array. |
abstract int |
read |
Reads characters into a portion of an array. |
int |
read |
Attempts to read characters into the specified character buffer. |
boolean |
ready() |
Tells whether this stream is ready to be read. |
void |
reset() |
Resets the stream. |
long |
skip |
Skips characters. |
long |
transferTo |
Reads all characters from this reader and writes the characters to the given writer in the order that they are read. |
protected Object lock
this
or a synchronized method.protected Reader()
protected Reader(Object lock)
lock
- The Object to synchronize on.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 int read(CharBuffer target) throws IOException
read
in interface Readable
target
- the buffer to read characters intoIOException
- if an I/O error occursNullPointerException
- if target is nullReadOnlyBufferException
- if target is a read only bufferpublic int read() throws IOException
Subclasses that intend to support efficient single-character input should override this method.
0x00-0xffff
), or -1 if the end of the stream has been reachedIOException
- If an I/O error occurspublic int read(char[] cbuf) throws IOException
If the length of cbuf
is zero, then no characters are read and 0
is returned; otherwise, there is an attempt to read at least one character. If no character is available because the stream is at its end, the value -1
is returned; otherwise, at least one character is read and stored into cbuf
.
cbuf
- Destination bufferIOException
- If an I/O error occurspublic abstract int read(char[] cbuf, int off, int len) throws IOException
If len
is zero, then no characters are read and 0
is returned; otherwise, there is an attempt to read at least one character. If no character is available because the stream is at its end, the value -1
is returned; otherwise, at least one character is read and stored into cbuf
.
cbuf
- Destination bufferoff
- Offset at which to start storing characterslen
- Maximum number of characters to readIndexOutOfBoundsException
- If off
is negative, or len
is negative, or len
is greater than cbuf.length - off
IOException
- If an I/O error occurspublic long skip(long n) throws IOException
n
- The number of characters to skipIllegalArgumentException
- If n
is negative.IOException
- If an I/O error occurspublic boolean ready() throws IOException
IOException
- If an I/O error occurspublic boolean markSupported()
public void mark(int readAheadLimit) throws IOException
readAheadLimit
- Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail.IOException
- If the stream does not support mark(), or if some other I/O error occurspublic void reset() throws IOException
IOException
- If the stream has not been marked, or if the mark has been invalidated, or if the stream does not support reset(), or if some other I/O error occurspublic abstract void close() throws IOException
close
in interface AutoCloseable
close
in interface Closeable
IOException
- If an I/O error occurspublic 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 the total number of characters transferred is greater than Long.MAX_VALUE, then Long.MAX_VALUE
will be returned.
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
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/Reader.html