Class NullReader
- java.lang.Object
-
- java.io.Reader
-
- org.apache.commons.io.input.NullReader
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.lang.Readable
public class NullReader extends java.io.Reader
A functional, lightweightReaderthat emulates a reader of a specified size.This implementation provides a lightweight object for testing with an
Readerwhere the contents don't matter.One use case would be for testing the handling of large
Readeras 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()andprocessChars()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 } } }This class is not thread-safe.
- Since:
- 1.3
-
-
Field Summary
Fields Modifier and Type Field Description static NullReaderINSTANCEThe singleton instance.
-
Constructor Summary
Constructors Constructor Description NullReader()Constructs aReaderthat emulates a size 0 reader which supports marking and does not throw EOFException.NullReader(long size)Constructs aReaderthat emulates a specified size which supports marking and does not throw EOFException.NullReader(long size, boolean markSupported, boolean throwEofException)Constructs aReaderthat emulates a specified size with option settings.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Closes this Reader.longgetPosition()Gets the current position.longgetSize()Gets the size thisReaderemulates.voidmark(int readLimit)Marks the current position.booleanmarkSupported()Tests whether mark is supported.protected intprocessChar()Returns a character value for theread()method.protected voidprocessChars(char[] chars, int offset, int length)Process the characters for theread(char[], offset, length)method.intread()Reads a character.intread(char[] chars)Reads some characters into the specified array.intread(char[] chars, int offset, int length)Reads the specified number characters into an array.voidreset()Resets the stream to the point when mark was last called.longskip(long numberOfChars)Skips a specified number of characters.
-
-
-
Field Detail
-
INSTANCE
public static final NullReader INSTANCE
The singleton instance.- Since:
- 2.12.0
-
-
Constructor Detail
-
NullReader
public NullReader()
Constructs aReaderthat emulates a size 0 reader which supports marking and does not throw EOFException.- Since:
- 2.7
-
NullReader
public NullReader(long size)
Constructs aReaderthat 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)
Constructs aReaderthat 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 anEOFExceptionor return -1 when the end of file is reached.
-
-
Method Detail
-
close
public void close() throws java.io.IOException
Closes this Reader. Resets the internal state to the initial values.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein classjava.io.Reader- Throws:
java.io.IOException- If an error occurs.
-
getPosition
public long getPosition()
Gets the current position.- Returns:
- the current position.
-
getSize
public long getSize()
Gets the size thisReaderemulates.- Returns:
- The size of the reader to emulate.
-
mark
public void mark(int readLimit)
Marks the current position.- Overrides:
markin classjava.io.Reader- Parameters:
readLimit- The number of characters before this marked position is invalid.- Throws:
java.lang.UnsupportedOperationException- if mark is not supported.
-
markSupported
public boolean markSupported()
Tests whether mark is supported.- Overrides:
markSupportedin classjava.io.Reader- 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
public int read() throws java.io.IOException
Reads a character.- Overrides:
readin classjava.io.Reader- Returns:
- Either The character value returned by
processChar()or-1if the end of file has been reached andthrowEofExceptionis set tofalse. - Throws:
java.io.EOFException- if the end of file is reached andthrowEofExceptionis set totrue.java.io.IOException- if trying to read past the end of file.
-
read
public int read(char[] chars) throws java.io.IOException
Reads some characters into the specified array.- Overrides:
readin classjava.io.Reader- Parameters:
chars- The character array to read into- Returns:
- The number of characters read or
-1if the end of file has been reached andthrowEofExceptionis set tofalse. - Throws:
java.io.EOFException- if the end of file is reached andthrowEofExceptionis set totrue.java.io.IOException- if trying to read past the end of file.
-
read
public int read(char[] chars, int offset, int length) throws java.io.IOException
Reads the specified number characters into an array.- Specified by:
readin classjava.io.Reader- 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
-1if the end of file has been reached andthrowEofExceptionis set tofalse. - Throws:
java.lang.NullPointerException- if the array isnull.java.lang.IndexOutOfBoundsException- ifoffsetorlengthare negative, or ifoffset + lengthis greater thanchars.length.java.io.EOFException- if the end of file is reached andthrowEofExceptionis set totrue.java.io.IOException- if trying to read past the end of file.
-
reset
public void reset() throws java.io.IOException
Resets the stream to the point when mark was last called.- Overrides:
resetin classjava.io.Reader- Throws:
java.lang.UnsupportedOperationException- if mark is not supported.java.io.IOException- If no position has been marked or the read limit has been exceeded since the last position was marked.
-
skip
public long skip(long numberOfChars) throws java.io.IOException
Skips a specified number of characters.- Overrides:
skipin classjava.io.Reader- Parameters:
numberOfChars- The number of characters to skip.- Returns:
- The number of characters skipped or
-1if the end of file has been reached andthrowEofExceptionis set tofalse. - Throws:
java.io.EOFException- if the end of file is reached andthrowEofExceptionis set totrue.java.io.IOException- if trying to read past the end of file.
-
-