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
(package private)  ByteBuffer _buf
           
(package private) static SimplePool<ByteEncoder> _pool
           
(package private) static Set<String> BAD_GLOBALS
           
(package private) static boolean DEBUG
           
 
Fields inherited from class com.mongodb.Bytes
_decodingHooks, _encodingHooks, _utf8, ARRAY, B_BINARY, B_FUNC, BINARY, BOOLEAN, CODE, CODE_W_SCOPE, COLLECTION_REF_ID, CONNECTIONS_PER_HOST, D, DATE, EOO, LOGGER, MAX_OBJECT_SIZE, MAX_STRING, MAXKEY, MINKEY, NO_REF_HACK, NULL, NUM_ENCODERS, NUMBER, NUMBER_INT, NUMBER_LONG, OBJECT, OID, ORDER, REF, REGEX, STRING, SYMBOL, TIMESTAMP, UNDEFINED
 
Method Summary
(package private)  boolean _dontRefContains(Object o)
           
(package private)  int _put(String name)
           
(package private)  void _putValueString(String s)
           
static boolean dbOnlyField(Object o)
          Whether a given field is generated by the db for use by the db
static boolean dbOnlyField(String s)
          Whether a given field is generated by the db for use by the db
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 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, cameFromDB, clearAllHooks, getFlag, getType, patternFlags, patternFlags
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG

static final boolean DEBUG

BAD_GLOBALS

static final Set<String> BAD_GLOBALS

_pool

static final SimplePool<ByteEncoder> _pool

_buf

final ByteBuffer _buf
Method Detail

dbOnlyField

public static boolean dbOnlyField(Object o)
Whether a given field is generated by the db for use by the db

Parameters:
o - an object to check
Returns:
whether the object is a db field

dbOnlyField

public static boolean dbOnlyField(String s)
Whether a given field is generated by the db for use by the db

Parameters:
s - a string to check
Returns:
whether the string is a db field

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)

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)

_putValueString

void _putValueString(String s)

_put

int _put(String name)

_dontRefContains

boolean _dontRefContains(Object o)