Class FrameSizeLimitedFilterInputStream

java.lang.Object
java.io.InputStream
org.apache.activemq.transport.FrameSizeLimitedFilterInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class FrameSizeLimitedFilterInputStream extends InputStream
A filtered style input stream that allows reads up to a given known max frame size before it starts to throw exceptions indicating the reader has exceeded the set limit. This can be used to wrap another stream that contains a protocol frame to be parsed and enforce that decoding of that frame does not cross the boundary set as the max available bytes before error.

This is a specialized stream type that may obfuscate the actual state of the underlying stream such as its actual available bytes. The user should be aware of the behavior of this stream when using it to ensure they do not run into unexpected failures. It is possible to configure this stream with a higher available limit than the underlying stream actually has access to but that inconsistency is left as a requirement for the caller to handle.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new uninitialized instance of the filter stream that will fail to read until a stream and a frame size limit is configured.
    Create a new instance with the given amount of available bytes that should be readable before an exception is thrown indicating that more bytes where requested from the known fixed frame size than is allowed.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    void
    Render the stream unusable until a reset is called that either changes the stream and assigns a new max or simply assigns a new max which assumes that the underlying stream remains readable which is only a subset of stream types such as byte array wrapper variants.
    void
    mark(int readLimit)
     
    boolean
     
    int
     
    int
    read(byte[] b)
     
    int
    read(byte[] b, int off, int len)
     
    void
     
    void
    Resets the number of available bytes that can be read from the underlying stream.
    void
    resetAvailable(int available)
    Resets the number of available bytes that can be read from the underlying stream to the new amount.
    void
    resetAvailable(InputStream in, int available)
    Resets the number of available bytes and assigns a new stream that can be read from the which allows this type to be re-usable across command reads.
    long
    skip(long amount)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FrameSizeLimitedFilterInputStream

      public FrameSizeLimitedFilterInputStream()
      Create a new uninitialized instance of the filter stream that will fail to read until a stream and a frame size limit is configured.
    • FrameSizeLimitedFilterInputStream

      public FrameSizeLimitedFilterInputStream(int available, InputStream in)
      Create a new instance with the given amount of available bytes that should be readable before an exception is thrown indicating that more bytes where requested from the known fixed frame size than is allowed.
      Parameters:
      available - The number of available bytes to allow in a given frame.
      in - The InputStream to read from (cannot be null).
  • Method Details

    • close

      public void close() throws IOException
      Render the stream unusable until a reset is called that either changes the stream and assigns a new max or simply assigns a new max which assumes that the underlying stream remains readable which is only a subset of stream types such as byte array wrapper variants.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InputStream
      Throws:
      IOException
    • resetAvailable

      public void resetAvailable()
      Resets the number of available bytes that can be read from the underlying stream. The underlying stream may still throw exceptions if it cannot provide this many bytes. As a result of calling this method any currently set mark is cleared and the stream cannot be reset back to a previously available number of bytes from this point onward.

      Calling this method on a stream wrapper that has not been initialized will not result in a readable state, the limit remains zero.

    • resetAvailable

      public void resetAvailable(int available)
      Resets the number of available bytes that can be read from the underlying stream to the new amount. The underlying stream may still throw exceptions if it cannot provide this many bytes. As a result of calling this method any currently set mark is cleared and the stream cannot be reset back to a previously available number of bytes from this point onward.
      Parameters:
      available - The new available number of bytes to allow from this stream wrapper
    • resetAvailable

      public void resetAvailable(InputStream in, int available)
      Resets the number of available bytes and assigns a new stream that can be read from the which allows this type to be re-usable across command reads. The underlying stream may still throw exceptions if it cannot provide this many bytes. As a result of calling this method any currently set mark is cleared and the stream cannot be reset back to a previously available number of bytes from this point onward.
      Parameters:
      in - The new input stream to read bytes from (cannot be assigned as null).
      available - The new available number of bytes to allow from this stream wrapper
    • 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 amount) throws IOException
      Overrides:
      skip in class InputStream
      Throws:
      IOException
    • available

      public int available() throws IOException
      Overrides:
      available in class InputStream
      Throws:
      IOException
    • 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