Package org.apache.commons.io.input
Class BoundedReader
- java.lang.Object
-
- java.io.Reader
-
- org.apache.commons.io.input.BoundedReader
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.lang.Readable
public class BoundedReader extends java.io.ReaderA 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 java.io.Reader#mark(int), since reading too many characters removes the ability to do a successful reset.
- Since:
- 2.5
-
-
Constructor Summary
Constructors Constructor Description BoundedReader(java.io.Reader target, int maxCharsFromTargetReader)Constructs a bounded reader
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes the targetvoidmark(int readAheadLimit)marks the target streamintread()Reads a single characterintread(char[] cbuf, int off, int len)Reads into an arrayvoidreset()Resets the target to the latest mark,
-
-
-
Constructor Detail
-
BoundedReader
public BoundedReader(java.io.Reader target, int maxCharsFromTargetReader) throws java.io.IOExceptionConstructs a bounded reader- Parameters:
target- The target stream that will be usedmaxCharsFromTargetReader- The maximum number of characters that can be read from target- Throws:
java.io.IOException- if mark fails
-
-
Method Detail
-
close
public void close() throws java.io.IOExceptionCloses the target- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein classjava.io.Reader- Throws:
java.io.IOException- If an I/O error occurs while calling the underlying reader's close method
-
reset
public void reset() throws java.io.IOExceptionResets the target to the latest mark,- Overrides:
resetin classjava.io.Reader- Throws:
java.io.IOException- If an I/O error occurs while calling the underlying reader's reset method- See Also:
Reader.reset()
-
mark
public void mark(int readAheadLimit) throws java.io.IOExceptionmarks the target stream- Overrides:
markin classjava.io.Reader- 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:
java.io.IOException- If an I/O error occurs while calling the underlying reader's mark method- See Also:
Reader.mark(int)
-
read
public int read() throws java.io.IOExceptionReads a single character- Overrides:
readin classjava.io.Reader- Returns:
- -1 on eof or the character read
- Throws:
java.io.IOException- If an I/O error occurs while calling the underlying reader's read method- See Also:
Reader.read()
-
read
public int read(char[] cbuf, int off, int len) throws java.io.IOExceptionReads into an array- Specified by:
readin classjava.io.Reader- Parameters:
cbuf- The buffer to filloff- The offsetlen- The number of chars to read- Returns:
- the number of chars read
- Throws:
java.io.IOException- If an I/O error occurs while calling the underlying reader's read method- See Also:
Reader.read(char[], int, int)
-
-