Package org.apache.commons.io.input
Class NullInputStream
java.lang.Object
java.io.InputStream
org.apache.commons.io.input.NullInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
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:
- 1.3
-
Constructor Summary
ConstructorsConstructorDescriptionCreate anInputStreamthat emulates a size 0 stream which supports marking and does not throw EOFException.NullInputStream(long size) Create anInputStreamthat emulates a specified size which supports marking and does not throw EOFException.NullInputStream(long size, boolean markSupported, boolean throwEofException) Create anInputStreamthat emulates a specified size with option settings. -
Method Summary
Modifier and TypeMethodDescriptionintReturn the number of bytes that can be read.voidclose()Close this input stream - resets the internal state to the initial values.longReturn the current position.longgetSize()Return the size thisInputStreamemulates.voidmark(int readlimit) Mark the current position.booleanIndicates whether mark is supported.protected intReturn a byte value for theread()method.protected voidprocessBytes(byte[] bytes, int offset, int length) Process the bytes for theread(byte[], offset, length)method.intread()Read a byte.intread(byte[] bytes) Read some bytes into the specified array.intread(byte[] bytes, int offset, int length) Read the specified number bytes into an array.voidreset()Reset the stream to the point when mark was last called.longskip(long numberOfBytes) Skip a specified number of bytes.Methods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Constructor Details
-
NullInputStream
public NullInputStream()Create anInputStreamthat emulates a size 0 stream which supports marking and does not throw EOFException.- Since:
- 2.7
-
NullInputStream
Create anInputStreamthat emulates a specified size which supports marking and does not throw EOFException.- Parameters:
size- The size of the input stream to emulate.
-
NullInputStream
Create anInputStreamthat emulates a specified size with option settings.- Parameters:
size- The size of the input stream to emulate.markSupported- Whether this instance will support themark()functionality.throwEofException- Whether this implementation will throw anEOFExceptionor return -1 when the end of file is reached.
-
-
Method Details
-
getPosition
Return the current position.- Returns:
- the current position.
-
getSize
Return the size thisInputStreamemulates.- Returns:
- The size of the input stream to emulate.
-
available
Return the number of bytes that can be read.- Overrides:
availablein classInputStream- Returns:
- The number of bytes that can be read.
-
close
Close this input stream - resets the internal state to the initial values.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException- If an error occurs.
-
mark
Mark the current position.- Overrides:
markin classInputStream- Parameters:
readlimit- The number of bytes before this marked position is invalid.- Throws:
UnsupportedOperationException- if mark is not supported.
-
markSupported
Indicates whether mark is supported.- Overrides:
markSupportedin classInputStream- Returns:
- Whether mark is supported or not.
-
read
Read a byte.- Specified by:
readin classInputStream- Returns:
- Either The byte value returned by
processByte()or-1if the end of file has been reached andthrowEofExceptionis set tofalse. - Throws:
EOFException- if the end of file is reached andthrowEofExceptionis set totrue.IOException- if trying to read past the end of file.
-
read
Read some bytes into the specified array.- Overrides:
readin classInputStream- Parameters:
bytes- The byte array to read into- Returns:
- The number of bytes read or
-1if the end of file has been reached andthrowEofExceptionis set tofalse. - Throws:
EOFException- if the end of file is reached andthrowEofExceptionis set totrue.IOException- if trying to read past the end of file.
-
read
Read the specified number bytes into an array.- Overrides:
readin classInputStream- Parameters:
bytes- The byte array to read into.offset- The offset to start reading bytes into.length- The number of bytes to read.- Returns:
- The number of bytes read or
-1if the end of file has been reached andthrowEofExceptionis set tofalse. - Throws:
EOFException- if the end of file is reached andthrowEofExceptionis set totrue.IOException- if trying to read past the end of file.
-
reset
Reset the stream to the point when mark was last called.- Overrides:
resetin classInputStream- Throws:
UnsupportedOperationException- if mark is not supported.IOException- If no position has been marked or the read limit has been exceed since the last position was marked.
-
skip
Skip a specified number of bytes.- Overrides:
skipin classInputStream- Parameters:
numberOfBytes- The number of bytes to skip.- Returns:
- The number of bytes skipped or
-1if the end of file has been reached andthrowEofExceptionis set tofalse. - Throws:
EOFException- if the end of file is reached andthrowEofExceptionis set totrue.IOException- if trying to read past the end of file.
-
processByte
Return a byte value for theread()method.This implementation returns zero.
- Returns:
- This implementation always returns zero.
-
processBytes
Process the bytes for theread(byte[], offset, length)method.This implementation leaves the byte array unchanged.
- Parameters:
bytes- The byte arrayoffset- The offset to start at.length- The number of bytes.
-