Class AbstractLimitedInputStream

  • All Implemented Interfaces:
    com.helger.commons.state.ICloseable, Closeable, AutoCloseable

    public abstract class AbstractLimitedInputStream
    extends com.helger.commons.io.stream.WrappedInputStream
    implements com.helger.commons.state.ICloseable
    An input stream, which limits its data size. This stream is used, if the content length is unknown.
    • Constructor Detail

      • AbstractLimitedInputStream

        public AbstractLimitedInputStream​(@Nonnull
                                          InputStream aIS,
                                          @Nonnegative
                                          long nSizeMax)
        Creates a new instance.
        Parameters:
        aIS - The input stream, which shall be limited.
        nSizeMax - The limit; no more than this number of bytes shall be returned by the source stream.
    • Method Detail

      • onLimitExceeded

        protected abstract void onLimitExceeded​(@Nonnegative
                                                long nSizeMax,
                                                @Nonnegative
                                                long nCount)
                                         throws IOException
        Called to indicate, that the input streams limit has been exceeded.
        Parameters:
        nSizeMax - The input streams limit, in bytes.
        nCount - 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:
        FilterInputStream
      • read

        public int read​(@Nonnull
                        byte[] b,
                        @Nonnegative
                        int nOfs,
                        @Nonnegative
                        int nLen)
                 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.
        nOfs - The start offset in the destination array b.
        nLen - 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:
        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:
        FilterInputStream
      • isClosed

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