public class LimitedReader extends Reader
BufferedReader
is decorated).
The focus of this class is to make sure that not more characters than specified can be read from the underlying reader. It can
e.g. be used to ensure that reading beyond the read ahead limit of a call of mark(int)
is not possible.
LimitedInputStream
Constructor and Description |
---|
LimitedReader(Reader decoratedReader,
long limit)
Creates a new instance of this class.
|
Modifier and Type | Method and Description |
---|---|
long |
availableCharacters()
The number of characters available, before the specified maximum number will have been read from the decorated reader.
|
void |
close()
Closes the decorated reader, if that is currently allowed.
|
long |
getLimit()
Returns the maximum number of characters that will be read from the decorated reader.
|
boolean |
isAllowClose()
Determines whether closing this reader shall currently be prevented.
|
boolean |
isLimitReached()
Checks whether the specified maximum number of characters has been read from the underlying reader.
|
void |
mark(int readAheadLimit) |
boolean |
markSupported() |
int |
read(char[] buffer,
int offset,
int length) |
boolean |
ready() |
void |
reset() |
void |
setAllowClose(boolean allowClose)
Allows to specify whether closing this reader shall currently be prevented.
|
long |
skip(long n) |
public LimitedReader(Reader decoratedReader, long limit)
decoratedReader
- the reader to be decoratedlimit
- the maximum number of characters that shall be read from the decorated readerpublic long getLimit()
public long availableCharacters()
public boolean isLimitReached()
true
if the maximum number of characters was readpublic boolean isAllowClose()
close()
will throw
an ClosingNotAllowedException
in such cases.true
, if closing this reader will currently be prevented or false
otherwisepublic void setAllowClose(boolean allowClose)
close()
will then throw
an ClosingNotAllowedException
.allowClose
- Specify true
here, if closing shall be prevented from now on or false
otherwise.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class Reader
ClosingNotAllowedException
- if isAllowClose()
currently returns true
. (Calling this method, if
the decorated reader was already closed will also throw this exception, if closing is currently not allowed.)IOException
- if an I/O error occurssetAllowClose(boolean)
,
Reader.close()
public int read(char[] buffer, int offset, int length) throws IOException
read
in class Reader
IOException
public long skip(long n) throws IOException
skip
in class Reader
IOException
public void mark(int readAheadLimit) throws IOException
mark
in class Reader
IOException
public boolean markSupported()
markSupported
in class Reader
public boolean ready() throws IOException
ready
in class Reader
IOException
public void reset() throws IOException
reset
in class Reader
IOException