/** * Reserves bytes for the BSON document header */ protected void reserveHeader() { _buffer.putInt(0); }
/** * Puts a 32-bit integer into the buffer at the current write position * and increases write position accordingly. * @param i the integer to put */ public void putInt(int i) { putInt(_position, i); _position += 4; }
/** * Reserves bytes for the BSON document header */ protected void reserveHeader() { _buffer.putInt(0); }
/** * Puts a 32-bit integer into the buffer at the current write position * and increases write position accordingly. * @param i the integer to put */ public void putInt(int i) { putInt(_position, i); _position += 4; }
/** * Puts a 32-bit floating point number into the buffer at the given * position. Does not increase the write position. * @param pos the position where to put the float * @param f the float to put */ public void putFloat(int pos, float f) { putInt(pos, Float.floatToRawIntBits(f)); }
/** * Puts a 32-bit floating point number into the buffer at the given * position. Does not increase the write position. * @param pos the position where to put the float * @param f the float to put */ public void putFloat(int pos, float f) { putInt(pos, Float.floatToRawIntBits(f)); }
/** * Writes the BSON document header to the output buffer at the * given position. Does not increase the buffer's write position. * @param pos the position where to write the header */ protected void putHeader(int pos) { _buffer.putInt(pos, _buffer.size() - pos); }
/** * Writes the BSON document header to the output buffer at the * given position. Does not increase the buffer's write position. * @param pos the position where to write the header */ protected void putHeader(int pos) { _buffer.putInt(pos, _buffer.size() - pos); }
/** * Write a BSON string structure (a null terminated string prependend by the length of the string) * * @param string The string to write * @return The number of bytes written, including the terminating null byte and the size of the string * @throws IOException If an error occurred in the stream while writing */ protected int _writeString(String string) throws IOException { //reserve space for the string size int p = _buffer.size(); _buffer.putInt(0); //write string int l = _writeCString(string); //write string size _buffer.putInt(p, l); return l + 4; }
/** * Write a BSON string structure (a null terminated string prependend by the length of the string) * * @param string The string to write * @return The number of bytes written, including the terminating null byte and the size of the string * @throws IOException If an error occurred in the stream while writing */ protected int _writeString(String string) throws IOException { //reserve space for the string size int p = _buffer.size(); _buffer.putInt(0); //write string int l = _writeCString(string); //write string size _buffer.putInt(p, l); return l + 4; }
/** * Write a MongoDB timestamp * * @param timestamp The timestamp to write * @throws IOException If an error occurred in the stream while writing */ public void writeTimestamp(Timestamp timestamp) throws IOException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write timestamp"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_TIMESTAMP); _buffer.putInt(timestamp.getInc()); _buffer.putInt(timestamp.getTime()); flushBuffer(); }
/** * Write a MongoDB timestamp * * @param timestamp The timestamp to write * @throws IOException If an error occurred in the stream while writing */ public void writeTimestamp(Timestamp timestamp) throws IOException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write timestamp"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_TIMESTAMP); _buffer.putInt(timestamp.getInc()); _buffer.putInt(timestamp.getTime()); flushBuffer(); }
@Override public void writeNumber(int v) throws IOException, JsonGenerationException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write number"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_INT32); _buffer.putInt(v); flushBuffer(); }
@Override public void writeNumber(int v) throws IOException, JsonGenerationException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write number"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_INT32); _buffer.putInt(v); flushBuffer(); }
@Override public void writeRaw(char[] text, int offset, int len) throws IOException, JsonGenerationException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write raw string"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_BINARY); _buffer.putInt(text.length * 2); _buffer.putByte(BsonConstants.SUBTYPE_BINARY); _buffer.putString(CharBuffer.wrap(text)); flushBuffer(); }
@Override public void writeRaw(char[] text, int offset, int len) throws IOException, JsonGenerationException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write raw string"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_BINARY); _buffer.putInt(text.length * 2); _buffer.putByte(BsonConstants.SUBTYPE_BINARY); _buffer.putString(CharBuffer.wrap(text)); flushBuffer(); }
/** * Write a BSON ObjectId * * @param objectId The objectId to write * @throws IOException If an error occurred in the stream while writing */ public void writeObjectId(ObjectId objectId) throws IOException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write datetime"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_OBJECTID); // ObjectIds have their byte order flipped int time = ByteOrderUtil.flip(objectId.getTime()); int machine = ByteOrderUtil.flip(objectId.getMachine()); int inc = ByteOrderUtil.flip(objectId.getInc()); _buffer.putInt(time); _buffer.putInt(machine); _buffer.putInt(inc); flushBuffer(); }
@Override public void writeRaw(String text) throws IOException, JsonGenerationException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write raw string"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_BINARY); _buffer.putInt(text.length() * 2); _buffer.putByte(BsonConstants.SUBTYPE_BINARY); _buffer.putString(text); flushBuffer(); }
@Override public void writeRaw(String text) throws IOException, JsonGenerationException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write raw string"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_BINARY); _buffer.putInt(text.length() * 2); _buffer.putByte(BsonConstants.SUBTYPE_BINARY); _buffer.putString(text); flushBuffer(); }
@Override public void writeRawUTF8String(byte[] text, int offset, int length) throws IOException, JsonGenerationException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write raw utf8 string"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_STRING); //reserve space for the string size int p = _buffer.size(); _buffer.putInt(0); //write string for (int i = offset; i < length; ++i) { _buffer.putByte(text[i]); } _buffer.putByte(BsonConstants.END_OF_STRING); //write string size _buffer.putInt(p, length); flushBuffer(); }