Class BoundedInputStream

java.lang.Object
java.io.InputStream
java.io.FilterInputStream
org.apache.commons.io.input.BoundedInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

Reads bytes up to a maximum length, if its count goes above that, it stops.

This is useful to wrap ServletInputStreams. The ServletInputStream will block if you try to read content from it that isn't there, because it doesn't know whether the content hasn't arrived yet or whether the content has finished. So, one of these, initialized with the Content-length sent in the ServletInputStream's header, will stop it blocking, providing it's been sent with a correct content length.

Since:
2.0
  • Constructor Details

    • BoundedInputStream

      Constructs a new BoundedInputStream that wraps the given input stream and is unlimited.
      Parameters:
      in - The wrapped input stream.
    • BoundedInputStream

      public BoundedInputStream(InputStream inputStream, long maxLength)
      Constructs a new BoundedInputStream that wraps the given input stream and limits it to a certain size.
      Parameters:
      inputStream - The wrapped input stream.
      maxLength - The maximum number of bytes to return.
  • Method Details

    • available

      public int available() throws IOException
      Overrides:
      available in class FilterInputStream
      Throws:
      IOException
    • close

      public void close() throws IOException
      Invokes the delegate's close() method if isPropagateClose() is true.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterInputStream
      Throws:
      IOException - if an I/O error occurs.
    • getCount

      public long getCount()
      Gets the count of bytes read.
      Returns:
      The count of bytes read.
      Since:
      2.12.0
    • getMaxLength

      public long getMaxLength()
      Gets the max count of bytes to read.
      Returns:
      The max count of bytes to read.
      Since:
      2.12.0
    • isPropagateClose

      public boolean isPropagateClose()
      Tests whether the close() method should propagate to the underling InputStream.
      Returns:
      true if calling close() propagates to the close() method of the underlying stream or false if it does not.
    • mark

      public void mark(int readLimit)
      Invokes the delegate's mark(int) method.
      Overrides:
      mark in class FilterInputStream
      Parameters:
      readLimit - read ahead limit
    • markSupported

      public boolean markSupported()
      Invokes the delegate's markSupported() method.
      Overrides:
      markSupported in class FilterInputStream
      Returns:
      true if mark is supported, otherwise false
    • onMaxLength

      protected void onMaxLength(long maxLength, long count) throws IOException
      A caller has caused a request that would cross the maxLength boundary.
      Parameters:
      maxLength - The max count of bytes to read.
      count - The count of bytes read.
      Throws:
      IOException - Subclasses may throw.
      Since:
      2.12.0
    • read

      public int read() throws IOException
      Invokes the delegate's read() method if the current position is less than the limit.
      Overrides:
      read in class FilterInputStream
      Returns:
      the byte read or -1 if the end of stream or the limit has been reached.
      Throws:
      IOException - if an I/O error occurs.
    • read

      public int read(byte[] b) throws IOException
      Invokes the delegate's read(byte[]) method.
      Overrides:
      read in class FilterInputStream
      Parameters:
      b - the buffer to read the bytes into
      Returns:
      the number of bytes read or -1 if the end of stream or the limit has been reached.
      Throws:
      IOException - if an I/O error occurs.
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Invokes the delegate's read(byte[], int, int) method.
      Overrides:
      read in class FilterInputStream
      Parameters:
      b - the buffer to read the bytes into
      off - The start offset
      len - The number of bytes to read
      Returns:
      the number of bytes read or -1 if the end of stream or the limit has been reached.
      Throws:
      IOException - if an I/O error occurs.
    • reset

      public void reset() throws IOException
      Invokes the delegate's reset() method.
      Overrides:
      reset in class FilterInputStream
      Throws:
      IOException - if an I/O error occurs.
    • setPropagateClose

      public void setPropagateClose(boolean propagateClose)
      Sets whether the close() method should propagate to the underling InputStream.
      Parameters:
      propagateClose - true if calling close() propagates to the close() method of the underlying stream or false if it does not.
    • skip

      public long skip(long n) throws IOException
      Invokes the delegate's skip(long) method.
      Overrides:
      skip in class FilterInputStream
      Parameters:
      n - the number of bytes to skip
      Returns:
      the actual number of bytes skipped
      Throws:
      IOException - if an I/O error occurs.
    • toString

      public String toString()
      Invokes the delegate's toString() method.
      Overrides:
      toString in class Object
      Returns:
      the delegate's toString()