Package org.bson.io
Interface BsonInput
- All Superinterfaces:
AutoCloseable,Closeable
- All Known Implementing Classes:
ByteBufferBsonInput
An input stream that is optimized for reading BSON values directly from the underlying stream.
- Since:
- 3.0
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()getMark(int readLimit) Gets a mark for the current position in the stream.intGets the current position in the streamdefault intGets 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.default 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.
-
Method Details
-
getPosition
int getPosition()Gets the current position in the stream- Returns:
- the current position
-
readByte
byte readByte()Reads a single byte from the stream- Returns:
- the byte value
-
readBytes
void readBytes(byte[] bytes) Reads the specified number of bytes into the given byte array. This is equivalent toreadBytes(bytes, 0, bytes.length).- Parameters:
bytes- the byte array to write into
-
readBytes
void readBytes(byte[] bytes, int offset, int length) Reads the specified number of bytes into the given byte array starting at the specified offset.- Parameters:
bytes- the byte array to write intooffset- the offset to start writinglength- the number of bytes to write
-
readInt64
long readInt64()Reads a BSON Int64 value from the stream.- Returns:
- the Int64 value
-
readDouble
double readDouble()Reads a BSON Double value from the stream.- Returns:
- the double value
-
readInt32
int readInt32()Reads a BSON Int32 value from the stream.- Returns:
- the Int32 value
-
readString
String readString()Reads a BSON String value from the stream.- Returns:
- the string
-
readObjectId
ObjectId readObjectId()Reads a BSON ObjectId value from the stream.- Returns:
- the ObjectId
-
readCString
String readCString()Reads a BSON CString value from the stream.- Returns:
- the CString
-
skipCString
void skipCString()Skips a BSON CString value from the stream. -
skip
void skip(int numBytes) Skips the specified number of bytes in the stream.- Parameters:
numBytes- the number of bytes to skip
-
getMark
Gets a mark for the current position in the stream.- Parameters:
readLimit- the maximum limit of bytes that can be read before the mark position becomes invalid- Returns:
- the mark
- Since:
- 3.7
-
hasRemaining
boolean hasRemaining()Returns true if there are more bytes left in the stream.- Returns:
- true if there are more bytes left in the stream.
-
getRemaining
default int getRemaining()Gets 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.- Returns:
- an upper bound on the number of readable bytes, or
Integer.MAX_VALUEif unknown - Since:
- 5.10
-
pipe
- Parameters:
output- the output to pipe tonumBytes- the number of bytes to pipe- Since:
- 5.8
-
close
void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-