Package org.bson
Class BasicBSONEncoder
- java.lang.Object
-
- org.bson.BasicBSONEncoder
-
- All Implemented Interfaces:
BSONEncoder
- Direct Known Subclasses:
DefaultDBEncoder
public class BasicBSONEncoder extends Object implements BSONEncoder
This is meant to be pooled or cached. There is some per instance memory for string conversion, etc...
-
-
Constructor Summary
Constructors Constructor Description BasicBSONEncoder()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void_putObjectField(String name, Object initialValue)Encodes any Object typevoiddone()Free the resources.byte[]encode(BSONObject document)Encode a document into byte array.protected BsonBinaryWritergetBsonWriter()Gets the writer responsible for writing the encoded BSON.protected OutputBuffergetOutputBuffer()Gets the buffer the BSON is being encoded into.protected voidputArray(String name, Object object)Encodes an array field.protected voidputBinary(String name, byte[] bytes)Encodes a byte array fieldprotected voidputBinary(String name, Binary binary)Encodes a Binary fieldprotected voidputBoolean(String name, Boolean value)Encodes a field with aBooleanorbooleanvalueprotected voidputCode(String name, Code code)Encodes a field to aBsonType.JAVASCRIPTvalue.protected voidputCodeWScope(String name, CodeWScope codeWScope)Encodes a field to aBsonType.JAVASCRIPT_WITH_SCOPEvalue.protected voidputDate(String name, Date date)Encodes a field with data and time value.protected voidputDecimal128(String name, Decimal128 value)Encodes a Decimal128 field.protected voidputIterable(String name, Iterable iterable)Encodes an Iterable, for exampleListvaluesprotected voidputMap(String name, Map map)Encodes a map, as a BSON documentprotected voidputMaxKey(String name)Encodes a field to aBsonType.MAX_KEYvalue.protected voidputMinKey(String name)Encodes a field to aBsonType.MIN_KEYvalue.protected voidputName(String name)Writes a field nameprotected voidputNull(String name)Encodes a null valueprotected voidputNumber(String name, Number number)Encodes any number field.protected intputObject(String name, BSONObject document)Encodes anyBSONObject, as a documentintputObject(BSONObject document)Encodes aBSONObject.protected voidputObjectId(String name, ObjectId objectId)Encodes an ObjectId field to aBsonType.OBJECT_ID.protected voidputPattern(String name, Pattern value)Encodes a Pattern field to aBsonType.REGULAR_EXPRESSION.protected booleanputSpecial(String name, Object special)Special values are not encoded into documents.protected voidputString(String name, String value)Encodes a String fieldprotected voidputSymbol(String name, Symbol symbol)Encodes a Symbol fieldprotected voidputTimestamp(String name, BSONTimestamp timestamp)Encodes a BSON timestampprotected voidputUndefined(String name)Encodes an undefined valueprotected voidputUUID(String name, UUID uuid)Encodes a field with aUUIDvalue.voidset(OutputBuffer buffer)Sets the buffer to wrich the result of encoding will be written.
-
-
-
Method Detail
-
encode
public byte[] encode(BSONObject document)
Description copied from interface:BSONEncoderEncode a document into byte array. This is a shortcut method which creates a newOutputBuffer, invokes the other 3 methods in a corresponding sequence: and returns the contents of theOutputBuffer.- Specified by:
encodein interfaceBSONEncoder- Parameters:
document- the document to be encoded- Returns:
- a byte sequence
-
done
public void done()
Description copied from interface:BSONEncoderFree the resources.- Specified by:
donein interfaceBSONEncoder
-
set
public void set(OutputBuffer buffer)
Description copied from interface:BSONEncoderSets the buffer to wrich the result of encoding will be written.- Specified by:
setin interfaceBSONEncoder- Parameters:
buffer- the buffer to be used to write a byte sequences to
-
getOutputBuffer
protected OutputBuffer getOutputBuffer()
Gets the buffer the BSON is being encoded into.- Returns:
- the OutputBuffer
-
getBsonWriter
protected BsonBinaryWriter getBsonWriter()
Gets the writer responsible for writing the encoded BSON.- Returns:
- the writer used to write the encoded BSON
-
putObject
public int putObject(BSONObject document)
Encodes aBSONObject. This is for the higher level api calls.- Specified by:
putObjectin interfaceBSONEncoder- Parameters:
document- the document to encode- Returns:
- the number of characters in the encoding
-
putName
protected void putName(String name)
Writes a field name- Parameters:
name- the field name
-
_putObjectField
protected void _putObjectField(String name, Object initialValue)
Encodes any Object type- Parameters:
name- the field nameinitialValue- the value to write
-
putNull
protected void putNull(String name)
Encodes a null value- Parameters:
name- the field name- See Also:
BsonType.NULL
-
putUndefined
protected void putUndefined(String name)
Encodes an undefined value- Parameters:
name- the field name- See Also:
BsonType.UNDEFINED
-
putTimestamp
protected void putTimestamp(String name, BSONTimestamp timestamp)
Encodes a BSON timestamp- Parameters:
name- the field nametimestamp- the timestamp to encode- See Also:
BsonType.TIMESTAMP
-
putCode
protected void putCode(String name, Code code)
Encodes a field to aBsonType.JAVASCRIPTvalue.- Parameters:
name- the field namecode- the value
-
putCodeWScope
protected void putCodeWScope(String name, CodeWScope codeWScope)
Encodes a field to aBsonType.JAVASCRIPT_WITH_SCOPEvalue.- Parameters:
name- the field namecodeWScope- the value
-
putBoolean
protected void putBoolean(String name, Boolean value)
Encodes a field with aBooleanorbooleanvalue- Parameters:
name- the field namevalue- the value
-
putDate
protected void putDate(String name, Date date)
Encodes a field with data and time value.- Parameters:
name- the field namedate- the value- See Also:
BsonType.DATE_TIME
-
putNumber
protected void putNumber(String name, Number number)
Encodes any number field.- Parameters:
name- the field namenumber- the value
-
putDecimal128
protected void putDecimal128(String name, Decimal128 value)
Encodes a Decimal128 field.- Parameters:
name- the field namevalue- the value- Since:
- 3.4
- Since server release
- 3.4
-
putBinary
protected void putBinary(String name, byte[] bytes)
Encodes a byte array field- Parameters:
name- the field namebytes- the value- See Also:
BsonType.BINARY
-
putBinary
protected void putBinary(String name, Binary binary)
Encodes a Binary field- Parameters:
name- the field namebinary- the value- See Also:
BsonType.BINARY
-
putUUID
protected void putUUID(String name, UUID uuid)
Encodes a field with aUUIDvalue. This is encoded to a binary value of subtypeBsonBinarySubType.UUID_LEGACY- Parameters:
name- the field nameuuid- the value
-
putSymbol
protected void putSymbol(String name, Symbol symbol)
Encodes a Symbol field- Parameters:
name- the field namesymbol- the value- See Also:
BsonType.SYMBOL
-
putString
protected void putString(String name, String value)
Encodes a String field- Parameters:
name- the field namevalue- the value- See Also:
BsonType.STRING
-
putPattern
protected void putPattern(String name, Pattern value)
Encodes a Pattern field to aBsonType.REGULAR_EXPRESSION.- Parameters:
name- the field namevalue- the value- See Also:
BsonType.BINARY- MongoDB documentation
- $regex
-
putObjectId
protected void putObjectId(String name, ObjectId objectId)
Encodes an ObjectId field to aBsonType.OBJECT_ID.- Parameters:
name- the field nameobjectId- the value
-
putArray
protected void putArray(String name, Object object)
Encodes an array field.- Parameters:
name- the field nameobject- the array, which can be any sort of primitive or String array
-
putIterable
protected void putIterable(String name, Iterable iterable)
Encodes an Iterable, for exampleListvalues- Parameters:
name- the field nameiterable- the value
-
putMap
protected void putMap(String name, Map map)
Encodes a map, as a BSON document- Parameters:
name- the field namemap- the value
-
putObject
protected int putObject(String name, BSONObject document)
Encodes anyBSONObject, as a document- Parameters:
name- the field namedocument- the value- Returns:
- the number of characters in the encoding
-
putSpecial
protected boolean putSpecial(String name, Object special)
Special values are not encoded into documents.- Parameters:
name- the field namespecial- the value- Returns:
- true if the operation is successful. This implementation always returns false.
-
putMinKey
protected void putMinKey(String name)
Encodes a field to aBsonType.MIN_KEYvalue.- Parameters:
name- the field name
-
putMaxKey
protected void putMaxKey(String name)
Encodes a field to aBsonType.MAX_KEYvalue.- Parameters:
name- the field name
-
-