Package org.bson.io
Class BasicOutputBuffer
java.lang.Object
java.io.OutputStream
org.bson.io.OutputBuffer
org.bson.io.BasicOutputBuffer
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable,BsonOutput
A BSON output stream that stores the output in a single, un-pooled byte array.
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct an instance with a default initial byte array size.BasicOutputBuffer(int initialSize) Construct an instance with the specified initial byte array size. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()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.byte[]Gets the internal buffer.intGets the current position in the stream.intgetSize()Gets the current size of the stream in number of bytes.intpipe(OutputStream out) Pipe the contents of this output buffer into the given output streambyte[]Gets a copy of the buffered bytes.voidtruncateToPosition(int newPosition) Truncates this stream to the new position.voidwrite(byte[] b) protected voidwrite(int absolutePosition, int value) Write the specified byte at the specified position.voidwriteByte(int value) Write a single byte to the stream.voidwriteBytes(byte[] bytes, int offset, int length) Writeslengthbytes from the byte array, starting atoffset.voidwriteInt32(int value) Writes a 32-bit BSON integer to the stream.voidwriteInt32(int position, int value) Writes a 32-bit BSON integer to the stream at the given position.voidwriteInt64(long value) Writes a 64-bit BSON integer to the stream.voidwriteObjectId(ObjectId value) Writes a BSON ObjectId to the stream.Methods inherited from class org.bson.io.OutputBuffer
flush, size, toString, write, write, writeBytes, writeCharacters, writeCString, writeDouble, writeInt, writeLong, writeStringMethods inherited from class java.io.OutputStream
nullOutputStream
-
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:
writein classOutputBuffer
-
toByteArray
public byte[] toByteArray()Description copied from class:OutputBufferGets a copy of the buffered bytes.- Overrides:
toByteArrayin classOutputBuffer- Returns:
- the byte array
- See Also:
-
writeInt32
public void writeInt32(int value) Description copied from interface:BsonOutputWrites a 32-bit BSON integer to the stream.- Specified by:
writeInt32in interfaceBsonOutput- Overrides:
writeInt32in classOutputBuffer- Parameters:
value- the value
-
writeInt32
public void writeInt32(int position, int value) Description copied from interface:BsonOutputWrites 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:
writeInt32in interfaceBsonOutput- Overrides:
writeInt32in classOutputBuffer- 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 sizevalue- the value
-
writeInt64
public void writeInt64(long value) Description copied from interface:BsonOutputWrites a 64-bit BSON integer to the stream.- Specified by:
writeInt64in interfaceBsonOutput- Overrides:
writeInt64in classOutputBuffer- Parameters:
value- the value
-
writeObjectId
Description copied from interface:BsonOutputWrites a BSON ObjectId to the stream.- Specified by:
writeObjectIdin interfaceBsonOutput- Overrides:
writeObjectIdin classOutputBuffer- Parameters:
value- the value
-
writeBytes
public void writeBytes(byte[] bytes, int offset, int length) Description copied from interface:BsonOutputWriteslengthbytes from the byte array, starting atoffset.- Parameters:
bytes- the non-null byte arrayoffset- the offset to start writing fromlength- the number of bytes to write
-
writeByte
public void writeByte(int value) Description copied from interface:BsonOutputWrite 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:OutputBufferWrite the specified byte at the specified position.- Specified by:
writein classOutputBuffer- Parameters:
absolutePosition- the position, which must be greater than equal to 0 and at least 4 less than the stream sizevalue- the value to write. The 24 high-order bits of the value are ignored.
-
getPosition
public int getPosition()Description copied from interface:BsonOutputGets the current position in the stream.- Returns:
- the current position
-
getSize
public int getSize()Description copied from interface:BsonOutputGets the current size of the stream in number of bytes.- Returns:
- size of data so far
-
pipe
Description copied from class:OutputBufferPipe the contents of this output buffer into the given output stream- Specified by:
pipein classOutputBuffer- 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:BsonOutputTruncates this stream to the new position. After this call, both size and position will equal the new position.- Specified by:
truncateToPositionin interfaceBsonOutput- Specified by:
truncateToPositionin classOutputBuffer- Parameters:
newPosition- the new position, which must be greater than or equal to 0 and less than the current size.
-
getByteBuffers
Description copied from class:OutputBufferGet 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:
getByteBuffersin classOutputBuffer- Returns:
- the non-null list of byte buffers, in LITTLE_ENDIAN order. The returned
ByteBufs must eventually be released explicitly, callingOutputBuffer.close()may be not enough to release them. The caller must not use theByteBufs after closing thisOutputBuffer, though releasing them is allowed to be done after closing thisOutputBuffer.
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceBsonOutput- Specified by:
closein interfaceCloseable- Overrides:
closein classOutputBuffer
-