public synchronized void write (Connection connection, ByteBuffer buffer, Object object) { byteBufferOutputStream.setByteBuffer(buffer); int start = buffer.position(); try { json.writeValue(object, Object.class, null); writer.flush(); } catch (Exception ex) { throw new JsonException("Error writing object: " + object, ex); } if (INFO && logging) { int end = buffer.position(); buffer.position(start); buffer.limit(end); int length = end - start; if (logBuffer.length < length) logBuffer = new byte[length]; buffer.get(logBuffer, 0, length); buffer.position(end); buffer.limit(buffer.capacity()); String message = new String(logBuffer, 0, length); if (prettyPrint) message = json.prettyPrint(message); info("Wrote: " + message); } }
/** * Flushes the stream by taking the current {@link ByteBuffer} using for writing. Buffer is flip * and put to the list of cached buffers. If the takeNewBuffer is <code>true</code> then a new * buffer will be placed and ready for write. * * @param takeNewBuffer * If the new buffers should be placed the super {@link OutputStream}. * @exception IOException * if an I/O error occurs. */ public void flush(boolean takeNewBuffer) throws IOException { ByteBuffer fullBuffer = super.getByteBuffer(); fullBuffer.flip(); byteBuffers.add(fullBuffer); totalWriteSize += fullBuffer.limit() - fullBuffer.position(); if (takeNewBuffer) { ByteBuffer byteBuffer = byteBufferProvider.acquireByteBuffer(); byteBuffer.clear(); super.setByteBuffer(byteBuffer); } else { super.setByteBuffer(null); } }
/** * Prepares for the write. This method will get a {@link ByteBuffer} from the * {@link ByteBufferProvider} and set it as the current output. * * @throws IOException * if preparation fails due to inability to obtain a byte buffers */ public void prepare() throws IOException { releaseAllBuffers(); totalWriteSize = 0L; ByteBuffer byteBuffer = byteBufferProvider.acquireByteBuffer(); byteBuffer.clear(); super.setByteBuffer(byteBuffer); }
public void write (Connection connection, ByteBuffer buffer, Object object) { byteBufferOutputStream.setByteBuffer(buffer); int start = buffer.position(); try { json.writeValue(object, Object.class, null); writer.flush(); } catch (Exception ex) { throw new JsonException("Error writing object: " + object, ex); } if (INFO && logging) { int end = buffer.position(); buffer.position(start); buffer.limit(end); int length = end - start; if (logBuffer.length < length) logBuffer = new byte[length]; buffer.get(logBuffer, 0, length); buffer.position(end); buffer.limit(buffer.capacity()); String message = new String(logBuffer, 0, length); if (prettyPrint) message = json.prettyPrint(message); info("Wrote: " + message); } }
/** * Releases all buffers in the stream. */ private void releaseAllBuffers() { for (ByteBuffer byteBuffer : byteBuffers) { byteBufferProvider.releaseByteBuffer(byteBuffer); } byteBuffers.clear(); ByteBuffer currentBuffer = super.getByteBuffer(); if (null != currentBuffer) { byteBufferProvider.releaseByteBuffer(currentBuffer); super.setByteBuffer(null); } } }