Class SizeLimitInputStream

java.lang.Object
java.io.InputStream
com.Ostermiller.util.SizeLimitInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class SizeLimitInputStream extends InputStream
An input stream wrapper that will read only a set number of bytes from the underlying stream.
Since:
ostermillerutils 1.04.00
Author:
Stephen Ostermiller https://ostermiller.org/contact.pl?regarding=Java+Utilities
  • Constructor Details

    • SizeLimitInputStream

      public SizeLimitInputStream(InputStream in, long maxBytesToRead)
      Create a new size limit input stream from another stream given a size limit.
      Parameters:
      in - The input stream.
      maxBytesToRead - the max number of bytes to allow to be read from the underlying stream.
      Since:
      ostermillerutils 1.04.00
  • Method Details

    • getBytesRead

      public long getBytesRead()
      Get the number of bytes actually read from this stream.
      Returns:
      number of bytes that have already been taken from this stream.
      Since:
      ostermillerutils 1.04.00
    • getBytesLeft

      public long getBytesLeft()
      Get the maximum number of bytes left to read before the limit (set in the constructor) is reached.
      Returns:
      The number of bytes that (at a maximum) are left to be taken from this stream.
      Since:
      ostermillerutils 1.04.00
    • allBytesRead

      public boolean allBytesRead()
      Tell whether the number of bytes specified in the constructor have been read yet.
      Returns:
      true iff the specified number of bytes have all been read.
      Since:
      ostermillerutils 1.04.00
    • getMaxBytesToRead

      public long getMaxBytesToRead()
      Get the number of total bytes (including bytes already read) that can be read from this stream (as set in the constructor).
      Returns:
      Maximum bytes that can be read until the size limit runs out
      Since:
      ostermillerutils 1.04.00
    • read

      public int read() throws IOException
      Specified by:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException
      Overrides:
      read in class InputStream
      Throws:
      IOException
    • skip

      public long skip(long n) throws IOException
      Overrides:
      skip in class InputStream
      Throws:
      IOException
    • available

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

      public void close() throws IOException
      Close this stream and underlying streams. Calling this method may make data on the underlying stream unavailable.

      Consider wrapping this stream in a NoCloseStream so that clients can call close() with no effect.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException
      Since:
      ostermillerutils 1.04.00
    • mark

      public void mark(int readlimit)
      Overrides:
      mark in class InputStream
    • reset

      public void reset() throws IOException
      Overrides:
      reset in class InputStream
      Throws:
      IOException
    • markSupported

      public boolean markSupported()
      Overrides:
      markSupported in class InputStream