/** * Writes all non-flushed internal buffers to the given output * stream. If {@link #flushTo(OutputStream)} has not been called * before, this method writes the whole buffer to the output stream. * @param out the output stream to write to * @throws IOException if the buffer could not be written */ public void writeTo(OutputStream out) throws IOException { writeTo(Channels.newChannel(out)); }
/** * Writes all non-flushed internal buffers to the given output * stream. If {@link #flushTo(OutputStream)} has not been called * before, this method writes the whole buffer to the output stream. * @param out the output stream to write to * @throws IOException if the buffer could not be written */ public void writeTo(OutputStream out) throws IOException { writeTo(Channels.newChannel(out)); }
@Override public void flush() throws IOException { //we must not flush the buffer if we are currently writing a document //otherwise we cannot write the document size to the header at the end if (_currentDocument == null) { _buffer.writeTo(_out); _buffer.clear(); } _out.flush(); }
@Override public void flush() throws IOException { //we must not flush the buffer if we are currently writing a document //otherwise we cannot write the document size to the header at the end if (_currentDocument == null) { _buffer.writeTo(_out); _buffer.clear(); } _out.flush(); }
private LittleEndianInputStream outputBufferToInputStream(DynamicOutputBuffer db) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); db.writeTo(baos); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); return new LittleEndianInputStream(bais); }
@Override public void close() throws IOException { //finish document if (isEnabled(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT)) { while (_currentDocument != null) { writeEndObject(); } } //write buffer to output stream (if streaming is enabled, //this will write the the rest of the buffer) _buffer.writeTo(_out); _buffer.clear(); _out.flush(); if (isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)) { _out.close(); } super.close(); }
@Override public void close() throws IOException { //finish document if (isEnabled(JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT)) { while (_currentDocument != null) { writeEndObject(); } } //write buffer to output stream (if streaming is enabled, //this will write the the rest of the buffer) _buffer.writeTo(_out); _buffer.clear(); _out.flush(); if (isEnabled(JsonGenerator.Feature.AUTO_CLOSE_TARGET)) { _out.close(); } super.close(); }
@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(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 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]); }
@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 putRandom() throws Exception { DynamicOutputBuffer db = new DynamicOutputBuffer(2); db.putByte(5, (byte)0); db.putByte(1, (byte)1); db.putByte(4, (byte)2); db.putByte(3, (byte)3); db.putByte(2, (byte)4); ByteArrayOutputStream baos = new ByteArrayOutputStream(); db.writeTo(baos); byte[] r = baos.toByteArray(); assertEquals(0, r[0]); assertEquals(0, r[5]); assertEquals(1, r[1]); assertEquals(2, r[4]); assertEquals(3, r[3]); assertEquals(4, r[2]); }
db.writeTo(baos); r = baos.toByteArray(); assertEquals(7, r.length);