Class LimitedInputStream

java.lang.Object
java.io.InputStream
java.io.FilterInputStream
org.apache.commons.fileupload.util.LimitedInputStream
All Implemented Interfaces:
Closeable, AutoCloseable, Closeable

public abstract class LimitedInputStream extends FilterInputStream implements Closeable
An input stream, which limits its data size. This stream is used, if the content length is unknown.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private boolean
    Whether this stream is already closed.
    private long
    The current number of bytes.
    private final long
    The maximum size of an item, in bytes.

    Fields inherited from class java.io.FilterInputStream

    in
  • Constructor Summary

    Constructors
    Constructor
    Description
    LimitedInputStream(InputStream inputStream, long sizeMax)
    Creates a new instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    Called to check, whether the input streams limit is reached.
    void
    Closes this input stream and releases any system resources associated with the stream.
    boolean
    Returns, whether this stream is already closed.
    protected abstract void
    raiseError(long sizeMax, long count)
    Called to indicate, that the input streams limit has been exceeded.
    int
    Reads the next byte of data from this input stream.
    int
    read(byte[] b, int off, int len)
    Reads up to len bytes of data from this input stream into an array of bytes.

    Methods inherited from class java.io.FilterInputStream

    available, mark, markSupported, read, reset, skip

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • sizeMax

      private final long sizeMax
      The maximum size of an item, in bytes.
    • count

      private long count
      The current number of bytes.
    • closed

      private boolean closed
      Whether this stream is already closed.
  • Constructor Details

    • LimitedInputStream

      public LimitedInputStream(InputStream inputStream, long sizeMax)
      Creates a new instance.
      Parameters:
      inputStream - The input stream, which shall be limited.
      sizeMax - The limit; no more than this number of bytes shall be returned by the source stream.
  • Method Details

    • checkLimit

      private void checkLimit() throws IOException
      Called to check, whether the input streams limit is reached.
      Throws:
      IOException - The given limit is exceeded.
    • close

      public void close() throws IOException
      Closes this input stream and releases any system resources associated with the stream. This method simply performs in.close().
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterInputStream
      Throws:
      IOException - if an I/O error occurs.
      See Also:
    • isClosed

      public boolean isClosed() throws IOException
      Returns, whether this stream is already closed.
      Specified by:
      isClosed in interface Closeable
      Returns:
      True, if the stream is closed, otherwise false.
      Throws:
      IOException - An I/O error occurred.
    • raiseError

      protected abstract void raiseError(long sizeMax, long count) throws IOException
      Called to indicate, that the input streams limit has been exceeded.
      Parameters:
      sizeMax - The input streams limit, in bytes.
      count - The actual number of bytes.
      Throws:
      IOException - The called method is expected to raise an IOException.
    • read

      public int read() throws IOException
      Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

      This method simply performs in.read() and returns the result.

      Overrides:
      read in class FilterInputStream
      Returns:
      the next byte of data, or -1 if the end of the stream is reached.
      Throws:
      IOException - if an I/O error occurs.
      See Also:
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Reads up to len bytes of data from this input stream into an array of bytes. If len is not zero, the method blocks until some input is available; otherwise, no bytes are read and 0 is returned.

      This method simply performs in.read(b, off, len) and returns the result.

      Overrides:
      read in class FilterInputStream
      Parameters:
      b - the buffer into which the data is read.
      off - The start offset in the destination array b.
      len - the maximum number of bytes read.
      Returns:
      the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
      Throws:
      NullPointerException - If b is null.
      IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off
      IOException - if an I/O error occurs.
      See Also: