public class BsonInputStream extends InputStream
BsonInputStream
provides a class to read BSON documents based on the
BSON specification.Modifier and Type | Field and Description |
---|---|
static Charset |
UTF8
UTF-8 Character set for encoding strings.
|
Constructor and Description |
---|
BsonInputStream(InputStream input)
Creates a BSON document reader.
|
BsonInputStream(InputStream input,
int expectedMaxDocumentSize)
Creates a BSON document reader.
|
BsonInputStream(InputStream input,
int expectedMaxDocumentSize,
StringDecoderCache cache)
Creates a BSON document reader.
|
BsonInputStream(InputStream input,
StringDecoderCache cache)
Creates a BSON document reader.
|
Modifier and Type | Method and Description |
---|---|
int |
available() |
protected int |
availableInBuffer()
Returns the number of bytes available in the buffer.
|
void |
close() |
long |
getBytesRead()
Returns the number of bytes that have been read by the stream.
|
int |
getMaxCachedStringEntries()
Deprecated.
The cache
StringDecoderCache should be controlled
directory. This method will be removed after the 2.1.0
release. |
int |
getMaxCachedStringLength()
Deprecated.
The cache
StringDecoderCache should be controlled
directory. This method will be removed after the 2.1.0
release. |
void |
mark(int readlimit) |
boolean |
markSupported() |
void |
prefetch(int size)
Tries to prefetch the requested number of bytes from the underlying
stream.
|
int |
read() |
int |
read(byte[] b) |
int |
read(byte[] b,
int off,
int len) |
protected ArrayElement |
readArrayElement()
Reads a BSON Array element:
|
protected BinaryElement |
readBinaryElement()
Reads a
BinaryElement 's contents:
|
protected BooleanElement |
readBooleanElement()
Reads a
BooleanElement from the stream. |
String |
readCString()
Reads a "cstring" value from the stream:
|
protected Element |
readDBPointerElement()
Deprecated.
Per the BSON specification.
|
Document |
readDocument()
Reads a BSON document element:
|
protected DocumentElement |
readDocumentElement()
Reads a BSON Subdocument element:
|
protected DoubleElement |
readDoubleElement()
Reads a
DoubleElement from the stream. |
protected Element |
readElement(byte token)
Reads the element:
|
protected List<Element> |
readElements()
Reads a BSON element list (e_list):
|
void |
readFully(byte[] buffer)
Reads the complete set of bytes from the stream or throws an
EOFException . |
int |
readInt()
Reads a little-endian 4 byte signed integer from the stream.
|
protected IntegerElement |
readIntegerElement()
Reads a
IntegerElement from the stream. |
protected JavaScriptElement |
readJavaScriptElement()
Reads a
JavaScriptElement from the stream. |
protected JavaScriptWithScopeElement |
readJavaScriptWithScopeElement()
Reads a
JavaScriptWithScopeElement from the stream. |
long |
readLong()
Reads a little-endian 8 byte signed integer from the stream.
|
protected LongElement |
readLongElement()
Reads a
LongElement from the stream. |
protected MaxKeyElement |
readMaxKeyElement()
Reads a
MaxKeyElement from the stream. |
protected MinKeyElement |
readMinKeyElement()
Reads a
MinKeyElement from the stream. |
protected MongoTimestampElement |
readMongoTimestampElement()
Reads a
MongoTimestampElement from the stream. |
protected NullElement |
readNullElement()
Reads a
NullElement from the stream. |
protected ObjectIdElement |
readObjectIdElement()
Reads a
ObjectIdElement from the stream. |
protected RegularExpressionElement |
readRegularExpressionElement()
Reads a
RegularExpressionElement from the stream. |
protected String |
readString()
Reads a "string" value from the stream:
|
protected StringElement |
readStringElement()
Reads a
StringElement from the stream. |
protected SymbolElement |
readSymbolElement()
Reads a
SymbolElement from the stream. |
protected TimestampElement |
readTimestampElement()
Reads a
TimestampElement from the stream. |
void |
reset() |
void |
setMaxCachedStringEntries(int maxCacheEntries)
Deprecated.
The cache
StringDecoderCache should be controlled
directory. This method will be removed after the 2.1.0
release. |
void |
setMaxCachedStringLength(int maxlength)
Deprecated.
The cache
StringDecoderCache should be controlled
directory. This method will be removed after the 2.1.0
release. |
long |
skip(long n) |
public static final Charset UTF8
public BsonInputStream(InputStream input)
input
- the underlying stream to read from.public BsonInputStream(InputStream input, int expectedMaxDocumentSize)
input
- the underlying stream to read from.expectedMaxDocumentSize
- The expected maximum size for a document. If this guess is
wrong then there may be incremental allocations of the read
buffer.public BsonInputStream(InputStream input, int expectedMaxDocumentSize, StringDecoderCache cache)
input
- the underlying stream to read from.expectedMaxDocumentSize
- The expected maximum size for a document. If this guess is
wrong then there may be incremental allocations of the read
buffer.cache
- The cache to use for decoded strings.public BsonInputStream(InputStream input, StringDecoderCache cache)
input
- the underlying stream to read from.cache
- The cache to use for decoded strings.public int available() throws IOException
Overridden to return the number of bytes in the buffer and from the source stream.
available
in class InputStream
IOException
public void close() throws IOException
Overridden to close the wrapped InputStream
.
close
in interface Closeable
close
in interface AutoCloseable
close
in class InputStream
IOException
public long getBytesRead()
@Deprecated public int getMaxCachedStringEntries()
StringDecoderCache
should be controlled
directory. This method will be removed after the 2.1.0
release.@Deprecated public int getMaxCachedStringLength()
StringDecoderCache
should be controlled
directory. This method will be removed after the 2.1.0
release.public void mark(int readlimit)
Overridden to throw an UnsupportedOperationException
.
mark
in class InputStream
public boolean markSupported()
Overridden to return false.
markSupported
in class InputStream
public final void prefetch(int size) throws IOException
size
- The number of bytes to try and read.IOException
- On a failure to read from the underlying stream.public int read() throws IOException
Overridden to track the bytes that have been read.
read
in class InputStream
IOException
public int read(byte[] b) throws IOException
Overridden to track the bytes that have been read.
read
in class InputStream
IOException
public int read(byte[] b, int off, int len) throws IOException
Overridden to track the bytes that have been read.
read
in class InputStream
IOException
public String readCString() throws EOFException, IOException
cstring ::= (byte*) "\x00"
CString - Zero or more modified UTF-8 encoded characters followed by '\x00'. The (byte*) MUST NOT contain '\x00', hence it is not full UTF-8.
EOFException
- On insufficient data for the integer.IOException
- On a failure reading the integer.public Document readDocument() throws IOException
document ::= int32 e_list "\x00"
EOFException
- On insufficient data for the document.IOException
- On a failure reading the document.public void readFully(byte[] buffer) throws EOFException, IOException
EOFException
.buffer
- The buffer into which the data is read.EOFException
- If the input stream reaches the end before reading all the
bytes.IOException
- On an error reading from the underlying stream.public int readInt() throws EOFException, IOException
EOFException
- On insufficient data for the integer.IOException
- On a failure reading the integer.public long readLong() throws EOFException, IOException
EOFException
- On insufficient data for the long.IOException
- On a failure reading the long.public void reset() throws UnsupportedOperationException
Overridden to throw an UnsupportedOperationException
.
reset
in class InputStream
UnsupportedOperationException
@Deprecated public void setMaxCachedStringEntries(int maxCacheEntries)
StringDecoderCache
should be controlled
directory. This method will be removed after the 2.1.0
release.maxCacheEntries
- The new value for the maximum number of strings that may have
their encoded form cached.@Deprecated public void setMaxCachedStringLength(int maxlength)
StringDecoderCache
should be controlled
directory. This method will be removed after the 2.1.0
release.maxlength
- The new value for the length for a string that the encoder is
allowed to cache.public long skip(long n) throws IOException
Overridden to track the bytes that have been skipped.
skip
in class InputStream
IOException
protected final int availableInBuffer()
protected ArrayElement readArrayElement() throws IOException
"\x04" e_name document
ArrayElement
.EOFException
- On insufficient data for the document.IOException
- On a failure reading the document.protected BinaryElement readBinaryElement() throws IOException
BinaryElement
's contents:
binary ::= int32 subtype (byte*)
subtype ::= "\x00" Binary / Generic
| "\x01" Function
| "\x02" Binary (Old)
| "\x03" UUID
| "\x05" MD5
| "\x80" User defined
BinaryElement
.IOException
- On a failure reading the binary data.protected BooleanElement readBooleanElement() throws IOException
BooleanElement
from the stream.BooleanElement
.IOException
- On a failure to read the contents of the
BooleanElement
.@Deprecated protected Element readDBPointerElement() throws IOException
DBPointerElement
from the stream.DBPointerElement
.IOException
- On a failure to read the contents of the
DBPointerElement
.protected DocumentElement readDocumentElement() throws IOException
"\x03" e_name document
ArrayElement
.IOException
- On a failure reading the document.protected DoubleElement readDoubleElement() throws IOException
DoubleElement
from the stream.DoubleElement
.IOException
- On a failure to read the contents of the
DoubleElement
.protected Element readElement(byte token) throws EOFException, IOException
element ::= "\x01" e_name double Floating point
| "\x02" e_name string UTF-8 string
| "\x03" e_name document Embedded document
| "\x04" e_name document Array
| "\x05" e_name binary Binary data
| "\x06" e_name Undefined — Deprecated
| "\x07" e_name (byte*12) ObjectId
| "\x08" e_name "\x00" Boolean "false"
| "\x08" e_name "\x01" Boolean "true"
| "\x09" e_name int64 UTC datetime
| "\x0A" e_name Null value
| "\x0B" e_name cstring cstring Regular expression
| "\x0C" e_name string (byte*12) DBPointer — Deprecated
| "\x0D" e_name string JavaScript code
| "\x0E" e_name string Symbol
| "\x0F" e_name code_w_s JavaScript code w/ scope
| "\x10" e_name int32 32-bit Integer
| "\x11" e_name int64 Timestamp
| "\x12" e_name int64 64-bit integer
| "\xFF" e_name Min key
| "\x7F" e_name Max key
token
- The element's token.EOFException
- On insufficient data for the element.IOException
- On a failure reading the element.protected List<Element> readElements() throws EOFException, IOException
e_list ::= element e_list | ""
EOFException
- On insufficient data for the elements.IOException
- On a failure reading the elements.protected IntegerElement readIntegerElement() throws IOException
IntegerElement
from the stream.IntegerElement
.IOException
- On a failure to read the contents of the
IntegerElement
.protected JavaScriptElement readJavaScriptElement() throws IOException
JavaScriptElement
from the stream.JavaScriptElement
.IOException
- On a failure to read the contents of the
JavaScriptElement
.protected JavaScriptWithScopeElement readJavaScriptWithScopeElement() throws IOException
JavaScriptWithScopeElement
from the stream.JavaScriptWithScopeElement
.IOException
- On a failure to read the contents of the
JavaScriptWithScopeElement
.protected LongElement readLongElement() throws IOException
LongElement
from the stream.LongElement
.IOException
- On a failure to read the contents of the LongElement
.protected MaxKeyElement readMaxKeyElement() throws IOException
MaxKeyElement
from the stream.MaxKeyElement
.IOException
- On a failure to read the contents of the
MaxKeyElement
.protected MinKeyElement readMinKeyElement() throws IOException
MinKeyElement
from the stream.MinKeyElement
.IOException
- On a failure to read the contents of the
MinKeyElement
.protected MongoTimestampElement readMongoTimestampElement() throws IOException
MongoTimestampElement
from the stream.MongoTimestampElement
.IOException
- On a failure to read the contents of the
MongoTimestampElement
.protected NullElement readNullElement() throws IOException
NullElement
from the stream.NullElement
.IOException
- On a failure to read the contents of the NullElement
.protected ObjectIdElement readObjectIdElement() throws IOException
ObjectIdElement
from the stream.ObjectIdElement
.IOException
- On a failure to read the contents of the
ObjectIdElement
.protected RegularExpressionElement readRegularExpressionElement() throws IOException
RegularExpressionElement
from the stream.RegularExpressionElement
.IOException
- On a failure to read the contents of the
RegularExpressionElement
.protected String readString() throws EOFException, IOException
string ::= int32 (byte*) "\x00"
String - The int32 is the number bytes in the (byte*) + 1 (for the trailing '\x00'). The (byte*) is zero or more UTF-8 encoded characters.
EOFException
- On insufficient data for the integer.IOException
- On a failure reading the integer.protected StringElement readStringElement() throws IOException
StringElement
from the stream.StringElement
.IOException
- On a failure to read the contents of the
StringElement
.protected SymbolElement readSymbolElement() throws IOException
SymbolElement
from the stream.SymbolElement
.IOException
- On a failure to read the contents of the
SymbolElement
.protected TimestampElement readTimestampElement() throws IOException
TimestampElement
from the stream.TimestampElement
.IOException
- On a failure to read the contents of the
TimestampElement
.Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.