Class BsonInputStream

All Implemented Interfaces:
Closeable, AutoCloseable, Positionable

public class BsonInputStream extends ParserInputStream
Specialized input stream for parsing BSON (Binary JSON) streams.

BSON uses little-endian byte order for all multi-byte values.

Notes:
  • This class is not intended for external use.
See Also:
  • Constructor Details

  • Method Details

    • read

      public int read() throws IOException
      Overrides:
      read in class ParserInputStream
      Throws:
      IOException
    • readLE4

      public int readLE4() throws IOException
      Reads a 4-byte little-endian integer.
      Returns:
      The 32-bit value.
      Throws:
      IOException - If the stream ends prematurely.
    • readLE8

      public long readLE8() throws IOException
      Reads an 8-byte little-endian long.
      Returns:
      The 64-bit value.
      Throws:
      IOException - If the stream ends prematurely.
    • readDocumentSize

      public int readDocumentSize() throws IOException
      Reads the document size (int32) from the stream and opens a length-bounded scope for the document body that follows, so a forged-short declared length cannot be bypassed by traversal that otherwise relies solely on finding a 0x00 terminator.
      Returns:
      The document size in bytes.
      Throws:
      IOException - If the stream ends prematurely.
    • readElementType

      public int readElementType() throws IOException
      Reads the 1-byte element type.
      Returns:
      The BSON type byte (0x01-0x13, 0x7F, 0xFF).
      Throws:
      IOException - If the stream ends prematurely.
    • readElementName

      Reads a cstring (null-terminated UTF-8).
      Returns:
      The element name.
      Throws:
      IOException - If the stream ends prematurely.
    • readCString

      public String readCString() throws IOException
      Reads a cstring (UTF-8 bytes until 0x00).

      Growth is capped by whichever is tighter of the innermost open document/array bound (enforced by read()) or the configured maxLength, so an unterminated cstring cannot grow the backing buffer without limit.

      Returns:
      The string value.
      Throws:
      IOException - If the stream ends prematurely or the cstring exceeds the configured maximum length.
    • isDocumentEnd

      public boolean isDocumentEnd() throws IOException
      Returns whether the next byte is the document terminator (0x00).
      Returns:
      true if next byte is 0x00, false otherwise.
      Throws:
      IOException - If the stream ends prematurely.
    • readDocumentTerminator

      public void readDocumentTerminator() throws IOException
      Consumes the document terminator byte (0x00) and closes the length-bounded scope opened by the matching readDocumentSize(), rejecting trailing bytes left unconsumed within the declared length.
      Throws:
      IOException - If the stream ends prematurely, the byte is not 0x00, or the declared document length was not fully consumed.
    • readDouble

      public double readDouble() throws IOException
      Reads an IEEE 754 double (8 bytes, little-endian).
      Returns:
      The double value.
      Throws:
      IOException - If the stream ends prematurely.
    • readInt32

      public int readInt32() throws IOException
      Reads a 32-bit integer (4 bytes, little-endian).
      Returns:
      The integer value.
      Throws:
      IOException - If the stream ends prematurely.
    • readInt64

      public long readInt64() throws IOException
      Reads a 64-bit integer (8 bytes, little-endian).
      Returns:
      The long value.
      Throws:
      IOException - If the stream ends prematurely.
    • readBoolean

      public boolean readBoolean() throws IOException
      Reads a boolean (1 byte: 0x00=false, 0x01=true).
      Returns:
      The boolean value.
      Throws:
      IOException - If the stream ends prematurely.
    • readString

      public String readString() throws IOException
      Reads a BSON string (int32 length + UTF-8 + 0x00).
      Returns:
      The string value.
      Throws:
      IOException - If the stream ends prematurely.
    • readBinary

      public byte[] readBinary() throws IOException
      Reads BSON binary (int32 length + subtype + bytes).
      Returns:
      The binary data.
      Throws:
      IOException - If the stream ends prematurely.
    • readDateTime

      public long readDateTime() throws IOException
      Reads BSON datetime (int64, UTC millis since epoch).
      Returns:
      The UTC millis since epoch.
      Throws:
      IOException - If the stream ends prematurely.
    • readObjectId

      public String readObjectId() throws IOException
      Reads ObjectId (12 bytes) as 24-character hex string.
      Returns:
      The ObjectId as hex string.
      Throws:
      IOException - If the stream ends prematurely.
    • readDecimal128

      Reads BSON decimal128 (16 bytes, IEEE 754-2008) as BigDecimal.
      Returns:
      The decimal value, or null if the wire value is NaN or ±Infinity (which have no BigDecimal form).
      Throws:
      IOException - If the stream ends prematurely.
    • skipValue

      public void skipValue(int type) throws IOException
      Skips a value of the given BSON type.
      Parameters:
      type - The BSON type byte (0x01-0x13, 0x7F, 0xFF).
      Throws:
      IOException - If the stream ends prematurely.