Class BsonOutputStream

java.lang.Object
java.io.OutputStream
org.apache.juneau.marshall.bson.BsonOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class BsonOutputStream extends OutputStream
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 [int32 size][content][0x00] is written to the parent stream.

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

  • Method Details

    • write

      public void write(int b)
      Specified by:
      write in class OutputStream
    • write

      public void write(byte[] b)
      Overrides:
      write in class OutputStream
    • write

      public void write(byte[] b, int off, int len)
      Overrides:
      write in class OutputStream
    • startDocument

      public void startDocument()
      Begin buffering a new document.
    • endDocument

      public byte[] endDocument()
      Finalize the current document and return the bytes (int32 size + content + 0x00).
      Returns:
      The complete document bytes.
    • writeDocumentTo

      public void writeDocumentTo(OutputStream parent)
      Finalize the current document and write it to the parent stream.
      Parameters:
      parent - The parent output stream.
    • writeElement

      public void writeElement(int type, String name)
      Writes a BSON element header (type byte + cstring name).
      Parameters:
      type - The BSON type code.
      name - The element name (cstring).
    • writeLE4

      public void writeLE4(int value)
      Writes a 4-byte little-endian integer.
      Parameters:
      value - The 32-bit value.
    • writeLE8

      public void writeLE8(long value)
      Writes an 8-byte little-endian long.
      Parameters:
      value - The 64-bit value.
    • writeDouble

      public void writeDouble(double value)
      Writes an IEEE 754 double (8 bytes, little-endian).
      Parameters:
      value - The double value.
    • writeInt32

      public void writeInt32(int value)
      Writes a 32-bit integer (4 bytes, little-endian).
      Parameters:
      value - The integer value.
    • writeInt64

      public void writeInt64(long value)
      Writes a 64-bit integer (8 bytes, little-endian).
      Parameters:
      value - The long value.
    • writeBoolean

      public void writeBoolean(boolean value)
      Writes a boolean (1 byte: 0x00=false, 0x01=true).
      Parameters:
      value - The boolean value.
    • writeNull

      public void writeNull()
      Writes BSON null (no value bytes).
    • writeString

      public void writeString(String value)
      Writes a BSON string (int32 length + UTF-8 bytes + 0x00).
      Parameters:
      value - The string to write (treated as empty if null).
    • writeCString

      public void writeCString(String value)
      Writes a cstring (UTF-8 bytes + 0x00, no length prefix).
      Parameters:
      value - The string to write (treated as empty if null).
    • writeBinary

      public void writeBinary(byte[] data)
      Writes BSON binary (int32 length + subtype byte + raw bytes).
      Parameters:
      data - The raw bytes (subtype 0x00 generic).
    • writeDateTime

      public void writeDateTime(long millis)
      Writes BSON datetime (int64, UTC milliseconds since epoch).
      Parameters:
      millis - The UTC milliseconds since epoch.
    • writeDecimal128

      public void writeDecimal128(BigDecimal value)
      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

      public void writeChildDocument(BsonOutputStream child)
      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

      public boolean isInDocument()
      Returns whether we are currently buffering a document.
      Returns:
      true if buffering, false otherwise.