Class LimitedInputStream

java.lang.Object
java.io.InputStream
org.refcodes.io.LimitedInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class LimitedInputStream extends InputStream
A LimitedInputStream wraps an InputStream and exposes at most a fixed number of bytes from it. Once the configured limit is reached, all read operations behave as if end of stream was reached (returning -1). This is useful when parsing framed or section based binary formats where a component must not read beyond its assigned payload region.
  • Constructor Details

    • LimitedInputStream

      public LimitedInputStream(InputStream aInputStream, long aLimit)
      Creates a LimitedInputStream that allows reading at most aLimit bytes from the provided stream. The wrapped stream is not closed by this wrapper.
      Parameters:
      aInputStream - The underlying stream to read from
      aLimit - The maximum number of bytes that can be read, must be >= 0
      Throws:
      NullPointerException - if aInputStream is null
      IllegalArgumentException - if aLimit is negative
    • LimitedInputStream

      public LimitedInputStream(InputStream aInputStream, long aLimit, boolean isDelegateClose)
      Creates a LimitedInputStream that allows reading at most aLimit bytes from the provided stream. The wrapped stream is not closed by this wrapper depending on the according constructor's delegate close flag.
      Parameters:
      aInputStream - The underlying stream to read from
      aLimit - The maximum number of bytes that can be read, must be >= 0
      isDelegateClose - If true, then close() closes the wrapped stream. If false, then close() is a no-op.
      Throws:
      NullPointerException - if aInputStream is null
      IllegalArgumentException - if aLimit is negative
  • Method Details