Class ParserInputStream

java.lang.Object
java.io.InputStream
org.apache.juneau.marshall.parser.ParserInputStream
All Implemented Interfaces:
Closeable, AutoCloseable, Positionable
Direct Known Subclasses:
BsonInputStream, CborInputStream, MsgPackInputStream

public class ParserInputStream extends InputStream implements Positionable
Input stream meant to be used as input for stream-based parsers.

Keeps track of current byte position.

Notes:
  • This class is not thread safe.
See Also:
  • Constructor Details

  • Method Details

    • setMaxLength

      public void setMaxLength(int value)
      Sets the maximum allowed wire-declared length (in bytes) for payloads and element counts read off the stream before they are used to size an allocation or drive a container loop.

      Guards against malformed input where a small payload declares a huge or negative length that would otherwise trigger OutOfMemoryError or NegativeArraySizeException.

      Parameters:
      value - The maximum length in bytes. Values ≤ 0 disable the cap (only the negative-length check remains).
    • checkLength

      public int checkLength(long len, String what) throws IOException
      Validates a wire-declared length/count against sane bounds before it is used to size an allocation or drive a container loop, using this stream's configured maximum.
      Parameters:
      len - The declared length/count read off the wire.
      what - A short description of the field being read (for the error message).
      Returns:
      The validated length as an int.
      Throws:
      IOException - If the length is negative (or beyond int range) or exceeds the configured maximum.
    • checkLength

      public static int checkLength(long len, long maxLength, String what) throws IOException
      Validates a wire-declared length/count against sane bounds before it is used to size an allocation or drive a container loop.

      Shared bounds-check used by the stream-based parsers and by other low-level readers (such as the Protobuf reader) that do not extend this class but need the same guard.

      Parameters:
      len - The declared length/count read off the wire.
      maxLength - The maximum allowed length. Values ≤ 0 are treated as Integer.MAX_VALUE (only the negative-length check applies).
      what - A short description of the field being read (for the error message).
      Returns:
      The validated length as an int.
      Throws:
      IOException - If the length is negative (or beyond int range) or exceeds the configured maximum.
    • getPosition

      Description copied from interface: Positionable
      Returns the current position in a reader or input stream.
      Specified by:
      getPosition in interface Positionable
      Returns:
      The current position in a reader or input stream.
    • read

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