org.apache.commons.io.input

## Class BoundedInputStream

- java.lang.Object
- - java.io.InputStream
  - - org.apache.commons.io.input.BoundedInputStream

- All Implemented Interfaces:
    Closeable, AutoCloseable

      public class BoundedInputStream
      extends InputStream

  This is a stream that will only supply bytes up to a certain length - if its position goes above that, it will stop.

  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 Summary

    Constructors
    Constructor and Description
    `BoundedInputStream(InputStream in)` Creates a new `BoundedInputStream` that wraps the given input stream and is unlimited.
    `BoundedInputStream(InputStream in, long size)` Creates a new `BoundedInputStream` that wraps the given input stream and limits it to a certain size.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `available()`
    `void` | `close()` Invokes the delegate's `close()` method if `isPropagateClose()` is `true`.
    `boolean` | `isPropagateClose()` Indicates whether the `close()` method should propagate to the underling `InputStream`.
    `void` | `mark(int readlimit)` Invokes the delegate's `mark(int)` method.
    `boolean` | `markSupported()` Invokes the delegate's `markSupported()` method.
    `int` | `read()` Invokes the delegate's `read()` method if the current position is less than the limit.
    `int` | `read(byte[] b)` Invokes the delegate's `read(byte[])` method.
    `int` | `read(byte[] b, int off, int len)` Invokes the delegate's `read(byte[], int, int)` method.
    `void` | `reset()` Invokes the delegate's `reset()` method.
    `void` | `setPropagateClose(boolean propagateClose)` Set whether the `close()` method should propagate to the underling `InputStream`.
    `long` | `skip(long n)` Invokes the delegate's `skip(long)` method.
    `String` | `toString()` Invokes the delegate's `toString()` method.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### BoundedInputStream

          public BoundedInputStream(InputStream in,
                                    long size)

      Creates a new `BoundedInputStream` that wraps the given input stream and limits it to a certain size.
      Parameters:
        `in` - The wrapped input stream
        `size` - The maximum number of bytes to return

    - #### BoundedInputStream

          public BoundedInputStream(InputStream in)

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

  - ### Method Detail

    - #### read

          public int read()
                   throws IOException

      Invokes the delegate's `read()` method if the current position is less than the limit.
      Specified by:
        `read` in class `InputStream`
      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 `InputStream`
      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 `InputStream`
      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

    - #### skip

          public long skip(long n)
                    throws IOException

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

    - #### available

          public int available()
                        throws IOException

      Overrides:
        `available` in class `InputStream`
      Throws:
        `IOException`

    - #### toString

          public String toString()

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

    - #### close

          public void close()
                     throws IOException

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

    - #### reset

          public void reset()
                     throws IOException

      Invokes the delegate's `reset()` method.
      Overrides:
        `reset` in class `InputStream`
      Throws:
        `IOException` - if an I/O error occurs

    - #### mark

          public void mark(int readlimit)

      Invokes the delegate's `mark(int)` method.
      Overrides:
        `mark` in class `InputStream`
      Parameters:
        `readlimit` - read ahead limit

    - #### markSupported

          public boolean markSupported()

      Invokes the delegate's `markSupported()` method.
      Overrides:
        `markSupported` in class `InputStream`
      Returns:
        true if mark is supported, otherwise false

    - #### isPropagateClose

          public boolean isPropagateClose()

      Indicates 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.

    - #### setPropagateClose

          public void setPropagateClose(boolean propagateClose)

      Set 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.

Copyright © 2002–2020 The Apache Software Foundation. All rights reserved.