/** * 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; }
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); }
/** * Creates a new embedded document or array * @param array true if the embedded object is an array * @throws IOException if the document could not be created */ protected void _writeStartObject(boolean array) throws IOException { _writeArrayFieldNameIfNeeded(); if (_currentDocument != null) { //embedded document/array _buffer.putByte(_typeMarker, array ? BsonConstants.TYPE_ARRAY : BsonConstants.TYPE_DOCUMENT); } _currentDocument = new DocumentInfo(_currentDocument, _buffer.size(), array); reserveHeader(); }
/** * Creates a new embedded document or array * @param array true if the embedded object is an array * @throws IOException if the document could not be created */ protected void _writeStartObject(boolean array) throws IOException { _writeArrayFieldNameIfNeeded(); if (_currentDocument != null) { //embedded document/array _buffer.putByte(_typeMarker, array ? BsonConstants.TYPE_ARRAY : BsonConstants.TYPE_DOCUMENT); } _currentDocument = new DocumentInfo(_currentDocument, _buffer.size(), array); reserveHeader(); }
@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); }
@Override public void writeStartObject() throws IOException, JsonGenerationException { if (nextObjectIsEmbeddedInValue) { _writeContext = _writeContext.createChildObjectContext(); _currentDocument = new DocumentInfo(_currentDocument, _buffer.size(), false); reserveHeader(); // We've skipped everything we need to skip, the next object may not be embedded in a value nextObjectIsEmbeddedInValue = false; } else { _verifyValueWrite("start an object"); _writeContext = _writeContext.createChildObjectContext(); _writeStartObject(false); } }
@Override public void writeStartObject() throws IOException, JsonGenerationException { if (nextObjectIsEmbeddedInValue) { _writeContext = _writeContext.createChildObjectContext(); _currentDocument = new DocumentInfo(_currentDocument, _buffer.size(), false); reserveHeader(); // We've skipped everything we need to skip, the next object may not be embedded in a value nextObjectIsEmbeddedInValue = false; } else { _verifyValueWrite("start an object"); _writeContext = _writeContext.createChildObjectContext(); _writeStartObject(false); } }
@Test public void putByteLittle() throws Exception { DynamicOutputBuffer db = new DynamicOutputBuffer(ByteOrder.LITTLE_ENDIAN, 2); assertEquals(0, db.size()); db.putByte((byte)0xA0); assertEquals(1, db.size()); db.putByte((byte)0xB0); assertEquals(2, db.size()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); db.writeTo(baos); byte[] r = baos.toByteArray(); assertEquals((byte)0xA0, r[0]); assertEquals((byte)0xB0, r[1]); }
@Test public void putByteBig() throws Exception { DynamicOutputBuffer db = new DynamicOutputBuffer(2); assertEquals(0, db.size()); db.putByte((byte)0xA0); assertEquals(1, db.size()); db.putByte((byte)0xB0); assertEquals(2, db.size()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); db.writeTo(baos); byte[] r = baos.toByteArray(); assertEquals((byte)0xA0, r[0]); assertEquals((byte)0xB0, r[1]); }
@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(); }
@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(); }
@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)); }