public class UnsynchronizedBufferedReader extends UnsynchronizedReader
Reader and buffers the input without any synchronization. Expensive interaction with the underlying reader is minimized,
since most (smaller) requests can be satisfied by accessing the buffer alone. The drawback is that some extra space is required to hold the buffer and that
copying takes place when filling that buffer, but this is usually outweighed by the performance benefits.
A typical application pattern for the class looks like this:
UnsynchronizedBufferedReader buf = new UnsynchronizedBufferedReader(new FileReader("file"));
Provenance: Apache Harmony's java.io.BufferedReader, renamed, and modified.
BufferedReader,
BufferedWriter| Constructor and Description |
|---|
UnsynchronizedBufferedReader(Reader in)
Constructs a new BufferedReader on the Reader
in. |
UnsynchronizedBufferedReader(Reader in,
int size)
Constructs a new BufferedReader on the Reader
in. |
| Modifier and Type | Method and Description |
|---|---|
(package private) void |
chompNewline()
Peeks at the next input character, refilling the buffer if necessary.
|
void |
close()
Closes this reader.
|
void |
mark(int markLimit)
Sets a mark position in this reader.
|
boolean |
markSupported()
|
int |
peek()
Returns the next character in the current reader without consuming it.
|
int |
peek(char[] buf)
Populates the buffer with the next
buf.length characters in the current reader without consuming them. |
int |
read()
Reads a single character from this reader and returns it with the two higher-order bytes set to 0.
|
int |
read(char[] buffer,
int offset,
int length)
Reads at most
length characters from this reader and stores them at offset in the character array buffer. |
String |
readLine()
Returns the next line of text available from this reader.
|
boolean |
ready()
Tests 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 amount)
Skips
amount characters in this reader. |
checkOpen, isClosed, setClosedpublic UnsynchronizedBufferedReader(Reader in)
in. The buffer gets the default size (8 KB).in - the Reader that is buffered.public UnsynchronizedBufferedReader(Reader in, int size)
in. The buffer size is specified by the parameter size.in - the Reader that is buffered.size - the size of the buffer to allocate.IllegalArgumentException - if size <= 0.final void chompNewline()
throws IOException
IOExceptionpublic void close()
throws IOException
close in interface Closeableclose in interface AutoCloseableclose in class UnsynchronizedReaderIOException - if an error occurs while closing this reader.public void mark(int markLimit)
throws IOException
markLimit indicates how many characters can be read before the mark is invalidated. Calling
reset() will reposition the reader back to the marked position if markLimit has not been surpassed.mark in class ReadermarkLimit - the number of characters that can be read before the mark is invalidated.IllegalArgumentException - if markLimit < 0.IOException - if an error occurs while setting a mark in this reader.markSupported(),
reset()public boolean markSupported()
mark(int) and reset() methods. This implementation returns true.markSupported in class Readertrue for BufferedReader.mark(int),
reset()public int peek()
throws IOException
read() will still return this value.IOException - If an I/O error occurspublic int peek(char[] buf)
throws IOException
buf.length characters in the current reader without consuming them. The next call to read() will
still return the next value.buf - the buffer to fill for the look ahead.IOException - If an I/O error occurspublic int read()
throws IOException
read in class ReaderIOException - if this reader is closed or some other I/O error occurs.public int read(char[] buffer,
int offset,
int length)
throws IOException
length 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 source reader has been reached. If all the buffered characters have been used, a mark has not been set
and the requested number of characters is larger than this readers buffer size, BufferedReader bypasses the buffer and simply places the results directly
into buffer.read in class Readerbuffer - the character array to store the characters read.offset - the initial position in buffer to store the bytes read from this reader.length - the maximum number of characters to read, must be non-negative.IndexOutOfBoundsException - if offset < 0 or length < 0, or if offset + length is greater than the size of buffer.IOException - if this reader is closed or some other I/O error occurs.public String readLine() throws IOException
LF, CR,
"\r\n" or the end of the reader. The string does not include the newline sequence.null if no characters were read before 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
ready in class Readertrue if this reader will not block when read is called, false if unknown or blocking will occur.IOException - if this reader is closed or some other I/O error occurs.read(),
read(char[], int, int),
readLine()public void reset()
throws IOException
mark() location. Invocations of read() and skip() will occur from this new location.reset in class ReaderIOException - if this reader is closed or no mark has been set.mark(int),
markSupported()public long skip(long amount)
throws IOException
amount characters in this reader. Subsequent read()s will not return these characters unless reset() is used. Skipping
characters may invalidate a mark if markLimit is surpassed.skip in class UnsynchronizedReaderamount - the maximum number of characters to skip.IllegalArgumentException - if amount < 0.IOException - if this reader is closed or some other I/O error occurs.mark(int),
markSupported(),
reset()