org.apache.commons.io.input
Class NullReader
Readerorg.apache.commons.io.input.NullReader
public class NullReader
extends Reader
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 <32length; i++) {
chars[i] = ... // set array value here
}
}
}
NullReader(long size)- Create a
Reader that emulates a specified size
which supports marking and does not throw EOFException.
|
NullReader(long size, boolean markSupported, boolean throwEofException)- Create a
Reader that emulates a specified
size with option settings.
|
void | close()- Close this Reader - resets the internal state to
the initial values.
|
private int | doEndOfFile()- Handle End of File.
|
long | getPosition()- Return the current position.
|
long | getSize()- Return the size this
Reader emulates.
|
void | mark(int readlimit)- Mark the current position.
|
boolean | markSupported()- Indicates whether mark is supported.
|
protected int | processChar()- Return 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()- Read a character.
|
int | read(char[] chars)- Read some characters into the specified array.
|
int | read(char[] chars, int offset, int length)- Read the specified number characters into an array.
|
void | reset()- Reset the stream to the point when mark was last called.
|
long | skip(long numberOfChars)- Skip a specified number of characters.
|
markSupported
private boolean markSupported
position
private long position
readlimit
private long readlimit
throwEofException
private boolean throwEofException
NullReader
public NullReader(long size)
Create a Reader that emulates a specified size
which supports marking and does not throw EOFException.
size - The size of the reader to emulate.
NullReader
public NullReader(long size,
boolean markSupported,
boolean throwEofException) Create a 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.
close
public void close()
throws IOException Close this Reader - resets the internal state to
the initial values.
doEndOfFile
private int doEndOfFile()
throws EOFException Handle End of File.
-1 if throwEofException is
set to false
getPosition
public long getPosition()
Return the current position.
getSize
public long getSize()
Return the size this Reader emulates.
- The size of the reader to emulate.
mark
public void mark(int readlimit)
Mark the current position.
readlimit - The number of characters before this marked position
is invalid.
markSupported
public boolean markSupported()
Indicates whether mark is supported.
- Whether mark is supported or not.
processChar
protected int processChar()
Return a character value for the
read() method.
This implementation returns zero.
- This implementation always returns zero.
processChars
protected void processChars(char[] chars,
int offset,
int length) Process the characters for the
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.
read
public int read()
throws IOException Read a character.
- Either The character value returned by
processChar()
or -1 if the end of file has been reached and
throwEofException is set to false.
read
public int read(char[] chars)
throws IOException Read some characters into the specified array.
chars - The character array to read into
- The number of characters read or
-1
if the end of file has been reached and
throwEofException is set to false.
read
public int read(char[] chars,
int offset,
int length)
throws IOException Read the specified number characters into an array.
chars - The character array to read into.offset - The offset to start reading characters into.length - The number of characters to read.
- The number of characters read or
-1
if the end of file has been reached and
throwEofException is set to false.
reset
public void reset()
throws IOException Reset the stream to the point when mark was last called.
skip
public long skip(long numberOfChars)
throws IOException Skip a specified number of characters.
numberOfChars - The number of characters to skip.
- The number of characters skipped or
-1
if the end of file has been reached and
throwEofException is set to false.
Copyright (c) 2002-2010 Apache Software Foundation