Package org.bson.io
Class ByteBufferBsonInput
java.lang.Object
org.bson.io.ByteBufferBsonInput
- All Implemented Interfaces:
Closeable,AutoCloseable,BsonInput
An implementation of
BsonInput that is backed by a ByteBuf.- Since:
- 3.0
-
Constructor Summary
ConstructorsConstructorDescriptionByteBufferBsonInput(ByteBuf buffer) Construct an instance with the given byte buffer. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()getMark(int readLimit) Gets a mark for the current position in the stream.intGets the current position in the streamintGets an upper bound on the number of bytes remaining to be read, orInteger.MAX_VALUEif this input cannot determine one.booleanReturns true if there are more bytes left in the stream.voidpipe(BsonOutput output, int numBytes) bytereadByte()Reads a single byte from the streamvoidreadBytes(byte[] bytes) Reads the specified number of bytes into the given byte array.voidreadBytes(byte[] bytes, int offset, int length) Reads the specified number of bytes into the given byte array starting at the specified offset.Reads a BSON CString value from the stream.doubleReads a BSON Double value from the stream.intReads a BSON Int32 value from the stream.longReads a BSON Int64 value from the stream.Reads a BSON ObjectId value from the stream.Reads a BSON String value from the stream.voidskip(int numBytes) Skips the specified number of bytes in the stream.voidSkips a BSON CString value from the stream.
-
Constructor Details
-
ByteBufferBsonInput
Construct an instance with the given byte buffer. The stream takes over ownership of the buffer and closes it when this instance is closed.- Parameters:
buffer- the byte buffer
-
-
Method Details
-
getPosition
public int getPosition()Description copied from interface:BsonInputGets the current position in the stream- Specified by:
getPositionin interfaceBsonInput- Returns:
- the current position
-
readByte
public byte readByte()Description copied from interface:BsonInputReads a single byte from the stream -
readBytes
public void readBytes(byte[] bytes) Description copied from interface:BsonInputReads the specified number of bytes into the given byte array. This is equivalent toreadBytes(bytes, 0, bytes.length). -
readBytes
public void readBytes(byte[] bytes, int offset, int length) Description copied from interface:BsonInputReads the specified number of bytes into the given byte array starting at the specified offset. -
readInt64
public long readInt64()Description copied from interface:BsonInputReads a BSON Int64 value from the stream. -
readDouble
public double readDouble()Description copied from interface:BsonInputReads a BSON Double value from the stream.- Specified by:
readDoublein interfaceBsonInput- Returns:
- the double value
-
readInt32
public int readInt32()Description copied from interface:BsonInputReads a BSON Int32 value from the stream. -
readObjectId
Description copied from interface:BsonInputReads a BSON ObjectId value from the stream.- Specified by:
readObjectIdin interfaceBsonInput- Returns:
- the ObjectId
-
readString
Description copied from interface:BsonInputReads a BSON String value from the stream.- Specified by:
readStringin interfaceBsonInput- Returns:
- the string
-
readCString
Description copied from interface:BsonInputReads a BSON CString value from the stream.- Specified by:
readCStringin interfaceBsonInput- Returns:
- the CString
-
skipCString
public void skipCString()Description copied from interface:BsonInputSkips a BSON CString value from the stream.- Specified by:
skipCStringin interfaceBsonInput
-
skip
public void skip(int numBytes) Description copied from interface:BsonInputSkips the specified number of bytes in the stream. -
getMark
Description copied from interface:BsonInputGets a mark for the current position in the stream. -
hasRemaining
public boolean hasRemaining()Description copied from interface:BsonInputReturns true if there are more bytes left in the stream.- Specified by:
hasRemainingin interfaceBsonInput- Returns:
- true if there are more bytes left in the stream.
-
getRemaining
public int getRemaining()Description copied from interface:BsonInputGets an upper bound on the number of bytes remaining to be read, orInteger.MAX_VALUEif this input cannot determine one. Decoders use this to reject an impossible declared length before allocating an array of that size.The default returns
Integer.MAX_VALUE, the maximum size the BSON format permits (the int32 length field) and the same valueBsonBinaryWriterSettingsuses as its default maximum document size. It means "no tighter bound is known" and disables the pre-allocation check. Implementations backed by untrusted or streamed input should override this to return their actual remaining length; otherwise a small malformed input that declares a huge length can trigger a large allocation (and potentialOutOfMemoryError) before the read fails.- Specified by:
getRemainingin interfaceBsonInput- Returns:
- an upper bound on the number of readable bytes, or
Integer.MAX_VALUEif unknown
-
pipe
Description copied from interface:BsonInput -
close
public void close()
-