com.mongodb
Class ByteEncoder

java.lang.Object
  extended by com.mongodb.Bytes
      extended by com.mongodb.ByteEncoder

public class ByteEncoder
extends Bytes

Serializes a DBObject into a string that can be sent to the database.

There is a pool of available encoders. Create a new one as follows:

ByteEncoder encoder = ByteEncoder.get(); // try forever until an encoder is available

Each key/value pair in the DBObject is encoded in the following format:

<type (byte)><name (String)><0 (byte)><data (serialized data)>
For example:
<NUMBER><name>0<double> // NUMBER = 1
<STRING><name>0<len><string>0 // STRING = 2


Field Summary
 
Fields inherited from class com.mongodb.Bytes
_utf8, MAX_STRING, ORDER, QUERYOPTION_NOTIMEOUT, QUERYOPTION_OPLOGREPLAY, QUERYOPTION_SLAVEOK, QUERYOPTION_TAILABLE
 
Method Summary
protected  void done()
          Resets and returns this encoder to the pool.
protected  void flip()
          Switches the encoder from being write-only to being read-only.
static ByteEncoder get()
          Fetches a new ByteEncoder from the pool of available ByteEncoders.
 byte[] getBytes()
          Returns the bytes in the bytebuffer.
protected  void putBinary(String name, byte[] data)
           
protected  void putBinary(String name, DBBinary val)
           
protected  int putBoolean(String name, Boolean b)
           
protected  int putCodeWScope(String name, CodeWScope code)
           
protected  int putDate(String name, Date d)
           
protected  int putDBPointer(String name, String ns, ObjectId oid)
           
protected  void putDBRef(String name, DBRefBase ref)
           
protected  int putNull(String name)
           
protected  int putNumber(String name, Number n)
           
 int putObject(DBObject o)
          Encodes a DBObject.
protected  int putObjectId(String name, ObjectId oid)
           
protected  int putString(String name, String s)
           
protected  int putSymbol(String name, DBSymbol s)
           
protected  int putTimestamp(String name, DBTimestamp ts)
           
protected  int putUndefined(String name)
           
protected  void reset()
          Returns encoder to its starting state, ready to encode an object.
 
Methods inherited from class com.mongodb.Bytes
addDecodingHook, addEncodingHook, applyDecodingHooks, applyEncodingHooks, clearAllHooks, decode, encode, getFlag, getType, patternFlags, patternFlags
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

get

public static ByteEncoder get()
Fetches a new ByteEncoder from the pool of available ByteEncoders.

Returns:
a new ByteEncoder

done

protected void done()
Resets and returns this encoder to the pool.


getBytes

public byte[] getBytes()
Returns the bytes in the bytebuffer. Attempts to leave the bytebuffer in the same state. Note that mark, if set, is lost.

Returns:
array of bytes

reset

protected void reset()
Returns encoder to its starting state, ready to encode an object.


flip

protected void flip()
Switches the encoder from being write-only to being read-only.


putObject

public int putObject(DBObject o)
Encodes a DBObject. This is for the higher level api calls

Parameters:
o - the object to encode
Returns:
the number of characters in the encoding

putNull

protected int putNull(String name)

putUndefined

protected int putUndefined(String name)

putTimestamp

protected int putTimestamp(String name,
                           DBTimestamp ts)

putCodeWScope

protected int putCodeWScope(String name,
                            CodeWScope code)

putBoolean

protected int putBoolean(String name,
                         Boolean b)

putDate

protected int putDate(String name,
                      Date d)

putNumber

protected int putNumber(String name,
                        Number n)

putBinary

protected void putBinary(String name,
                         byte[] data)

putBinary

protected void putBinary(String name,
                         DBBinary val)

putSymbol

protected int putSymbol(String name,
                        DBSymbol s)

putString

protected int putString(String name,
                        String s)

putObjectId

protected int putObjectId(String name,
                          ObjectId oid)

putDBPointer

protected int putDBPointer(String name,
                           String ns,
                           ObjectId oid)

putDBRef

protected void putDBRef(String name,
                        DBRefBase ref)