Package org.bson.io

Class BasicOutputBuffer

All Implemented Interfaces:
Closeable, Flushable, AutoCloseable, BsonOutput

public class BasicOutputBuffer extends OutputBuffer
A BSON output stream that stores the output in a single, un-pooled byte array.
  • Constructor Details

    • BasicOutputBuffer

      public BasicOutputBuffer()
      Construct an instance with a default initial byte array size.
    • BasicOutputBuffer

      public BasicOutputBuffer(int initialSize)
      Construct an instance with the specified initial byte array size.
      Parameters:
      initialSize - the initial size of the byte array
  • Method Details

    • getInternalBuffer

      public byte[] getInternalBuffer()
      Gets the internal buffer.
      Returns:
      the internal buffer
      Since:
      3.3
    • write

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

      public byte[] toByteArray()
      Description copied from class: OutputBuffer
      Gets a copy of the buffered bytes.
      Overrides:
      toByteArray in class OutputBuffer
      Returns:
      the byte array
      See Also:
    • writeInt32

      public void writeInt32(int value)
      Description copied from interface: BsonOutput
      Writes a 32-bit BSON integer to the stream.
      Specified by:
      writeInt32 in interface BsonOutput
      Overrides:
      writeInt32 in class OutputBuffer
      Parameters:
      value - the value
    • writeInt32

      public void writeInt32(int position, int value)
      Description copied from interface: BsonOutput
      Writes a 32-bit BSON integer to the stream at the given position. This is useful for patching in the size of a document once the last byte of it has been encoded and its size it known.
      Specified by:
      writeInt32 in interface BsonOutput
      Overrides:
      writeInt32 in class OutputBuffer
      Parameters:
      position - the position to write the value, which must be greater than or equal to 0 and less than or equal to the current size
      value - the value
    • writeInt64

      public void writeInt64(long value)
      Description copied from interface: BsonOutput
      Writes a 64-bit BSON integer to the stream.
      Specified by:
      writeInt64 in interface BsonOutput
      Overrides:
      writeInt64 in class OutputBuffer
      Parameters:
      value - the value
    • writeObjectId

      public void writeObjectId(ObjectId value)
      Description copied from interface: BsonOutput
      Writes a BSON ObjectId to the stream.
      Specified by:
      writeObjectId in interface BsonOutput
      Overrides:
      writeObjectId in class OutputBuffer
      Parameters:
      value - the value
    • writeBytes

      public void writeBytes(byte[] bytes, int offset, int length)
      Description copied from interface: BsonOutput
      Writes length bytes from the byte array, starting at offset.
      Parameters:
      bytes - the non-null byte array
      offset - the offset to start writing from
      length - the number of bytes to write
    • writeByte

      public void writeByte(int value)
      Description copied from interface: BsonOutput
      Write a single byte to the stream. The byte to be written is the eight low-order bits of the specified value. The 24 high-order bits of the value are ignored.
      Parameters:
      value - the value
    • write

      protected void write(int absolutePosition, int value)
      Description copied from class: OutputBuffer
      Write the specified byte at the specified position.
      Specified by:
      write in class OutputBuffer
      Parameters:
      absolutePosition - the position, which must be greater than equal to 0 and at least 4 less than the stream size
      value - the value to write. The 24 high-order bits of the value are ignored.
    • getPosition

      public int getPosition()
      Description copied from interface: BsonOutput
      Gets the current position in the stream.
      Returns:
      the current position
    • getSize

      public int getSize()
      Description copied from interface: BsonOutput
      Gets the current size of the stream in number of bytes.
      Returns:
      size of data so far
    • pipe

      public int pipe(OutputStream out) throws IOException
      Description copied from class: OutputBuffer
      Pipe the contents of this output buffer into the given output stream
      Specified by:
      pipe in class OutputBuffer
      Parameters:
      out - the stream to pipe to
      Returns:
      number of bytes written to the stream
      Throws:
      IOException - if the stream throws an exception
    • truncateToPosition

      public void truncateToPosition(int newPosition)
      Description copied from interface: BsonOutput
      Truncates this stream to the new position. After this call, both size and position will equal the new position.
      Specified by:
      truncateToPosition in interface BsonOutput
      Specified by:
      truncateToPosition in class OutputBuffer
      Parameters:
      newPosition - the new position, which must be greater than or equal to 0 and less than the current size.
    • getByteBuffers

      public List<ByteBuf> getByteBuffers()
      Description copied from class: OutputBuffer
      Get a list of byte buffers that are prepared to be read from; in other words, whose position is 0 and whose limit is the number of bytes that should read.

      Note that the byte buffers may be read-only.

      Specified by:
      getByteBuffers in class OutputBuffer
      Returns:
      the non-null list of byte buffers, in LITTLE_ENDIAN order. The returned ByteBufs must eventually be released explicitly, calling OutputBuffer.close() may be not enough to release them. The caller must not use the ByteBufs after closing this OutputBuffer, though releasing them is allowed to be done after closing this OutputBuffer.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface BsonOutput
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputBuffer