public class NullInputStream extends 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 } } }
Constructor and Description |
---|
NullInputStream(long size)
Create an
InputStream that emulates a specified size
which supports marking and does not throw EOFException. |
NullInputStream(long size,
boolean markSupported,
boolean throwEofException)
Create an
InputStream that emulates a specified
size with option settings. |
Modifier and Type | Method and Description |
---|---|
int |
available()
Return the number of bytes that can be read.
|
void |
close()
Close this input stream - resets the internal state to
the initial values.
|
long |
getPosition()
Return the current position.
|
long |
getSize()
Return the size this
InputStream emulates. |
void |
mark(int readlimit)
Mark the current position.
|
boolean |
markSupported()
Indicates whether mark is supported.
|
protected int |
processByte()
Return a byte value for the
read() method. |
protected void |
processBytes(byte[] bytes,
int offset,
int length)
Process the bytes for the
read(byte[], offset, length)
method. |
int |
read()
Read a byte.
|
int |
read(byte[] bytes)
Read some bytes into the specified array.
|
int |
read(byte[] bytes,
int offset,
int length)
Read the specified number bytes into an array.
|
void |
reset()
Reset the stream to the point when mark was last called.
|
long |
skip(long numberOfBytes)
Skip a specified number of bytes.
|
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 long getPosition()
public long getSize()
InputStream
emulates.public int available()
available
in class InputStream
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
close
in class InputStream
IOException
- If an error occurs.public void mark(int readlimit)
mark
in class InputStream
readlimit
- The number of bytes before this marked position
is invalid.UnsupportedOperationException
- if mark is not supported.public boolean markSupported()
markSupported
in class InputStream
public int read() throws IOException
read
in class InputStream
processByte()
or -1
if the end of file has been reached and
throwEofException
is set to false
.EOFException
- if the end of file is reached and
throwEofException
is set to true
.IOException
- if trying to read past the end of file.public int read(byte[] bytes) throws IOException
read
in class InputStream
bytes
- The byte array to read into-1
if the end of file has been reached and
throwEofException
is set to false
.EOFException
- if the end of file is reached and
throwEofException
is set to true
.IOException
- if trying to read past the end of file.public int read(byte[] bytes, int offset, int length) throws IOException
read
in class InputStream
bytes
- 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
.EOFException
- if the end of file is reached and
throwEofException
is set to true
.IOException
- if trying to read past the end of file.public void reset() throws IOException
reset
in class InputStream
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.public long skip(long numberOfBytes) throws IOException
skip
in class InputStream
numberOfBytes
- The number of bytes to skip.-1
if the end of file has been reached and
throwEofException
is set to false
.EOFException
- if the end of file is reached and
throwEofException
is set to true
.IOException
- if trying to read past the end of file.protected 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.Copyright © 2002–2016 The Apache Software Foundation. All rights reserved.