Package org.apache.commons.io.input
Class NullInputStream
java.lang.Object
java.io.InputStream
org.apache.commons.io.input.AbstractInputStream
org.apache.commons.io.input.NullInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
A 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
-
Field Summary
-
Constructor Summary
ConstructorDescriptionConstructs anInputStream
that emulates a size 0 stream which supports marking and does not throw EOFException.NullInputStream
(long size) Constructs anInputStream
that emulates a specified size which supports marking and does not throw EOFException.NullInputStream
(long size, boolean markSupported, boolean throwEofException) Constructs anInputStream
that emulates a specified size with option settings. -
Method Summary
Modifier and TypeMethodDescriptionint
void
close()
Closes this input stream.long
Gets the current position.long
getSize()
Gets the size thisInputStream
emulates.init()
Initializes or re-initializes this instance for reuse.void
mark
(int readLimit) Marks the current position.boolean
Tests whether mark is supported.protected int
Returns a byte value for theread()
method.protected void
processBytes
(byte[] bytes, int offset, int length) Processes the bytes for theread(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.Methods inherited from class org.apache.commons.io.input.AbstractInputStream
isClosed, setClosed
-
Field Details
-
Constructor Details
-
NullInputStream
public NullInputStream()Constructs anInputStream
that emulates a size 0 stream which supports marking and does not throw EOFException.This is an "empty" input stream.
- Since:
- 2.7
-
NullInputStream
Constructs anInputStream
that emulates a specified size which supports marking and does not throw EOFException.- Parameters:
size
- The size of the input stream to emulate.
-
NullInputStream
Constructs anInputStream
that 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 anEOFException
or return -1 when the end of file is reached.
-
-
Method Details
-
available
- Overrides:
available
in classInputStream
-
close
Closes this input stream.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classAbstractInputStream
- Throws:
IOException
- If an error occurs.
-
getPosition
Gets the current position.- Returns:
- the current position.
-
getSize
Gets the size thisInputStream
emulates.- Returns:
- The size of the input stream to emulate.
-
init
Initializes or re-initializes this instance for reuse.- Returns:
- this instance.
- Since:
- 2.17.0
-
mark
Marks the current position.- Overrides:
mark
in classInputStream
- Parameters:
readLimit
- The number of bytes before this marked position is invalid.- Throws:
UnsupportedOperationException
- if mark is not supported.
-
markSupported
Tests whether mark is supported.- Overrides:
markSupported
in classInputStream
- Returns:
- Whether mark is supported or not.
-
processByte
Returns a byte value for theread()
method.This implementation returns zero.
- Returns:
- This implementation always returns zero.
-
processBytes
Processes 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.
-
read
Reads a byte.- Specified by:
read
in classInputStream
- Returns:
- Either The byte value returned by
processByte()
or-1
if the end of file has been reached andthrowEofException
is set tofalse
. - Throws:
EOFException
- if the end of file is reached andthrowEofException
is set totrue
.IOException
- if trying to read past the end of file.
-
read
Reads some bytes into the specified array.- Overrides:
read
in classInputStream
- Parameters:
bytes
- The byte array to read into- Returns:
- The number of bytes read or
-1
if the end of file has been reached andthrowEofException
is set tofalse
. - Throws:
EOFException
- if the end of file is reached andthrowEofException
is set totrue
.IOException
- if trying to read past the end of file.
-
read
Reads the specified number bytes into an array.- Overrides:
read
in 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
-1
if the end of file has been reached andthrowEofException
is set tofalse
. - Throws:
EOFException
- if the end of file is reached andthrowEofException
is set totrue
.IOException
- if trying to read past the end of file.
-
reset
Resets the stream to the point when mark was last called.- Overrides:
reset
in classInputStream
- Throws:
UnsupportedOperationException
- if mark is not supported.IOException
- If no position has been marked or the read limit has been exceeded since the last position was marked.
-
skip
Skips a specified number of bytes.- Overrides:
skip
in classInputStream
- Parameters:
numberOfBytes
- The number of bytes to skip.- Returns:
- The number of bytes skipped or
-1
if the end of file has been reached andthrowEofException
is set tofalse
. - Throws:
EOFException
- if the end of file is reached andthrowEofException
is set totrue
.IOException
- if trying to read past the end of file.
-
init()
to reset state.