/** * Puts a byte into the buffer at the current write position * and increases the write position accordingly. * @param b the byte to put */ public void putByte(byte b) { putByte(_position, b); ++_position; }
/** * Puts a byte into the buffer at the current write position * and increases the write position accordingly. * @param b the byte to put */ public void putByte(byte b) { putByte(_position, b); ++_position; }
protected void _writeFieldName(String name) throws IOException, JsonGenerationException { //escape characters if necessary name = escapeCharacters(name); //reserve bytes for the type _typeMarker = _buffer.size(); _buffer.putByte((byte)0); //write field name _buffer.putUTF8(name); _buffer.putByte(BsonConstants.END_OF_STRING); }
protected void _writeFieldName(String name) throws IOException, JsonGenerationException { //escape characters if necessary name = escapeCharacters(name); //reserve bytes for the type _typeMarker = _buffer.size(); _buffer.putByte((byte)0); //write field name _buffer.putUTF8(name); _buffer.putByte(BsonConstants.END_OF_STRING); }
@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 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 writeNull() throws IOException, JsonGenerationException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write null"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_NULL); flushBuffer(); }
@Override public void writeNumber(double d) throws IOException, JsonGenerationException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write number"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_DOUBLE); _buffer.putDouble(d); flushBuffer(); }
@Override public void writeNumber(double d) throws IOException, JsonGenerationException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write number"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_DOUBLE); _buffer.putDouble(d); flushBuffer(); }
@Override public void writeString(String text) throws IOException, JsonGenerationException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write string"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_STRING); _writeString(text); 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(long v) throws IOException, JsonGenerationException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write number"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_INT64); _buffer.putLong(v); flushBuffer(); }
@Override public void writeString(String text) throws IOException, JsonGenerationException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write string"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_STRING); _writeString(text); 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(long v) throws IOException, JsonGenerationException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write number"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_INT64); _buffer.putLong(v); flushBuffer(); }
@Test public void readZeroTerminatedUTF() throws Exception { DynamicOutputBuffer db = new DynamicOutputBuffer(20); db.putUTF8("Hello"); db.putByte((byte)0); LittleEndianInputStream leis = outputBufferToInputStream(db); assertEquals("Hello", leis.readUTF(-1)); } }