public class NullReader extends Reader
Reader
that emulates
a reader of a specified size.
This implementation provides a light weight
object for testing with an Reader
where the contents don't matter.
One use case would be for testing the handling of
large Reader
as it can emulate that
scenario without the overhead of actually processing
large numbers of characters - significantly speeding up
test execution times.
This implementation returns a space from the method that
reads a character and leaves the array unchanged in the read
methods that are passed a character array.
If alternative data is required the processChar()
and
processChars()
methods can be implemented to generate
data, for example:
public class TestReader extends NullReader { public TestReader(int size) { super(size); } protected char processChar() { return ... // return required value here } protected void processChars(char[] chars, int offset, int length) { for (int i = offset; i < length; i++) { chars[i] = ... // set array value here } } }
Constructor and Description |
---|
NullReader()
Creates a
Reader that emulates a size 0 reader
which supports marking and does not throw EOFException. |
NullReader(long size)
Creates a
Reader that emulates a specified size
which supports marking and does not throw EOFException. |
NullReader(long size,
boolean markSupported,
boolean throwEofException)
Creates a
Reader that emulates a specified
size with option settings. |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes this Reader - resets the internal state to
the initial values.
|
long |
getPosition()
Returns the current position.
|
long |
getSize()
Returns the size this
Reader emulates. |
void |
mark(int readlimit)
Marks the current position.
|
boolean |
markSupported()
Indicates whether mark is supported.
|
protected int |
processChar()
Returns a character value for the
read() method. |
protected void |
processChars(char[] chars,
int offset,
int length)
Process the characters for the
read(char[], offset, length)
method. |
int |
read()
Reads a character.
|
int |
read(char[] chars)
Reads some characters into the specified array.
|
int |
read(char[] chars,
int offset,
int length)
Reads the specified number characters into an array.
|
void |
reset()
Resets the stream to the point when mark was last called.
|
long |
skip(long numberOfChars)
Skips a specified number of characters.
|
public NullReader()
Reader
that emulates a size 0 reader
which supports marking and does not throw EOFException.public NullReader(long size)
Reader
that emulates a specified size
which supports marking and does not throw EOFException.size
- The size of the reader to emulate.public NullReader(long size, boolean markSupported, boolean throwEofException)
Reader
that emulates a specified
size with option settings.size
- The size of the reader to emulate.markSupported
- Whether this instance will support
the mark()
functionality.throwEofException
- Whether this implementation
will throw an EOFException
or return -1 when the
end of file is reached.public long getPosition()
public long getSize()
Reader
emulates.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class Reader
IOException
- If an error occurs.public void mark(int readlimit)
mark
in class Reader
readlimit
- The number of characters before this marked position
is invalid.UnsupportedOperationException
- if mark is not supported.public boolean markSupported()
markSupported
in class Reader
public int read() throws IOException
read
in class Reader
processChar()
or -1
if the end of file has been reached and
throwEofException
is set to false
.EOFException
- if the end of file is reached and
throwEofException
is set to true
.IOException
- if trying to read past the end of file.public int read(char[] chars) throws IOException
read
in class Reader
chars
- The character array to read into-1
if the end of file has been reached and
throwEofException
is set to false
.EOFException
- if the end of file is reached and
throwEofException
is set to true
.IOException
- if trying to read past the end of file.public int read(char[] chars, int offset, int length) throws IOException
read
in class Reader
chars
- The character array to read into.offset
- The offset to start reading characters into.length
- The number of characters to read.-1
if the end of file has been reached and
throwEofException
is set to false
.EOFException
- if the end of file is reached and
throwEofException
is set to true
.IOException
- if trying to read past the end of file.public void reset() throws IOException
reset
in class Reader
UnsupportedOperationException
- if mark is not supported.IOException
- If no position has been marked
or the read limit has been exceed since the last position was
marked.public long skip(long numberOfChars) throws IOException
skip
in class Reader
numberOfChars
- The number of characters to skip.-1
if the end of file has been reached and
throwEofException
is set to false
.EOFException
- if the end of file is reached and
throwEofException
is set to true
.IOException
- if trying to read past the end of file.protected int processChar()
read()
method.
This implementation returns zero.
protected void processChars(char[] chars, int offset, int length)
read(char[], offset, length)
method.
This implementation leaves the character array unchanged.
chars
- The character arrayoffset
- The offset to start at.length
- The number of characters.Copyright © 2002–2020 The Apache Software Foundation. All rights reserved.