Package org.apache.commons.io.input
Class NullInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.apache.commons.io.input.NullInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class NullInputStream extends java.io.InputStreamA functional, light weightInputStreamthat emulates a stream of a specified size.This implementation provides a light weight object for testing with an
InputStreamwhere the contents don't matter.One use case would be for testing the handling of large
InputStreamas 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()andprocessBytes()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
Fields Modifier and Type Field Description static NullInputStreamINSTANCEThe singleton instance.
-
Constructor Summary
Constructors Constructor Description NullInputStream()Constructs anInputStreamthat emulates a size 0 stream which supports marking and does not throw EOFException.NullInputStream(long size)Constructs anInputStreamthat emulates a specified size which supports marking and does not throw EOFException.NullInputStream(long size, boolean markSupported, boolean throwEofException)Constructs anInputStreamthat emulates a specified size with option settings.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Returns the number of bytes that can be read.voidclose()Closes this input stream - resets the internal state to the initial values.longgetPosition()Gets the current position.longgetSize()Gets the size thisInputStreamemulates.voidmark(int readLimit)Marks the current position.booleanmarkSupported()Tests whether mark is supported.intread()Reads a byte.intread(byte[] bytes)Reads some bytes into the specified array.intread(byte[] bytes, int offset, int length)Reads the specified number bytes into an array.voidreset()Resets the stream to the point when mark was last called.longskip(long numberOfBytes)Skips a specified number of bytes.
-
-
-
Field Detail
-
INSTANCE
public static final NullInputStream INSTANCE
The singleton instance.- Since:
- 2.12.0
-
-
Constructor Detail
-
NullInputStream
public NullInputStream()
Constructs anInputStreamthat emulates a size 0 stream which supports marking and does not throw EOFException.- Since:
- 2.7
-
NullInputStream
public NullInputStream(long size)
Constructs anInputStreamthat emulates a specified size which supports marking and does not throw EOFException.- Parameters:
size- The size of the input stream to emulate.
-
NullInputStream
public NullInputStream(long size, boolean markSupported, boolean throwEofException)Constructs 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 Detail
-
available
public int available()
Returns the number of bytes that can be read.- Overrides:
availablein classjava.io.InputStream- Returns:
- The number of bytes that can be read.
-
close
public void close() throws java.io.IOExceptionCloses this input stream - resets the internal state to the initial values.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException- If an error occurs.
-
getPosition
public long getPosition()
Gets the current position.- Returns:
- the current position.
-
getSize
public long getSize()
Gets the size thisInputStreamemulates.- Returns:
- The size of the input stream to emulate.
-
mark
public void mark(int readLimit)
Marks the current position.- Overrides:
markin classjava.io.InputStream- Parameters:
readLimit- The number of bytes before this marked position is invalid.- Throws:
java.lang.UnsupportedOperationException- if mark is not supported.
-
markSupported
public boolean markSupported()
Tests whether mark is supported.- Overrides:
markSupportedin classjava.io.InputStream- Returns:
- Whether mark is supported or not.
-
read
public int read() throws java.io.IOExceptionReads a byte.- Specified by:
readin classjava.io.InputStream- Returns:
- Either The byte value returned by
processByte()or-1if the end of file has been reached andthrowEofExceptionis set tofalse. - Throws:
java.io.EOFException- if the end of file is reached andthrowEofExceptionis set totrue.java.io.IOException- if trying to read past the end of file.
-
read
public int read(byte[] bytes) throws java.io.IOExceptionReads some bytes into the specified array.- Overrides:
readin classjava.io.InputStream- 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:
java.io.EOFException- if the end of file is reached andthrowEofExceptionis set totrue.java.io.IOException- if trying to read past the end of file.
-
read
public int read(byte[] bytes, int offset, int length) throws java.io.IOExceptionReads the specified number bytes into an array.- Overrides:
readin classjava.io.InputStream- 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:
java.io.EOFException- if the end of file is reached andthrowEofExceptionis set totrue.java.io.IOException- if trying to read past the end of file.
-
reset
public void reset() throws java.io.IOExceptionResets the stream to the point when mark was last called.- Overrides:
resetin classjava.io.InputStream- Throws:
java.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.
-
skip
public long skip(long numberOfBytes) throws java.io.IOExceptionSkips a specified number of bytes.- Overrides:
skipin classjava.io.InputStream- 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:
java.io.EOFException- if the end of file is reached andthrowEofExceptionis set totrue.java.io.IOException- if trying to read past the end of file.
-
-