Package org.apache.commons.io.input
Class BoundedReader
java.lang.Object
java.io.Reader
java.io.FilterReader
org.apache.commons.io.input.ProxyReader
org.apache.commons.io.input.BoundedReader
- All Implemented Interfaces:
Closeable,AutoCloseable,Readable
A reader that imposes a limit to the number of characters that can be read from an underlying reader, returning EOF
when this limit is reached, regardless of state of underlying reader.
One use case is to avoid overrunning the readAheadLimit supplied to Reader.mark(int), since reading
too many characters removes the ability to do a successful reset.
- Since:
- 2.5
-
Field Summary
Fields inherited from class java.io.FilterReader
in -
Constructor Summary
ConstructorsConstructorDescriptionBoundedReader(Reader target, int maxCharsFromTargetReader) Constructs a bounded reader -
Method Summary
Methods inherited from class org.apache.commons.io.input.ProxyReader
afterRead, beforeRead, close, handleIOException, markSupported, read, read, ready, setReference, unwrap
-
Constructor Details
-
BoundedReader
Constructs a bounded reader- Parameters:
target- The target stream that will be used.maxCharsFromTargetReader- The maximum number of characters that can be read from target.
-
-
Method Details
-
mark
marks the target stream- Overrides:
markin classProxyReader- Parameters:
readAheadLimit- The number of characters that can be read while still retaining the ability to do #reset(). Note that this parameter is not validated with respect to maxCharsFromTargetReader. There is no way to pass past maxCharsFromTargetReader, even if this value is greater.- Throws:
IOException- If an I/O error occurs while calling the underlying reader's mark method.- See Also:
-
read
Reads a single character- Overrides:
readin classProxyReader- Returns:
- -1 on EOF or the character read.
- Throws:
IOException- If an I/O error occurs while calling the underlying reader's read method.- See Also:
-
read
Reads into an array- Overrides:
readin classProxyReader- Parameters:
cbuf- The buffer to fill.off- The offset.len- The number of chars to read.- Returns:
- the number of chars read.
- Throws:
NullPointerException- if the buffer isnull.IndexOutOfBoundsException- ifofforlenare negative, or ifoff + lenis greater thancbuf.length.IOException- If an I/O error occurs while calling the underlying reader's read method.- See Also:
-
reset
Resets the target to the latest mark,- Overrides:
resetin classProxyReader- Throws:
IOException- If an I/O error occurs while calling the underlying reader's reset method.- See Also:
-
skip
Description copied from class:ProxyReaderInvokes the delegate'sskip(long)method.- Overrides:
skipin classProxyReader- Parameters:
n- the number of bytes to skip.- Returns:
- the number of bytes to skipped or EOF if the end of stream.
- Throws:
IOException- if an I/O error occurs.
-