Top Description Fields Constructors Methods
org.apache.commons.io.input

public Class NullReader

extends Reader
Class Inheritance
Imports
java.io.EOFException, .IOException, .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 < length; i++) {
             chars[i] = ... // set array value here
         }
     }
 }
Since
Commons IO 1.3
Version
$Revision: 463529 $

Field Summary

Modifier and TypeField and Description
private boolean
private long
private boolean
private long
private long
private long
private boolean
Inherited from java.io.Reader:
lock

Constructor Summary

AccessConstructor and Description
public
NullReader(long
The size of the reader to emulate.
size
)

Create a Reader that emulates a specified size which supports marking and does not throw EOFException.

public
NullReader(long
The size of the reader to emulate.
size
,
boolean
Whether this instance will support the mark() functionality.
markSupported
,
boolean
Whether this implementation will throw an EOFException or return -1 when the end of file is reached.
throwEofException
)

Create a Reader that emulates a specified size with option settings.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements abstract java.io.Reader.close.

Implements java.io.Closeable.close.

Close this Reader - resets the internal state to the initial values.
private int

Returns:

-1 if throwEofException is set to false
doEndOfFile
()

Handle End of File.

public long

Returns:

the current position.
getPosition
()

Return the current position.

public long

Returns:

The size of the reader to emulate.
getSize
()

Return the size this Reader emulates.

public synchronized void
mark(int
The number of characters before this marked position is invalid.
readlimit
)

Overrides java.io.Reader.mark.

Mark the current position.
public boolean

Returns:

Whether mark is supported or not.
markSupported
()

Overrides java.io.Reader.markSupported.

Indicates whether mark is supported.
protected int

Returns:

This implementation always returns zero.
processChar
()

Return a character value for the read() method.

protected void
processChars(char[]
The character array
chars
,
int
The offset to start at.
offset
,
int
The number of characters.
length
)

Process the characters for the read(char[], offset, length) method.

public int

Returns:

Either The character value returned by processChar() or -1 if the end of file has been reached and throwEofException is set to false.
read
()

Overrides java.io.Reader.read.

Read a character.
public int

Returns:

The number of characters read or -1 if the end of file has been reached and throwEofException is set to false.
read
(char[]
The character array to read into
chars
)

Overrides java.io.Reader.read.

Read some characters into the specified array.
public int

Returns:

The number of characters read or -1 if the end of file has been reached and throwEofException is set to false.
read
(char[]
The character array to read into.
chars
,
int
The offset to start reading characters into.
offset
,
int
The number of characters to read.
length
)

Implements abstract java.io.Reader.read.

Read the specified number characters into an array.
public synchronized void
reset()

Overrides java.io.Reader.reset.

Reset the stream to the point when mark was last called.
public long

Returns:

The number of characters skipped or -1 if the end of file has been reached and throwEofException is set to false.
skip
(long
The number of characters to skip.
numberOfChars
)

Overrides java.io.Reader.skip.

Skip a specified number of characters.
Inherited from java.io.Reader:
nullReaderreadreadytransferTo

Field Detail

eofback to summary
private boolean eof
markback to summary
private long mark
markSupportedback to summary
private boolean markSupported
positionback to summary
private long position
readlimitback to summary
private long readlimit
sizeback to summary
private long size
throwEofExceptionback to summary
private boolean throwEofException

Constructor Detail

NullReaderback to summary
public NullReader(long size)

Create a Reader that emulates a specified size which supports marking and does not throw EOFException.

Parameters
size:long

The size of the reader to emulate.

NullReaderback to summary
public NullReader(long size, boolean markSupported, boolean throwEofException)

Create a Reader that emulates a specified size with option settings.

Parameters
size:long

The size of the reader to emulate.

markSupported:boolean

Whether this instance will support the mark() functionality.

throwEofException:boolean

Whether this implementation will throw an EOFException or return -1 when the end of file is reached.

Method Detail

closeback to summary
public void close() throws IOException

Implements abstract java.io.Reader.close.

Implements java.io.Closeable.close.

Close this Reader - resets the internal state to the initial values.

Exceptions
IOException:
If an error occurs.
doEndOfFileback to summary
private int doEndOfFile() throws EOFException

Handle End of File.

Returns:int

-1 if throwEofException is set to false

Exceptions
EOFException:
if throwEofException is set to true.
getPositionback to summary
public long getPosition()

Return the current position.

Returns:long

the current position.

getSizeback to summary
public long getSize()

Return the size this Reader emulates.

Returns:long

The size of the reader to emulate.

markback to summary
public synchronized void mark(int readlimit)

Overrides java.io.Reader.mark.

Mark the current position.

Parameters
readlimit:int

The number of characters before this marked position is invalid.

Exceptions
UnsupportedOperationException:
if mark is not supported.
markSupportedback to summary
public boolean markSupported()

Overrides java.io.Reader.markSupported.

Indicates whether mark is supported.

Returns:boolean

Whether mark is supported or not.

processCharback to summary
protected int processChar()

Return a character value for the read() method.

This implementation returns zero.

Returns:int

This implementation always returns zero.

processCharsback to summary
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.

Parameters
chars:char[]

The character array

offset:int

The offset to start at.

length:int

The number of characters.

readback to summary
public int read() throws IOException

Overrides java.io.Reader.read.

Read a character.

Returns:int

Either The character value returned by processChar() or -1 if the end of file has been reached and throwEofException is set to false.

Exceptions
IOException:
if trying to read past the end of file.
EOFException:
if the end of file is reached and throwEofException is set to true.
readback to summary
public int read(char[] chars) throws IOException

Overrides java.io.Reader.read.

Read some characters into the specified array.

Parameters
chars:char[]

The character array to read into

Returns:int

The number of characters read or -1 if the end of file has been reached and throwEofException is set to false.

Exceptions
IOException:
if trying to read past the end of file.
EOFException:
if the end of file is reached and throwEofException is set to true.
readback to summary
public int read(char[] chars, int offset, int length) throws IOException

Implements abstract java.io.Reader.read.

Read the specified number characters into an array.

Parameters
chars:char[]

The character array to read into.

offset:int

The offset to start reading characters into.

length:int

The number of characters to read.

Returns:int

The number of characters read or -1 if the end of file has been reached and throwEofException is set to false.

Exceptions
IOException:
if trying to read past the end of file.
EOFException:
if the end of file is reached and throwEofException is set to true.
resetback to summary
public synchronized void reset() throws IOException

Overrides java.io.Reader.reset.

Reset the stream to the point when mark was last called.

Exceptions
IOException:
If no position has been marked or the read limit has been exceed since the last position was marked.
UnsupportedOperationException:
if mark is not supported.
skipback to summary
public long skip(long numberOfChars) throws IOException

Overrides java.io.Reader.skip.

Skip a specified number of characters.

Parameters
numberOfChars:long

The number of characters to skip.

Returns:long

The number of characters skipped or -1 if the end of file has been reached and throwEofException is set to false.

Exceptions
IOException:
if trying to read past the end of file.
EOFException:
if the end of file is reached and throwEofException is set to true.