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

public Class NullInputStream

extends InputStream
Class Inheritance
Imports
java.io.EOFException, .IOException, .InputStream

A functional, light weight InputStream that emulates a stream of a specified size.

This implementation provides a light weight object for testing with an InputStream where the contents don't matter.

One use case would be for testing the handling of large InputStream as it can emulate that scenario without the overhead of actually processing large numbers of bytes - significantly speeding up test execution times.

This implementation returns zero from the method that reads a byte and leaves the array unchanged in the read methods that are passed a byte array. If alternative data is required the processByte() and processBytes() methods can be implemented to generate data, for example:

 public class TestInputStream extends NullInputStream {
     public TestInputStream(int size) {
         super(size);
     }
     protected int processByte() {
         return ... // return required value here
     }
     protected void processBytes(byte[] bytes, int offset, int length) {
         for (int i = offset; i < length; i++) {
             bytes[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

Constructor Summary

AccessConstructor and Description
public
NullInputStream(long
The size of the input stream to emulate.
size
)

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

public
NullInputStream(long
The size of the input stream 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 an InputStream that emulates a specified size with option settings.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

The number of bytes that can be read.
available
()

Overrides java.io.InputStream.available.

Return the number of bytes that can be read.
public void
close()

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Close this input stream - 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 input stream to emulate.
getSize
()

Return the size this InputStream emulates.

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

Overrides java.io.InputStream.mark.

Mark the current position.
public boolean

Returns:

Whether mark is supported or not.
markSupported
()

Overrides java.io.InputStream.markSupported.

Indicates whether mark is supported.
protected int

Returns:

This implementation always returns zero.
processByte
()

Return a byte value for the read() method.

protected void
processBytes(byte[]
The byte array
bytes
,
int
The offset to start at.
offset
,
int
The number of bytes.
length
)

Process the bytes for the read(byte[], offset, length) method.

public int

Returns:

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

Implements abstract java.io.InputStream.read.

Read a byte.
public int

Returns:

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

Overrides java.io.InputStream.read.

Read some bytes into the specified array.
public int

Returns:

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

Overrides java.io.InputStream.read.

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

Overrides java.io.InputStream.reset.

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

Returns:

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

Overrides java.io.InputStream.skip.

Skip a specified number of bytes.
Inherited from java.io.InputStream:
nullInputStreamreadAllBytesreadNBytesreadNBytesskipNBytestransferTo

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

NullInputStreamback to summary
public NullInputStream(long size)

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

Parameters
size:long

The size of the input stream to emulate.

NullInputStreamback to summary
public NullInputStream(long size, boolean markSupported, boolean throwEofException)

Create an InputStream that emulates a specified size with option settings.

Parameters
size:long

The size of the input stream 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

availableback to summary
public int available()

Overrides java.io.InputStream.available.

Return the number of bytes that can be read.

Returns:int

The number of bytes that can be read.

closeback to summary
public void close() throws IOException

Overrides java.io.InputStream.close.

Implements java.io.Closeable.close.

Close this input stream - 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 InputStream emulates.

Returns:long

The size of the input stream to emulate.

markback to summary
public synchronized void mark(int readlimit)

Overrides java.io.InputStream.mark.

Mark the current position.

Parameters
readlimit:int

The number of bytes before this marked position is invalid.

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

Overrides java.io.InputStream.markSupported.

Indicates whether mark is supported.

Returns:boolean

Whether mark is supported or not.

processByteback to summary
protected int processByte()

Return a byte value for the read() method.

This implementation returns zero.

Returns:int

This implementation always returns zero.

processBytesback to summary
protected void processBytes(byte[] bytes, int offset, int length)

Process the bytes for the read(byte[], offset, length) method.

This implementation leaves the byte array unchanged.

Parameters
bytes:byte[]

The byte array

offset:int

The offset to start at.

length:int

The number of bytes.

readback to summary
public int read() throws IOException

Implements abstract java.io.InputStream.read.

Read a byte.

Returns:int

Either The byte value returned by processByte() 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(byte[] bytes) throws IOException

Overrides java.io.InputStream.read.

Read some bytes into the specified array.

Parameters
bytes:byte[]

The byte array to read into

Returns:int

The number of bytes 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(byte[] bytes, int offset, int length) throws IOException

Overrides java.io.InputStream.read.

Read the specified number bytes into an array.

Parameters
bytes:byte[]

The byte array to read into.

offset:int

The offset to start reading bytes into.

length:int

The number of bytes to read.

Returns:int

The number of bytes 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.InputStream.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 numberOfBytes) throws IOException

Overrides java.io.InputStream.skip.

Skip a specified number of bytes.

Parameters
numberOfBytes:long

The number of bytes to skip.

Returns:long

The number of bytes 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.