/** * Tries to copy as much bytes as possible from this buffer to * the given channel. See {@link #flushTo(WritableByteChannel)} * for further information. * @param out the output stream to write to * @throws IOException if the buffer could not be flushed */ public void flushTo(OutputStream out) throws IOException { int n1 = _flushPosition / _bufferSize; int n2 = _position / _bufferSize; if (n1 < n2) { flushTo(Channels.newChannel(out)); } }
@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(); }
/** * Puts a 64-bit floating point number into the buffer at the current * write position and increases the write position accordingly. * @param d the double to put */ public void putDouble(double d) { putDouble(_position, d); _position += 8; }
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 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(); }
/** * Similar to {@link #writeBinary(Base64Variant, byte, byte[], int, int)}, * but with the possibility to specify a binary subtype (see * {@link BsonConstants}). * @param b64variant base64 variant to use (will be ignored for BSON) * @param subType the binary subtype * @param data the binary data to write * @param offset the offset of the first byte to write * @param len the number of bytes to write * @throws IOException if the binary data could not be written */ public void writeBinary(Base64Variant b64variant, byte subType, byte[] data, int offset, int len) throws IOException { //base64 is not needed for BSON _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write binary"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_BINARY); _buffer.putInt(len); _buffer.putByte(subType); int end = offset + len; if (end > data.length) { end = data.length; } while (offset < end) { _buffer.putByte(data[offset]); ++offset; } flushBuffer(); }
/** * Write a BSON date time * * @param date The date to write * @throws IOException If an error occurred in the stream while writing */ public void writeDateTime(Date date) throws IOException { _writeArrayFieldNameIfNeeded(); _verifyValueWrite("write datetime"); _buffer.putByte(_typeMarker, BsonConstants.TYPE_DATETIME); _buffer.putLong(date.getTime()); flushBuffer(); }
@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 putInt32Big() throws Exception { DynamicOutputBuffer db = new DynamicOutputBuffer(2); assertEquals(0, db.size()); db.putInt(0xAABBCCDD); assertEquals(4, db.size()); db.writeTo(baos); byte[] r = baos.toByteArray(); assertEquals((byte)0xAA, r[0]); db = new DynamicOutputBuffer(40); assertEquals(0, db.size()); db.putInt(0xAABBCCDD); assertEquals(4, db.size()); db.writeTo(baos); r = baos.toByteArray(); assertEquals((byte)0xAA, r[0]);
@Test public void putInt64Big() throws Exception { DynamicOutputBuffer db = new DynamicOutputBuffer(2); assertEquals(0, db.size()); db.putLong(0x66778899AABBCCDDL); assertEquals(8, db.size()); db.writeTo(baos); byte[] r = baos.toByteArray(); assertEquals((byte)0x66, r[0]); db = new DynamicOutputBuffer(40); assertEquals(0, db.size()); db.putLong(0x66778899AABBCCDDL); assertEquals(8, db.size()); db.writeTo(baos); r = baos.toByteArray(); assertEquals((byte)0x66, r[0]);
@Test public void putFloatingPoint() throws Exception { DynamicOutputBuffer db = new DynamicOutputBuffer(2); db.putFloat(1234.1234f); db.putDouble(5678.5678); ByteArrayOutputStream baos = new ByteArrayOutputStream(); db.writeTo(baos); byte[] r = baos.toByteArray(); ByteBuffer bb = ByteBuffer.wrap(r); assertEquals(1234.1234f, bb.getFloat(), 0.00001); assertEquals(5678.5678, bb.getDouble(), 0.00001); }
@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); }
@Test public void flush() throws Exception { DynamicOutputBuffer db = new DynamicOutputBuffer(2); db.putByte((byte)1); db.putByte((byte)2); db.putByte((byte)3); db.putByte((byte)4); db.putByte((byte)5); db.flushTo(baos); byte[] r = baos.toByteArray(); assertEquals(4, r.length); assertEquals(4, r[3]); db.putByte((byte)6); db.flushTo(baos); r = baos.toByteArray(); assertEquals(6, r.length); assertEquals(6, r[5]); db.putByte((byte)7); db.writeTo(baos); r = baos.toByteArray(); assertEquals(7, r.length);
ByteBuffer minibb = null; CharsetEncoder enc = getUTF8Encoder(); CharBuffer in = CharBuffer.wrap(s); ByteBuffer bb = getBuffer(pos2); int index = pos2 % _bufferSize; bb.position(index); bb.flip(); while (bb.remaining() > 0) { putByte(pos2, bb.get()); ++pos2; index = 0; } else { bb = getBuffer(pos2); index = pos2 % _bufferSize; bb.position(index); adaptSize(pos2); return pos2 - pos;
@Test public void putUTF8() throws Exception { DynamicOutputBuffer db = new DynamicOutputBuffer(2); int w = db.putUTF8("Hello"); assertEquals(5, w); assertEquals(5, db.size()); db = new DynamicOutputBuffer(10); w = db.putUTF8("Hello"); assertEquals(5, w); assertEquals(5, db.size()); db.writeTo(baos); String s = new String(baos.toByteArray()); assertEquals("Hello", s); db = new DynamicOutputBuffer(2); w = db.putUTF8("a\u20AC\u00A2\u00A2bb"); assertEquals(10, w); assertEquals(10, db.size()); db.writeTo(baos); byte[] r = baos.toByteArray(); assertEquals('a', r[0]);
/** * 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; }
@Test(expected = java.lang.IndexOutOfBoundsException.class) public void putAfterWrite() throws Exception { DynamicOutputBuffer db = new DynamicOutputBuffer(2); db.putByte((byte)1); ByteArrayOutputStream baos = new ByteArrayOutputStream(); db.writeTo(baos); db.putByte((byte)8); }
@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)); } }