Package org.apache.commons.io.input
Class NullReader
java.lang.Object
java.io.Reader
org.apache.commons.io.input.NullReader
- All Implemented Interfaces:
Closeable
,AutoCloseable
,Readable
A functional, light weight
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 } } }
- Since:
- 1.3
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
static final NullReader
The singleton instance.private long
private final boolean
private long
private long
private final long
private final boolean
-
Constructor Summary
ConstructorsConstructorDescriptionCreates aReader
that emulates a size 0 reader which supports marking and does not throw EOFException.NullReader
(long size) Creates aReader
that emulates a specified size which supports marking and does not throw EOFException.NullReader
(long size, boolean markSupported, boolean throwEofException) Creates aReader
that emulates a specified size with option settings. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this Reader - resets the internal state to the initial values.private int
Handles End of File.long
Returns the current position.long
getSize()
Returns the size thisReader
emulates.void
mark
(int readlimit) Marks the current position.boolean
Indicates whether mark is supported.protected int
Returns a character value for theread()
method.protected void
processChars
(char[] chars, int offset, int length) Process the characters for theread(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.
-
Field Details
-
INSTANCE
The singleton instance.- Since:
- 2.12.0
-
size
private final long size -
position
private long position -
mark
private long mark -
readlimit
private long readlimit -
eof
private boolean eof -
throwEofException
private final boolean throwEofException -
markSupported
private final boolean markSupported
-
-
Constructor Details
-
NullReader
public NullReader()Creates aReader
that emulates a size 0 reader which supports marking and does not throw EOFException.- Since:
- 2.7
-
NullReader
public NullReader(long size) Creates aReader
that emulates a specified size which supports marking and does not throw EOFException.- Parameters:
size
- The size of the reader to emulate.
-
NullReader
public NullReader(long size, boolean markSupported, boolean throwEofException) Creates aReader
that emulates a specified size with option settings.- Parameters:
size
- The size of the reader to emulate.markSupported
- Whether this instance will support themark()
functionality.throwEofException
- Whether this implementation will throw anEOFException
or return -1 when the end of file is reached.
-
-
Method Details
-
close
Closes this Reader - resets the internal state to the initial values.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classReader
- Throws:
IOException
- If an error occurs.
-
doEndOfFile
Handles End of File.- Returns:
-1
ifthrowEofException
is set tofalse
- Throws:
EOFException
- ifthrowEofException
is set totrue
.
-
getPosition
public long getPosition()Returns the current position.- Returns:
- the current position.
-
getSize
public long getSize()Returns the size thisReader
emulates.- Returns:
- The size of the reader to emulate.
-
mark
public void mark(int readlimit) Marks the current position.- Overrides:
mark
in classReader
- Parameters:
readlimit
- The number of characters before this marked position is invalid.- Throws:
UnsupportedOperationException
- if mark is not supported.
-
markSupported
public boolean markSupported()Indicates whether mark is supported.- Overrides:
markSupported
in classReader
- Returns:
- Whether mark is supported or not.
-
processChar
protected int processChar()Returns a character value for theread()
method.This implementation returns zero.
- Returns:
- This implementation always returns zero.
-
processChars
protected void processChars(char[] chars, int offset, int length) Process the characters for theread(char[], offset, length)
method.This implementation leaves the character array unchanged.
- Parameters:
chars
- The character arrayoffset
- The offset to start at.length
- The number of characters.
-
read
Reads a character.- Overrides:
read
in classReader
- Returns:
- Either The character value returned by
processChar()
or-1
if the end of file has been reached andthrowEofException
is set tofalse
. - Throws:
EOFException
- if the end of file is reached andthrowEofException
is set totrue
.IOException
- if trying to read past the end of file.
-
read
Reads some characters into the specified array.- Overrides:
read
in classReader
- Parameters:
chars
- The character array to read into- Returns:
- The number of characters read or
-1
if the end of file has been reached andthrowEofException
is set tofalse
. - Throws:
EOFException
- if the end of file is reached andthrowEofException
is set totrue
.IOException
- if trying to read past the end of file.
-
read
Reads the specified number characters into an array.- Specified by:
read
in classReader
- Parameters:
chars
- The character array to read into.offset
- The offset to start reading characters into.length
- The number of characters to read.- Returns:
- The number of characters read or
-1
if the end of file has been reached andthrowEofException
is set tofalse
. - Throws:
EOFException
- if the end of file is reached andthrowEofException
is set totrue
.IOException
- if trying to read past the end of file.
-
reset
Resets the stream to the point when mark was last called.- Overrides:
reset
in classReader
- Throws:
UnsupportedOperationException
- if mark is not supported.IOException
- If no position has been marked or the read limit has been exceeded since the last position was marked.
-
skip
Skips a specified number of characters.- Overrides:
skip
in classReader
- Parameters:
numberOfChars
- The number of characters to skip.- Returns:
- The number of characters skipped or
-1
if the end of file has been reached andthrowEofException
is set tofalse
. - Throws:
EOFException
- if the end of file is reached andthrowEofException
is set totrue
.IOException
- if trying to read past the end of file.
-