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:
java.io.Closeable,java.lang.AutoCloseable,java.lang.Readable
public class UnsynchronizedBufferedReader extends UnsynchronizedReader
Wraps an existingReaderand 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:
BufferedReader,BufferedWriter
-
-
Constructor Summary
Constructors Constructor Description UnsynchronizedBufferedReader(java.io.Reader in)Constructs a new BufferedReader on the Readerin.UnsynchronizedBufferedReader(java.io.Reader in, int size)Constructs a new BufferedReader on the Readerin.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description (package private) voidchompNewline()Peeks at the next input character, refilling the buffer if necessary.voidclose()Closes this reader.private intfillBuf()Populates the buffer with data.voidmark(int markLimit)Sets a mark position in this reader.booleanmarkSupported()intpeek()Returns the next character in the current reader without consuming it.intpeek(char[] buf)Populates the buffer with the nextbuf.lengthcharacters in the current reader without consuming them.intread()Reads a single character from this reader and returns it with the two higher-order bytes set to 0.intread(char[] buffer, int offset, int length)Reads at mostlengthcharacters from this reader and stores them atoffsetin the character arraybuffer.java.lang.StringreadLine()Returns the next line of text available from this reader.booleanready()Tests whether this reader is ready to be read without blocking.voidreset()Resets this reader's position to the lastmark()location.longskip(long amount)Skipsamountcharacters in this reader.-
Methods inherited from class org.apache.commons.io.input.UnsynchronizedReader
checkOpen, isClosed, setClosed
-
-
-
-
Field Detail
-
NUL
private static final char NUL
- See Also:
- Constant Field Values
-
in
private final java.io.Reader in
-
buf
private char[] buf
The characters that can be read and refilled in bulk. We maintain three indices into this buffer:{ X X X X X X X X X X X X - - } ^ ^ ^ | | | mark pos endPos points to the next readable character. End is one greater than the last readable character. When
pos == end, the buffer is empty and must befilledbefore characters can be read.Mark is the value pos will be set to on calls to
reset(). Its value is in the range[0...pos]. If the mark is-1, the buffer cannot be reset.MarkLimit limits the distance between the mark and the pos. When this limit is exceeded,
reset()is permitted (but not required) to throw an exception. For shorter distances,reset()shall not throw (unless the reader is closed).
-
pos
private int pos
-
end
private int end
-
mark
private int mark
-
markLimit
private int markLimit
-
-
Constructor Detail
-
UnsynchronizedBufferedReader
public UnsynchronizedBufferedReader(java.io.Reader in)
Constructs a new BufferedReader on the Readerin. The buffer gets the default size (8 KB).- Parameters:
in- the Reader that is buffered.
-
UnsynchronizedBufferedReader
public UnsynchronizedBufferedReader(java.io.Reader in, int size)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:
java.lang.IllegalArgumentException- ifsize <= 0.
-
-
Method Detail
-
chompNewline
final void chompNewline() throws java.io.IOExceptionPeeks at the next input character, refilling the buffer if necessary. If this character is a newline character ("\n"), it is discarded.- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOExceptionCloses 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:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classUnsynchronizedReader- Throws:
java.io.IOException- if an error occurs while closing this reader.
-
fillBuf
private int fillBuf() throws java.io.IOExceptionPopulates the buffer with data. It is an error to call this method when the buffer still contains data; ie. ifpos < end.- Returns:
- the number of bytes read into the buffer, or -1 if the end of the source stream has been reached.
- Throws:
java.io.IOException
-
mark
public void mark(int markLimit) throws java.io.IOExceptionSets a mark position in this reader. The parametermarkLimitindicates how many characters can be read before the mark is invalidated. Callingreset()will reposition the reader back to the marked position ifmarkLimithas not been surpassed.- Overrides:
markin classjava.io.Reader- Parameters:
markLimit- the number of characters that can be read before the mark is invalidated.- Throws:
java.lang.IllegalArgumentException- ifmarkLimit < 0.java.io.IOException- if an error occurs while setting a mark in this reader.- See Also:
markSupported(),reset()
-
markSupported
public boolean markSupported()
-
peek
public int peek() throws java.io.IOExceptionReturns 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:
java.io.IOException- If an I/O error occurs
-
peek
public int peek(char[] buf) throws java.io.IOExceptionPopulates the buffer with the nextbuf.lengthcharacters 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:
java.io.IOException- If an I/O error occurs
-
read
public int read() throws java.io.IOExceptionReads 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:
readin classjava.io.Reader- Returns:
- the character read or -1 if the end of the source reader has been reached.
- Throws:
java.io.IOException- if this reader is closed or some other I/O error occurs.
-
read
public int read(char[] buffer, int offset, int length) throws java.io.IOExceptionReads at mostlengthcharacters from this reader and stores them atoffsetin 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:
readin classjava.io.Reader- Parameters:
buffer- the character array to store the characters read.offset- the initial position inbufferto 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:
java.lang.IndexOutOfBoundsException- ifoffset < 0orlength < 0, or ifoffset + lengthis greater than the size ofbuffer.java.io.IOException- if this reader is closed or some other I/O error occurs.
-
readLine
public java.lang.String readLine() throws java.io.IOExceptionReturns 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
nullif no characters were read before the end of the reader has been reached. - Throws:
java.io.IOException- if this reader is closed or some other I/O error occurs.
-
ready
public boolean ready() throws java.io.IOExceptionTests whether this reader is ready to be read without blocking.- Overrides:
readyin classjava.io.Reader- Returns:
trueif this reader will not block whenreadis called,falseif unknown or blocking will occur.- Throws:
java.io.IOException- if this reader is closed or some other I/O error occurs.- See Also:
read(),read(char[], int, int),readLine()
-
reset
public void reset() throws java.io.IOExceptionResets this reader's position to the lastmark()location. Invocations ofread()andskip()will occur from this new location.- Overrides:
resetin classjava.io.Reader- Throws:
java.io.IOException- if this reader is closed or no mark has been set.- See Also:
mark(int),markSupported()
-
skip
public long skip(long amount) throws java.io.IOExceptionSkipsamountcharacters in this reader. Subsequentread()s will not return these characters unlessreset()is used. Skipping characters may invalidate a mark ifmarkLimitis surpassed.- Overrides:
skipin classUnsynchronizedReader- Parameters:
amount- the maximum number of characters to skip.- Returns:
- the number of characters actually skipped.
- Throws:
java.lang.IllegalArgumentException- ifamount < 0.java.io.IOException- if this reader is closed or some other I/O error occurs.- See Also:
mark(int),markSupported(),reset()
-
-