/** * Puts a character sequence into the buffer at the current * write position and increases the write position accordingly. * @param s the character sequence to put */ public void putString(CharSequence s) { putString(_position, s); _position += (s.length() * 2); }
/** * Puts a character sequence into the buffer at the current * write position and increases the write position accordingly. * @param s the character sequence to put */ public void putString(CharSequence s) { putString(_position, s); _position += (s.length() * 2); }
@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(); }
@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(); }
@Test public void putString() throws Exception { DynamicOutputBuffer db = new DynamicOutputBuffer(2); db.putString("Hello"); assertEquals(10, db.size()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); db.writeTo(baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ReadableByteChannel channel = Channels.newChannel(bais); ByteBuffer buf = ByteBuffer.allocate(10); channel.read(buf); buf.flip(); CharBuffer cbuf = buf.asCharBuffer(); char[] c = new char[5]; cbuf.get(c); String s = String.valueOf(c); assertEquals("Hello", s); }