Package org.apache.commons.io.input
Class UnsynchronizedBufferedReader
java.lang.Object
java.io.Reader
org.apache.commons.io.input.UnsynchronizedReader
org.apache.commons.io.input.UnsynchronizedBufferedReader
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Readable
Wraps an existing
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.
- Since:
- 2.17.0
- See Also:
-
Field Summary
-
Constructor Summary
ConstructorDescriptionConstructs a new BufferedReader on the Readerin
.UnsynchronizedBufferedReader
(Reader in, int size) Constructs a new BufferedReader on the Readerin
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this reader.void
mark
(int markLimit) Sets a mark position in this reader.boolean
int
peek()
Returns the next character in the current reader without consuming it.int
peek
(char[] buf) Populates the buffer with the nextbuf.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 mostlength
characters from this reader and stores them atoffset
in the character arraybuffer
.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 lastmark()
location.long
skip
(long amount) Skipsamount
characters in this reader.Methods inherited from class org.apache.commons.io.input.UnsynchronizedReader
isClosed, setClosed
-
Constructor Details
-
UnsynchronizedBufferedReader
Constructs a new BufferedReader on the Readerin
. The buffer gets the default size (8 KB).- Parameters:
in
- the Reader that is buffered.
-
UnsynchronizedBufferedReader
Constructs a new BufferedReader on the Readerin
. The buffer size is specified by the parametersize
.- Parameters:
in
- the Reader that is buffered.size
- the size of the buffer to allocate.- Throws:
IllegalArgumentException
- ifsize <= 0
.
-
-
Method Details
-
close
Closes this reader. This implementation closes the buffered source reader and releases the buffer. Nothing is done if this reader has already been closed.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classUnsynchronizedReader
- Throws:
IOException
- if an error occurs while closing this reader.
-
mark
Sets a mark position in this reader. The parametermarkLimit
indicates how many characters can be read before the mark is invalidated. Callingreset()
will reposition the reader back to the marked position ifmarkLimit
has not been surpassed.- Overrides:
mark
in classReader
- Parameters:
markLimit
- the number of characters that can be read before the mark is invalidated.- Throws:
IllegalArgumentException
- ifmarkLimit < 0
.IOException
- if an error occurs while setting a mark in this reader.- See Also:
-
markSupported
Tests whether this reader supports themark(int)
andreset()
methods. This implementation returnstrue
.- Overrides:
markSupported
in classReader
- Returns:
true
forBufferedReader
.- See Also:
-
peek
Returns the next character in the current reader without consuming it. So the next call toread()
will still return this value.- Returns:
- the next character
- Throws:
IOException
- If an I/O error occurs
-
peek
Populates the buffer with the nextbuf.length
characters in the current reader without consuming them. The next call toread()
will still return the next value.- Parameters:
buf
- the buffer to fill for the look ahead.- Returns:
- the buffer itself
- Throws:
IOException
- If an I/O error occurs
-
read
Reads a single character from this reader and returns it with the two higher-order bytes set to 0. If possible, BufferedReader returns a character from the buffer. If there are no characters available in the buffer, it fills the buffer and then returns a character. It returns -1 if there are no more characters in the source reader.- Overrides:
read
in classReader
- Returns:
- the character read or -1 if the end of the source reader has been reached.
- Throws:
IOException
- if this reader is closed or some other I/O error occurs.
-
read
Reads at mostlength
characters from this reader and stores them atoffset
in the character arraybuffer
. 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 intobuffer
.- Specified by:
read
in classReader
- Parameters:
buffer
- the character array to store the characters read.offset
- the initial position inbuffer
to store the bytes read from this reader.length
- the maximum number of characters to read, must be non-negative.- Returns:
- number of characters read or -1 if the end of the source reader has been reached.
- Throws:
IndexOutOfBoundsException
- ifoffset < 0
orlength < 0
, or ifoffset + length
is greater than the size ofbuffer
.IOException
- if this reader is closed or some other I/O error occurs.
-
readLine
Returns the next line of text available from this reader. A line is represented by zero or more characters followed byLF
,CR
,"\r\n"
or the end of the reader. The string does not include the newline sequence.- Returns:
- the contents of the line or
null
if no characters were read before the end of the reader has been reached. - Throws:
IOException
- if this reader is closed or some other I/O error occurs.
-
ready
Tests whether this reader is ready to be read without blocking.- Overrides:
ready
in classReader
- Returns:
true
if this reader will not block whenread
is called,false
if unknown or blocking will occur.- Throws:
IOException
- if this reader is closed or some other I/O error occurs.- See Also:
-
reset
Resets this reader's position to the lastmark()
location. Invocations ofread()
andskip()
will occur from this new location.- Overrides:
reset
in classReader
- Throws:
IOException
- if this reader is closed or no mark has been set.- See Also:
-
skip
Skipsamount
characters in this reader. Subsequentread()
s will not return these characters unlessreset()
is used. Skipping characters may invalidate a mark ifmarkLimit
is surpassed.- Overrides:
skip
in classUnsynchronizedReader
- Parameters:
amount
- the maximum number of characters to skip.- Returns:
- the number of characters actually skipped.
- Throws:
IllegalArgumentException
- ifamount < 0
.IOException
- if this reader is closed or some other I/O error occurs.- See Also:
-