public class NullInputStream
extends java.io.InputStream
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 |
eof |
static NullInputStream |
INSTANCE
The singleton instance.
|
private long |
mark |
private boolean |
markSupported |
private long |
position |
private long |
readLimit |
private long |
size |
private boolean |
throwEofException |
| Constructor and Description |
|---|
NullInputStream()
Constructs an
InputStream that emulates a size 0 stream which supports marking and does not throw EOFException. |
NullInputStream(long size)
Constructs an
InputStream that emulates a specified size which supports marking and does not throw EOFException. |
NullInputStream(long size,
boolean markSupported,
boolean throwEofException)
Constructs an
InputStream that emulates a specified size with option settings. |
| Modifier and Type | Method and Description |
|---|---|
int |
available()
Returns the number of bytes that can be read.
|
private void |
checkThrowEof(java.lang.String message)
Throws
EOFException if throwEofException is enabled. |
void |
close()
Closes this input stream - resets the internal state to the initial values.
|
long |
getPosition()
Gets the current position.
|
long |
getSize()
Gets the size this
InputStream emulates. |
private int |
handleEof()
Handles End of File.
|
void |
mark(int readLimit)
Marks the current position.
|
boolean |
markSupported()
Tests whether mark is supported.
|
protected int |
processByte()
Returns a byte value for the
read() method. |
protected void |
processBytes(byte[] bytes,
int offset,
int length)
Processes the bytes for the
read(byte[], offset, length) method. |
int |
read()
Reads a byte.
|
int |
read(byte[] bytes)
Reads some bytes into the specified array.
|
int |
read(byte[] bytes,
int offset,
int length)
Reads the specified number bytes into an array.
|
void |
reset()
Resets the stream to the point when mark was last called.
|
long |
skip(long numberOfBytes)
Skips a specified number of bytes.
|
public static final NullInputStream INSTANCE
private final long size
private long position
private long mark
private long readLimit
private boolean eof
private final boolean throwEofException
private final boolean markSupported
public NullInputStream()
InputStream that emulates a size 0 stream which supports marking and does not throw EOFException.public NullInputStream(long size)
InputStream that emulates a specified size which supports marking and does not throw EOFException.size - The size of the input stream to emulate.public NullInputStream(long size,
boolean markSupported,
boolean throwEofException)
InputStream that emulates a specified size with option settings.size - The size of the input stream to emulate.markSupported - Whether this instance will support the mark() functionality.throwEofException - Whether this implementation will throw an EOFException or return -1 when the end of file is reached.public int available()
available in class java.io.InputStreamprivate void checkThrowEof(java.lang.String message)
throws java.io.EOFException
EOFException if throwEofException is enabled.message - The EOFException message.java.io.EOFException - Thrown if throwEofException is enabled.public void close()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseableclose in class java.io.InputStreamjava.io.IOException - If an error occurs.public long getPosition()
public long getSize()
InputStream emulates.private int handleEof()
throws java.io.EOFException
-1 if throwEofException is set to falsejava.io.EOFException - if throwEofException is set to true.public void mark(int readLimit)
mark in class java.io.InputStreamreadLimit - The number of bytes before this marked position is invalid.java.lang.UnsupportedOperationException - if mark is not supported.public boolean markSupported()
markSupported in class java.io.InputStreamprotected int processByte()
read() method.
This implementation returns zero.
protected void processBytes(byte[] bytes,
int offset,
int length)
read(byte[], offset, length) method.
This implementation leaves the byte array unchanged.
bytes - The byte arrayoffset - The offset to start at.length - The number of bytes.public int read()
throws java.io.IOException
read in class java.io.InputStreamprocessByte() or -1 if the end of file has been reached and throwEofException is set to
false.java.io.EOFException - if the end of file is reached and throwEofException is set to true.java.io.IOException - if trying to read past the end of file.public int read(byte[] bytes)
throws java.io.IOException
read in class java.io.InputStreambytes - The byte array to read into-1 if the end of file has been reached and throwEofException is set to false.java.io.EOFException - if the end of file is reached and throwEofException is set to true.java.io.IOException - if trying to read past the end of file.public int read(byte[] bytes,
int offset,
int length)
throws java.io.IOException
read in class java.io.InputStreambytes - The byte array to read into.offset - The offset to start reading bytes into.length - The number of bytes to read.-1 if the end of file has been reached and throwEofException is set to false.java.io.EOFException - if the end of file is reached and throwEofException is set to true.java.io.IOException - if trying to read past the end of file.public void reset()
throws java.io.IOException
reset in class java.io.InputStreamjava.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.public long skip(long numberOfBytes)
throws java.io.IOException
skip in class java.io.InputStreamnumberOfBytes - The number of bytes to skip.-1 if the end of file has been reached and throwEofException is set to false.java.io.EOFException - if the end of file is reached and throwEofException is set to true.java.io.IOException - if trying to read past the end of file.