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
}
}
}
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| private long | |
| private boolean | |
| private long | |
| private long | |
| private long | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public | NullInputStream(long
The size of the input stream to emulate. size)Create an |
| public | NullInputStream(long
The size of the input stream to emulate. size, boolean Whether this instance will support
the markSupported, boolean mark() functionality.Whether this implementation
will throw an throwEofException)EOFException or return -1 when the
end of file is reached.Create an |
| Modifier and Type | Method and Description |
|---|---|
| public int | Returns: The number of bytes that can be read.Overrides java. |
| public void | close()
Overrides java. Implements java. |
| private int | |
| public long | |
| public long | Returns: The size of the input stream to emulate.Return the size this |
| public synchronized void | mark(int
The number of bytes before this marked position
is invalid. readlimit)Overrides java. |
| public boolean | Returns: Whether mark is supported or not.Overrides java. |
| protected int | Returns: This implementation always returns zero.Return a byte value for the |
| 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 |
| public int | Returns: Either The byte value returned byprocessByte()
or -1 if the end of file has been reached and
throwEofException is set to false.Implements abstract java. |
| public int | Returns: The number of bytes read or-1
if the end of file has been reached and
throwEofException is set to false.The byte array to read into bytes)Overrides java. |
| public int | Returns: The number of bytes read or-1
if the end of file has been reached and
throwEofException is set to false.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. |
| public synchronized void | reset()
Overrides java. |
| public long | Returns: The number of bytes skipped or-1
if the end of file has been reached and
throwEofException is set to false.The number of bytes to skip. numberOfBytes)Overrides java. |
| eof | back to summary |
|---|---|
| private boolean eof | |
| mark | back to summary |
|---|---|
| private long mark | |
| markSupported | back to summary |
|---|---|
| private boolean markSupported | |
| position | back to summary |
|---|---|
| private long position | |
| readlimit | back to summary |
|---|---|
| private long readlimit | |
| size | back to summary |
|---|---|
| private long size | |
| throwEofException | back to summary |
|---|---|
| private boolean throwEofException | |
| NullInputStream | back to summary |
|---|---|
| public NullInputStream(long size) Create an
| |
| NullInputStream | back to summary |
|---|---|
| public NullInputStream(long size, boolean markSupported, boolean throwEofException) Create an
| |
| available | back to summary |
|---|---|
| public int available() Overrides java. Return the number of bytes that can be read.
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Overrides java. Implements java. Close this input stream - resets the internal state to the initial values.
| |
| doEndOfFile | back to summary |
|---|---|
| private int doEndOfFile() throws EOFException Handle End of File.
| |
| getPosition | back to summary |
|---|---|
| public long getPosition() Return the current position.
| |
| getSize | back to summary |
|---|---|
| public long getSize() Return the size this
| |
| mark | back to summary |
|---|---|
| public synchronized void mark(int readlimit) Overrides java. Mark the current position.
| |
| markSupported | back to summary |
|---|---|
| public boolean markSupported() Overrides java. Indicates whether mark is supported.
| |
| processByte | back to summary |
|---|---|
| protected int processByte() Return a byte value for the This implementation returns zero.
| |
| processBytes | back to summary |
|---|---|
| protected void processBytes(byte[] bytes, int offset, int length) Process the bytes for the This implementation leaves the byte array unchanged.
| |
| read | back to summary |
|---|---|
| public int read() throws IOException Implements abstract java. Read a byte.
| |
| read | back to summary |
|---|---|
| public int read(byte[] bytes) throws IOException Overrides java. Read some bytes into the specified array.
| |
| read | back to summary |
|---|---|
| public int read(byte[] bytes, int offset, int length) throws IOException Overrides java. Read the specified number bytes into an array.
| |
| reset | back to summary |
|---|---|
| public synchronized void reset() throws IOException Overrides java. Reset the stream to the point when mark was last called.
| |
| skip | back to summary |
|---|---|
| public long skip(long numberOfBytes) throws IOException Overrides java. Skip a specified number of bytes.
| |