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
ConstructorDescriptionConstruct 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 TypeMethodDescriptionvoid
close()
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.int
Gets the current position in the stream.int
getSize()
Gets the current size of the stream in number of bytes.int
pipe
(OutputStream out) Pipe the contents of this output buffer into the given output streamvoid
truncateToPosition
(int newPosition) Truncates this stream to the new position.void
write
(byte[] b) protected void
write
(int absolutePosition, int value) Write the specified byte at the specified position.void
writeByte
(int value) Write a single byte to the stream.void
writeBytes
(byte[] bytes, int offset, int length) Writeslength
bytes from the byte array, starting atoffset
.Methods inherited from class org.bson.io.OutputBuffer
size, toByteArray, toString, write, write, writeBytes, writeCString, writeDouble, writeInt, writeInt32, writeInt32, writeInt64, writeLong, writeObjectId, writeString
Methods inherited from class java.io.OutputStream
flush, 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:
write
in classOutputBuffer
-
writeBytes
public void writeBytes(byte[] bytes, int offset, int length) Description copied from interface:BsonOutput
Writeslength
bytes 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: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 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: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
Description copied from class:OutputBuffer
Pipe the contents of this output buffer into the given output stream- Specified by:
pipe
in 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:BsonOutput
Truncates this stream to the new position. After this call, both size and position will equal the new position.- Specified by:
truncateToPosition
in interfaceBsonOutput
- Specified by:
truncateToPosition
in 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: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 classOutputBuffer
- Returns:
- the non-null list of byte buffers, in LITTLE_ENDIAN order
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceBsonOutput
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputBuffer
-