All Implemented Interfaces:
ReadCounter, Closeable, AutoCloseable

public class LimitInputStreamextends CountingInputStream
InputStream that limits total bytes read to a specified maximum.
  • Field Details

    • maxx

      private final long maxx
      Most bytes the stream will read.
  • Constructor Details

    • LimitInputStream

      public LimitInputStream(InputStream in, long max)
      Create a LimitInputStream that limits total bytes read.
      Parameters:
      in - the underlying input stream
      max - the maximum number of bytes that may be read
  • Method Details

    • available

      public int available() throws IOException
      Return the number of bytes available, limited by the remaining quota.
      Overrides:
      available in class FilterInputStream
      Returns:
      the number of readable bytes, capped by the remaining quota
      Throws:
      IOException - on I/O error
    • skip

      public long skip(long n) throws IOException
      Skip bytes, limited by the remaining quota.
      Overrides:
      skip in class CountingInputStream
      Parameters:
      n - number of bytes to skip
      Returns:
      actual bytes skipped
      Throws:
      IOException - on I/O error
    • read

      public int read() throws IOException
      Read a single byte, returning -1 if the limit has been reached.
      Overrides:
      read in class CountingInputStream
      Returns:
      the byte read, or -1 on end of stream or limit reached
      Throws:
      IOException - on I/O error
    • read

      public int read(byte[] buf, int off, int len) throws IOException
      Read bytes into a buffer, limited by the remaining quota.
      Overrides:
      read in class CountingInputStream
      Parameters:
      buf - the buffer to read into
      off - the start offset in the buffer
      len - the maximum number of bytes to read
      Returns:
      the number of bytes read, or -1 on end of stream or limit reached
      Throws:
      IOException - on I/O error