/** * Encodes the given string as UTF-8, puts it into the buffer * and increases the write position accordingly. * @param s the string to put * @return the number of UTF-8 bytes put */ public int putUTF8(String s) { int written = putUTF8(_position, s); _position += written; return written; }
/** * Encodes the given string as UTF-8, puts it into the buffer * and increases the write position accordingly. * @param s the string to put * @return the number of UTF-8 bytes put */ public int putUTF8(String s) { int written = putUTF8(_position, s); _position += written; return written; }
/** * Write a BSON cstring structure (a null terminated string) * * @param string The string to write * @return The number of bytes written, including the terminating null byte * @throws IOException If an error occurred in the stream while writing */ protected int _writeCString(String string) throws IOException { //escape characters if necessary string = escapeCharacters(string); int l = _buffer.putUTF8(string); _buffer.putByte(BsonConstants.END_OF_STRING); return l + 1; }
/** * Write a BSON cstring structure (a null terminated string) * * @param string The string to write * @return The number of bytes written, including the terminating null byte * @throws IOException If an error occurred in the stream while writing */ protected int _writeCString(String string) throws IOException { //escape characters if necessary string = escapeCharacters(string); int l = _buffer.putUTF8(string); _buffer.putByte(BsonConstants.END_OF_STRING); return l + 1; }
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); }
/** * Test if the internal mini buffer is correctly cleared on overflow. If * not, this test will cause an infinite loop. See issue #79. */ @Test(timeout = 5000) public void bufferAllocOnOverflow() { final int SIZE = DynamicOutputBuffer.DEFAULT_BUFFER_SIZE - 1; char[] chars = new char[SIZE + SIZE]; Arrays.fill(chars, 0, SIZE, 'A'); chars[SIZE] = (char)2047; // represented with 2 bytes in UTF8 chars[SIZE + 1] = (char)2048; // represented with 3 bytes in UTF8 Arrays.fill(chars, SIZE + 2, 2 * SIZE - 2, 'B'); chars[2 * SIZE - 2] = (char)2048; chars[2 * SIZE - 1] = (char)2048; DynamicOutputBuffer buff = new DynamicOutputBuffer(); int nbytes = buff.putUTF8(String.valueOf(chars)); // sizeof(bytes) - sizeof(chars) should be 7 because 2047 adds 1 extra // byte and 3 chars of 2048 add 6 extra bytes in total assertEquals("Unexpected number of written bytes", chars.length + 7, nbytes); } }
@Test(expected = CharacterCodingException.class) public void readUTFError() throws Exception { DynamicOutputBuffer db = new DynamicOutputBuffer(20); db.putUTF8("a\u00A2"); LittleEndianInputStream leis = outputBufferToInputStream(db); leis.readUTF(2); }
@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)); } }
@Test public void readUTF() throws Exception { DynamicOutputBuffer db = new DynamicOutputBuffer(20); db.putUTF8("Helloa\u20AC\u00A2\u00A2bb"); LittleEndianInputStream leis = outputBufferToInputStream(db); assertEquals("Hello", leis.readUTF(5)); assertEquals("a\u20AC\u00A2\u00A2bb", leis.readUTF(db.size() - 5)); }
@Test public void putUTF8() throws Exception { DynamicOutputBuffer db = new DynamicOutputBuffer(2); int w = db.putUTF8("Hello"); assertEquals(5, w); assertEquals(5, db.size()); w = db.putUTF8("Hello"); assertEquals(5, w); assertEquals(5, db.size()); w = db.putUTF8("a\u20AC\u00A2\u00A2bb"); assertEquals(10, w); assertEquals(10, db.size());