Package java.io
Class Reader
java.lang.Object
java.io.Reader
- All Implemented Interfaces:
Closeable,AutoCloseable,Readable
- Direct Known Subclasses:
BufferedReader,CharArrayReader,FilterReader,InputStreamReader,PipedReader,StringReader
public abstract class Reader extends Object implements Readable, Closeable
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:
Writer
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description abstract voidclose()Closes this reader.voidmark(int readLimit)Sets a mark position in this reader.booleanmarkSupported()Indicates whether this reader supports themark()andreset()methods.intread()Reads a single character from this reader and returns it as an integer with the two higher-order bytes set to 0.intread(char[] buffer)Reads characters from this reader and stores them in the character arraybufferstarting at offset 0.abstract intread(char[] buffer, int offset, int count)Reads up tocountcharacters from this reader and stores them atoffsetin the character arraybuffer.intread(CharBuffer target)Reads characters and puts them into thetargetcharacter buffer.booleanready()Indicates whether this reader is ready to be read without blocking.voidreset()Resets this reader's position to the lastmark()location.longskip(long charCount)SkipscharCountcharacters in this reader.
-
Field Details
-
lock
The object used to synchronize access to the reader.
-
-
Constructor Details
-
Reader
protected Reader()Constructs a newReaderwiththisas the object used to synchronize critical sections. -
Reader
Constructs a newReaderwithlockused to synchronize critical sections.- Parameters:
lock- theObjectused to synchronize critical sections.- Throws:
NullPointerException- iflockisnull.
-
-
Method Details
-
close
Closes this reader. Implementations of this method should free any resources associated with the reader.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an error occurs while closing this reader.
-
mark
Sets a mark position in this reader. The parameterreadLimitindicates how many characters can be read before the mark is invalidated. Callingreset()will reposition the reader back to the marked position ifreadLimithas not been surpassed.This default implementation simply throws an
IOException; subclasses must provide their own implementation.- Parameters:
readLimit- the number of characters that can be read before the mark is invalidated.- Throws:
IllegalArgumentException- ifreadLimit < 0.IOException- if an error occurs while setting a mark in this reader.- See Also:
markSupported(),reset()
-
markSupported
public boolean markSupported()Indicates whether this reader supports themark()andreset()methods. This default implementation returnsfalse.- Returns:
- always
false.
-
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:
- 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
Reads characters from this reader and stores them in the character arraybufferstarting at offset 0. Returns the number of characters actually 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
Reads up tocountcharacters from this reader and stores them atoffsetin the character arraybuffer. Returns the number of characters actually 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.
-
ready
Indicates whether this reader is ready to be read without blocking. Returnstrueif this reader will not block whenreadis called,falseif unknown or blocking will occur. This default implementation always returnsfalse.- Returns:
- always
false. - Throws:
IOException- if this reader is closed or some other I/O error occurs.- See Also:
read(),read(char[]),read(char[], int, int)
-
reset
Resets this reader's position to the lastmark()location. Invocations ofread()andskip()will occur from this new location. If this reader has not been marked, the behavior ofreset()is implementation specific. This default implementation throws anIOException.- Throws:
IOException- always thrown in this default implementation.- See Also:
mark(int),markSupported()
-
skip
SkipscharCountcharacters in this reader. Subsequent calls ofreadmethods will not return these characters unlessresetis used. This method may perform multiple reads to readcharCountcharacters.- Returns:
- the number of characters actually skipped.
- Throws:
IllegalArgumentException- ifcharCount < 0.IOException- if this reader is closed or some other I/O error occurs.- See Also:
mark(int),markSupported(),reset()
-
read
Reads characters and puts them into thetargetcharacter buffer.- Specified by:
readin interfaceReadable- Parameters:
target- the destination character buffer.- Returns:
- the number of characters put into
targetor -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- iftargetisnull.ReadOnlyBufferException- iftargetis read-only.
-