Package org.bson

Class BasicBSONEncoder

  • All Implemented Interfaces:
    BSONEncoder
    Direct Known Subclasses:
    DefaultDBEncoder


    public class BasicBSONEncoder
    extends java.lang.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​(java.lang.String name, java.lang.Object initialValue)
      Encodes any Object type
      void done​()
      Free the resources.
      byte[] encode​(BSONObject document)
      Encode a document into byte array.
      protected BsonBinaryWriter getBsonWriter​()
      Gets the writer responsible for writing the encoded BSON.
      protected OutputBuffer getOutputBuffer​()
      Gets the buffer the BSON is being encoded into.
      protected void putArray​(java.lang.String name, java.lang.Object object)
      Encodes an array field.
      protected void putBinary​(java.lang.String name, byte[] bytes)
      Encodes a byte array field
      protected void putBinary​(java.lang.String name, Binary binary)
      Encodes a Binary field
      protected void putBoolean​(java.lang.String name, java.lang.Boolean value)
      Encodes a field with a Boolean or boolean value
      protected void putCode​(java.lang.String name, Code code)
      Encodes a field to a BsonType.JAVASCRIPT value.
      protected void putCodeWScope​(java.lang.String name, CodeWScope codeWScope)
      Encodes a field to a BsonType.JAVASCRIPT_WITH_SCOPE value.
      protected void putDate​(java.lang.String name, java.util.Date date)
      Encodes a field with data and time value.
      protected void putDecimal128​(java.lang.String name, Decimal128 value)
      Encodes a Decimal128 field.
      protected void putIterable​(java.lang.String name, java.lang.Iterable iterable)
      Encodes an Iterable, for example List values
      protected void putMap​(java.lang.String name, java.util.Map map)
      Encodes a map, as a BSON document
      protected void putMaxKey​(java.lang.String name)
      Encodes a field to a BsonType.MAX_KEY value.
      protected void putMinKey​(java.lang.String name)
      Encodes a field to a BsonType.MIN_KEY value.
      protected void putName​(java.lang.String name)
      Writes a field name
      protected void putNull​(java.lang.String name)
      Encodes a null value
      protected void putNumber​(java.lang.String name, java.lang.Number number)
      Encodes any number field.
      protected int putObject​(java.lang.String name, BSONObject document)
      Encodes any BSONObject, as a document
      int putObject​(BSONObject document)
      Encodes a BSONObject.
      protected void putObjectId​(java.lang.String name, ObjectId objectId)
      Encodes an ObjectId field to a BsonType.OBJECT_ID.
      protected void putPattern​(java.lang.String name, java.util.regex.Pattern value)
      Encodes a Pattern field to a BsonType.REGULAR_EXPRESSION.
      protected boolean putSpecial​(java.lang.String name, java.lang.Object special)
      Special values are not encoded into documents.
      protected void putString​(java.lang.String name, java.lang.String value)
      Encodes a String field
      protected void putSymbol​(java.lang.String name, Symbol symbol)
      Encodes a Symbol field
      protected void putTimestamp​(java.lang.String name, BSONTimestamp timestamp)
      Encodes a BSON timestamp
      protected void putUndefined​(java.lang.String name)
      Encodes an undefined value
      protected void putUUID​(java.lang.String name, java.util.UUID uuid)
      Encodes a field with a UUID value.
      void set​(OutputBuffer buffer)
      Sets the buffer to wrich the result of encoding will be written.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BasicBSONEncoder

        public BasicBSONEncoder​()
    • Method Detail

      • done

        public void done​()
        Description copied from interface: BSONEncoder
        Free the resources.
        Specified by:
        done in interface BSONEncoder
      • set

        public void set​(OutputBuffer buffer)
        Description copied from interface: BSONEncoder
        Sets the buffer to wrich the result of encoding will be written.
        Specified by:
        set in interface BSONEncoder
        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 a BSONObject. This is for the higher level api calls.
        Specified by:
        putObject in interface BSONEncoder
        Parameters:
        document - the document to encode
        Returns:
        the number of characters in the encoding
      • putName

        protected void putName​(java.lang.String name)
        Writes a field name
        Parameters:
        name - the field name
      • _putObjectField

        protected void _putObjectField​(java.lang.String name,
                                       java.lang.Object initialValue)
        Encodes any Object type
        Parameters:
        name - the field name
        initialValue - the value to write
      • putNull

        protected void putNull​(java.lang.String name)
        Encodes a null value
        Parameters:
        name - the field name
        See Also:
        BsonType.NULL
      • putUndefined

        protected void putUndefined​(java.lang.String name)
        Encodes an undefined value
        Parameters:
        name - the field name
        See Also:
        BsonType.UNDEFINED
      • putTimestamp

        protected void putTimestamp​(java.lang.String name,
                                    BSONTimestamp timestamp)
        Encodes a BSON timestamp
        Parameters:
        name - the field name
        timestamp - the timestamp to encode
        See Also:
        BsonType.TIMESTAMP
      • putCode

        protected void putCode​(java.lang.String name,
                               Code code)
        Encodes a field to a BsonType.JAVASCRIPT value.
        Parameters:
        name - the field name
        code - the value
      • putCodeWScope

        protected void putCodeWScope​(java.lang.String name,
                                     CodeWScope codeWScope)
        Encodes a field to a BsonType.JAVASCRIPT_WITH_SCOPE value.
        Parameters:
        name - the field name
        codeWScope - the value
      • putBoolean

        protected void putBoolean​(java.lang.String name,
                                  java.lang.Boolean value)
        Encodes a field with a Boolean or boolean value
        Parameters:
        name - the field name
        value - the value
      • putDate

        protected void putDate​(java.lang.String name,
                               java.util.Date date)
        Encodes a field with data and time value.
        Parameters:
        name - the field name
        date - the value
        See Also:
        BsonType.DATE_TIME
      • putNumber

        protected void putNumber​(java.lang.String name,
                                 java.lang.Number number)
        Encodes any number field.
        Parameters:
        name - the field name
        number - the value
      • putDecimal128

        protected void putDecimal128​(java.lang.String name,
                                     Decimal128 value)
        Encodes a Decimal128 field.
        Parameters:
        name - the field name
        value - the value
        Since:
        3.4
        Since server release
        3.4
      • putBinary

        protected void putBinary​(java.lang.String name,
                                 byte[] bytes)
        Encodes a byte array field
        Parameters:
        name - the field name
        bytes - the value
        See Also:
        BsonType.BINARY
      • putBinary

        protected void putBinary​(java.lang.String name,
                                 Binary binary)
        Encodes a Binary field
        Parameters:
        name - the field name
        binary - the value
        See Also:
        BsonType.BINARY
      • putUUID

        protected void putUUID​(java.lang.String name,
                               java.util.UUID uuid)
        Encodes a field with a UUID value. This is encoded to a binary value of subtype BsonBinarySubType.UUID_LEGACY
        Parameters:
        name - the field name
        uuid - the value
      • putSymbol

        protected void putSymbol​(java.lang.String name,
                                 Symbol symbol)
        Encodes a Symbol field
        Parameters:
        name - the field name
        symbol - the value
        See Also:
        BsonType.SYMBOL
      • putString

        protected void putString​(java.lang.String name,
                                 java.lang.String value)
        Encodes a String field
        Parameters:
        name - the field name
        value - the value
        See Also:
        BsonType.STRING
      • putPattern

        protected void putPattern​(java.lang.String name,
                                  java.util.regex.Pattern value)
        Encodes a Pattern field to a BsonType.REGULAR_EXPRESSION.
        Parameters:
        name - the field name
        value - the value
        See Also:
        BsonType.BINARY
        MongoDB documentation
        $regex
      • putObjectId

        protected void putObjectId​(java.lang.String name,
                                   ObjectId objectId)
        Encodes an ObjectId field to a BsonType.OBJECT_ID.
        Parameters:
        name - the field name
        objectId - the value
      • putArray

        protected void putArray​(java.lang.String name,
                                java.lang.Object object)
        Encodes an array field.
        Parameters:
        name - the field name
        object - the array, which can be any sort of primitive or String array
      • putIterable

        protected void putIterable​(java.lang.String name,
                                   java.lang.Iterable iterable)
        Encodes an Iterable, for example List values
        Parameters:
        name - the field name
        iterable - the value
      • putMap

        protected void putMap​(java.lang.String name,
                              java.util.Map map)
        Encodes a map, as a BSON document
        Parameters:
        name - the field name
        map - the value
      • putObject

        protected int putObject​(java.lang.String name,
                                BSONObject document)
        Encodes any BSONObject, as a document
        Parameters:
        name - the field name
        document - the value
        Returns:
        the number of characters in the encoding
      • putSpecial

        protected boolean putSpecial​(java.lang.String name,
                                     java.lang.Object special)
        Special values are not encoded into documents.
        Parameters:
        name - the field name
        special - the value
        Returns:
        true if the operation is successful. This implementation always returns false.
      • putMinKey

        protected void putMinKey​(java.lang.String name)
        Encodes a field to a BsonType.MIN_KEY value.
        Parameters:
        name - the field name
      • putMaxKey

        protected void putMaxKey​(java.lang.String name)
        Encodes a field to a BsonType.MAX_KEY value.
        Parameters:
        name - the field name