FileDocCategorySizeDatePackage
NullReader.javaAPI DocAndroid 1.5 API10535Wed May 06 22:42:46 BST 2009org.apache.commons.io.input

NullReader

public class NullReader extends Reader
A functional, light weight {@link Reader} that emulates a reader of a specified size.

This implementation provides a light weight object for testing with an {@link Reader} where the contents don't matter.

One use case would be for testing the handling of large {@link 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 $

Fields Summary
private long
size
private long
position
private long
mark
private long
readlimit
private boolean
eof
private boolean
throwEofException
private boolean
markSupported
Constructors Summary
public NullReader(long size)
Create a {@link Reader} that emulates a specified size which supports marking and does not throw EOFException.

param
size The size of the reader to emulate.


                                   
       
       this(size, true, false);
    
public NullReader(long size, boolean markSupported, boolean throwEofException)
Create a {@link Reader} that emulates a specified size with option settings.

param
size The size of the reader to emulate.
param
markSupported Whether this instance will support the mark() functionality.
param
throwEofException Whether this implementation will throw an {@link EOFException} or return -1 when the end of file is reached.

       this.size = size;
       this.markSupported = markSupported;
       this.throwEofException = throwEofException;
    
Methods Summary
public voidclose()
Close this Reader - resets the internal state to the initial values.

throws
IOException If an error occurs.

        eof = false;
        position = 0;
        mark = -1;
    
private intdoEndOfFile()
Handle End of File.

return
-1 if throwEofException is set to false
throws
EOFException if throwEofException is set to true.

        eof = true;
        if (throwEofException) {
            throw new EOFException();
        }
        return -1;
    
public longgetPosition()
Return the current position.

return
the current position.

        return position;
    
public longgetSize()
Return the size this {@link Reader} emulates.

return
The size of the reader to emulate.

        return size;
    
public synchronized voidmark(int readlimit)
Mark the current position.

param
readlimit The number of characters before this marked position is invalid.
throws
UnsupportedOperationException if mark is not supported.

        if (!markSupported) {
            throw new UnsupportedOperationException("Mark not supported");
        }
        mark = position;
        this.readlimit = readlimit;
    
public booleanmarkSupported()
Indicates whether mark is supported.

return
Whether mark is supported or not.

        return markSupported;
    
protected intprocessChar()
Return a character value for the read() method.

This implementation returns zero.

return
This implementation always returns zero.

        // do nothing - overridable by subclass
        return 0;
    
protected voidprocessChars(char[] chars, int offset, int length)
Process the characters for the read(char[], offset, length) method.

This implementation leaves the character array unchanged.

param
chars The character array
param
offset The offset to start at.
param
length The number of characters.

        // do nothing - overridable by subclass
    
public intread(char[] chars, int offset, int length)
Read the specified number characters into an array.

param
chars The character array to read into.
param
offset The offset to start reading characters into.
param
length The number of characters to read.
return
The number of characters read or -1 if the end of file has been reached and throwEofException is set to false.
throws
EOFException if the end of file is reached and throwEofException is set to true.
throws
IOException if trying to read past the end of file.

        if (eof) {
            throw new IOException("Read after end of file");
        }
        if (position == size) {
            return doEndOfFile();
        }
        position += length;
        int returnLength = length;
        if (position > size) {
            returnLength = length - (int)(position - size);
            position = size;
        }
        processChars(chars, offset, returnLength);
        return returnLength;
    
public intread()
Read a character.

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

        if (eof) {
            throw new IOException("Read after end of file");
        }
        if (position == size) {
            return doEndOfFile();
        }
        position++;
        return processChar();
    
public intread(char[] chars)
Read some characters into the specified array.

param
chars The character array to read into
return
The number of characters read or -1 if the end of file has been reached and throwEofException is set to false.
throws
EOFException if the end of file is reached and throwEofException is set to true.
throws
IOException if trying to read past the end of file.

        return read(chars, 0, chars.length);
    
public synchronized voidreset()
Reset the stream to the point when mark was last called.

throws
UnsupportedOperationException if mark is not supported.
throws
IOException If no position has been marked or the read limit has been exceed since the last position was marked.

        if (!markSupported) {
            throw new UnsupportedOperationException("Mark not supported");
        }
        if (mark < 0) {
            throw new IOException("No position has been marked");
        }
        if (position > (mark + readlimit)) {
            throw new IOException("Marked position [" + mark +
                    "] is no longer valid - passed the read limit [" +
                    readlimit + "]");
        }
        position = mark;
        eof = false;
    
public longskip(long numberOfChars)
Skip a specified number of characters.

param
numberOfChars The number of characters to skip.
return
The number of characters skipped or -1 if the end of file has been reached and throwEofException is set to false.
throws
EOFException if the end of file is reached and throwEofException is set to true.
throws
IOException if trying to read past the end of file.

        if (eof) {
            throw new IOException("Skip after end of file");
        }
        if (position == size) {
            return doEndOfFile();
        }
        position += numberOfChars;
        long returnLength = numberOfChars;
        if (position > size) {
            returnLength = numberOfChars - (position - size);
            position = size;
        }
        return returnLength;