Class NullInputStream
- All Implemented Interfaces:
- Closeable,- AutoCloseable
InputStream that emulates a stream of a specified size.
 
 This implementation provides a lightweight 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
          }
      }
  }
 
 This class is not thread-safe.
- Since:
- 1.3
- 
Field SummaryFields
- 
Constructor SummaryConstructorsConstructorDescriptionConstructs 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 SummaryModifier and TypeMethodDescriptionintvoidclose()Closes this input stream.longGets the current position.longgetSize()Gets the size thisInputStreamemulates.init()Initializes or re-initializes this instance for reuse.voidmark(int readLimit) Marks the current position.booleanTests whether mark is supported.protected intReturns a byte value for theread()method.protected voidprocessBytes(byte[] bytes, int offset, int length) Processes the bytes for theread(byte[], offset, length)method.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.Methods inherited from class org.apache.commons.io.input.AbstractInputStreamisClosed, setClosed
- 
Field Details
- 
Constructor Details- 
NullInputStreampublic NullInputStream()Constructs anInputStreamthat emulates a size 0 stream which supports marking and does not throw EOFException.This is an "empty" input stream. - Since:
- 2.7
 
- 
NullInputStreamConstructs anInputStreamthat emulates a specified size which supports marking and does not throw EOFException.- Parameters:
- size- The size of the input stream to emulate.
 
- 
NullInputStreamConstructs anInputStreamthat emulates a specified size with option settings.- Parameters:
- 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- EOFExceptionor return -1 when the end of file is reached.
 
 
- 
- 
Method Details- 
available- Overrides:
- availablein class- InputStream
 
- 
closeCloses this input stream.- Specified by:
- closein interface- AutoCloseable
- Specified by:
- closein interface- Closeable
- Overrides:
- closein class- AbstractInputStream
- Throws:
- IOException- If an error occurs.
 
- 
getPositionGets the current position.- Returns:
- the current position.
 
- 
getSizeGets the size thisInputStreamemulates.- Returns:
- The size of the input stream to emulate.
 
- 
initInitializes or re-initializes this instance for reuse.- Returns:
- this instance.
- Since:
- 2.17.0
 
- 
markMarks the current position.- Overrides:
- markin class- InputStream
- Parameters:
- readLimit- The number of bytes before this marked position is invalid.
- Throws:
- UnsupportedOperationException- if mark is not supported.
 
- 
markSupportedTests whether mark is supported.- Overrides:
- markSupportedin class- InputStream
- Returns:
- Whether mark is supported or not.
 
- 
processByteReturns a byte value for theread()method.This implementation returns zero. - Returns:
- This implementation always returns zero.
 
- 
processBytesProcesses the bytes for theread(byte[], offset, length)method.This implementation leaves the byte array unchanged. - Parameters:
- bytes- The byte array
- offset- The offset to start at.
- length- The number of bytes.
 
- 
readReads a byte.- Specified by:
- readin class- InputStream
- 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 and- throwEofExceptionis set to- true.
- IOException- if trying to read past the end of file.
 
- 
readReads some bytes into the specified array.- Overrides:
- readin class- 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:
- EOFException- if the end of file is reached and- throwEofExceptionis set to- true.
- IOException- if trying to read past the end of file.
 
- 
readReads the specified number bytes into an array.- Overrides:
- readin class- 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:
- EOFException- if the end of file is reached and- throwEofExceptionis set to- true.
- IOException- if trying to read past the end of file.
 
- 
resetResets the stream to the point when mark was last called.- Overrides:
- resetin class- InputStream
- 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.
 
- 
skipSkips a specified number of bytes.- Overrides:
- skipin class- 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:
- EOFException- if the end of file is reached and- throwEofExceptionis set to- true.
- IOException- if trying to read past the end of file.
 
 
- 
init()to reset state.