Package org.apache.juneau.marshall.bson
Class BsonOutputStream
java.lang.Object
java.io.OutputStream
org.apache.juneau.marshall.bson.BsonOutputStream
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
Specialized output stream for serializing BSON (Binary JSON) streams.
BSON uses little-endian byte order for all multi-byte values. Documents are length-prefixed: the total
byte count (int32) must appear before the content. Since the size is unknown until all elements are written,
each document is buffered in a ByteArrayOutputStream child. When complete, the size is calculated
and
Notes:
- This class is not intended for external use.
See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCreates a child BsonOutputStream for buffering a nested document.byte[]Finalize the current document and return the bytes (int32 size + content + 0x00).booleanReturns whether we are currently buffering a document.voidBegin buffering a new document.voidwrite(byte[] b) voidwrite(byte[] b, int off, int len) voidwrite(int b) voidwriteBinary(byte[] data) Writes BSON binary (int32 length + subtype byte + raw bytes).voidwriteBoolean(boolean value) Writes a boolean (1 byte: 0x00=false, 0x01=true).voidWrites the child document's content as an embedded document to this stream.voidwriteCString(String value) Writes a cstring (UTF-8 bytes + 0x00, no length prefix).voidwriteDateTime(long millis) Writes BSON datetime (int64, UTC milliseconds since epoch).voidwriteDecimal128(BigDecimal value) Writes BSON decimal128 (16 bytes, IEEE 754-2008) from BigDecimal.voidwriteDocumentTo(OutputStream parent) Finalize the current document and write it to the parent stream.voidwriteDouble(double value) Writes an IEEE 754 double (8 bytes, little-endian).voidwriteElement(int type, String name) Writes a BSON element header (type byte + cstring name).voidwriteInt32(int value) Writes a 32-bit integer (4 bytes, little-endian).voidwriteInt64(long value) Writes a 64-bit integer (8 bytes, little-endian).voidwriteLE4(int value) Writes a 4-byte little-endian integer.voidwriteLE8(long value) Writes an 8-byte little-endian long.voidWrites BSON null (no value bytes).voidwriteString(String value) Writes a BSON string (int32 length + UTF-8 bytes + 0x00).Methods inherited from class java.io.OutputStream
close, flush, nullOutputStream
-
Constructor Details
-
BsonOutputStream
Constructor.- Parameters:
os- The output stream being wrapped.
-
-
Method Details
-
write
- Specified by:
writein classOutputStream
-
write
- Overrides:
writein classOutputStream
-
write
- Overrides:
writein classOutputStream
-
startDocument
Begin buffering a new document. -
endDocument
Finalize the current document and return the bytes (int32 size + content + 0x00).- Returns:
- The complete document bytes.
-
writeDocumentTo
Finalize the current document and write it to the parent stream.- Parameters:
parent- The parent output stream.
-
writeElement
Writes a BSON element header (type byte + cstring name).- Parameters:
type- The BSON type code.name- The element name (cstring).
-
writeLE4
Writes a 4-byte little-endian integer.- Parameters:
value- The 32-bit value.
-
writeLE8
Writes an 8-byte little-endian long.- Parameters:
value- The 64-bit value.
-
writeDouble
Writes an IEEE 754 double (8 bytes, little-endian).- Parameters:
value- The double value.
-
writeInt32
Writes a 32-bit integer (4 bytes, little-endian).- Parameters:
value- The integer value.
-
writeInt64
Writes a 64-bit integer (8 bytes, little-endian).- Parameters:
value- The long value.
-
writeBoolean
Writes a boolean (1 byte: 0x00=false, 0x01=true).- Parameters:
value- The boolean value.
-
writeNull
Writes BSON null (no value bytes). -
writeString
Writes a BSON string (int32 length + UTF-8 bytes + 0x00).- Parameters:
value- The string to write (treated as empty ifnull ).
-
writeCString
Writes a cstring (UTF-8 bytes + 0x00, no length prefix).- Parameters:
value- The string to write (treated as empty ifnull ).
-
writeBinary
Writes BSON binary (int32 length + subtype byte + raw bytes).- Parameters:
data- The raw bytes (subtype 0x00 generic).
-
writeDateTime
Writes BSON datetime (int64, UTC milliseconds since epoch).- Parameters:
millis- The UTC milliseconds since epoch.
-
writeDecimal128
Writes BSON decimal128 (16 bytes, IEEE 754-2008) from BigDecimal.- Parameters:
value- The decimal value.
-
createChild
Creates a child BsonOutputStream for buffering a nested document.- Returns:
- A new child stream (no parent; use
writeChildDocument(org.apache.juneau.marshall.bson.BsonOutputStream)to embed).
-
writeChildDocument
Writes the child document's content as an embedded document to this stream.- Parameters:
child- The child stream (must have been finalized via endDocument).
-
isInDocument
Returns whether we are currently buffering a document.- Returns:
true if buffering,false otherwise.
-