Package org.apache.juneau.marshall.bson
Class BsonInputStream
java.lang.Object
java.io.InputStream
org.apache.juneau.marshall.parser.ParserInputStream
org.apache.juneau.marshall.bson.BsonInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable,Positionable
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns whether the next byte is the document terminator (0x00).intread()byte[]Reads BSON binary (int32 length + subtype + bytes).booleanReads a boolean (1 byte: 0x00=false, 0x01=true).Reads a cstring (UTF-8 bytes until 0x00).longReads BSON datetime (int64, UTC millis since epoch).Reads BSON decimal128 (16 bytes, IEEE 754-2008) as BigDecimal.intReads 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 a0x00terminator.voidConsumes the document terminator byte (0x00) and closes the length-bounded scope opened by the matchingreadDocumentSize(), rejecting trailing bytes left unconsumed within the declared length.doubleReads an IEEE 754 double (8 bytes, little-endian).Reads a cstring (null-terminated UTF-8).intReads the 1-byte element type.intReads a 32-bit integer (4 bytes, little-endian).longReads a 64-bit integer (8 bytes, little-endian).intreadLE4()Reads a 4-byte little-endian integer.longreadLE8()Reads an 8-byte little-endian long.Reads ObjectId (12 bytes) as 24-character hex string.Reads a BSON string (int32 length + UTF-8 + 0x00).voidskipValue(int type) Skips a value of the given BSON type.Methods inherited from class org.apache.juneau.marshall.parser.ParserInputStream
checkLength, checkLength, getPosition, setMaxLengthMethods inherited from class java.io.InputStream
available, close, mark, markSupported, nullInputStream, read, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferTo
-
Constructor Details
-
BsonInputStream
Constructor.- Parameters:
pipe- The parser input.- Throws:
IOException- Thrown by underlying stream.
-
-
Method Details
-
read
- Overrides:
readin classParserInputStream- Throws:
IOException
-
readLE4
Reads a 4-byte little-endian integer.- Returns:
- The 32-bit value.
- Throws:
IOException- If the stream ends prematurely.
-
readLE8
Reads an 8-byte little-endian long.- Returns:
- The 64-bit value.
- Throws:
IOException- If the stream ends prematurely.
-
readDocumentSize
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 a0x00terminator.- Returns:
- The document size in bytes.
- Throws:
IOException- If the stream ends prematurely.
-
readElementType
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
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 configuredmaxLength, 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
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
Consumes the document terminator byte (0x00) and closes the length-bounded scope opened by the matchingreadDocumentSize(), 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
Reads an IEEE 754 double (8 bytes, little-endian).- Returns:
- The double value.
- Throws:
IOException- If the stream ends prematurely.
-
readInt32
Reads a 32-bit integer (4 bytes, little-endian).- Returns:
- The integer value.
- Throws:
IOException- If the stream ends prematurely.
-
readInt64
Reads a 64-bit integer (8 bytes, little-endian).- Returns:
- The long value.
- Throws:
IOException- If the stream ends prematurely.
-
readBoolean
Reads a boolean (1 byte: 0x00=false, 0x01=true).- Returns:
- The boolean value.
- Throws:
IOException- If the stream ends prematurely.
-
readString
Reads a BSON string (int32 length + UTF-8 + 0x00).- Returns:
- The string value.
- Throws:
IOException- If the stream ends prematurely.
-
readBinary
Reads BSON binary (int32 length + subtype + bytes).- Returns:
- The binary data.
- Throws:
IOException- If the stream ends prematurely.
-
readDateTime
Reads BSON datetime (int64, UTC millis since epoch).- Returns:
- The UTC millis since epoch.
- Throws:
IOException- If the stream ends prematurely.
-
readObjectId
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
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.
-