public ChannelBuffer getBuffer(ByteBuffer nioBuffer) { if (!nioBuffer.isReadOnly() && nioBuffer.isDirect()) { return ChannelBuffers.wrappedBuffer(nioBuffer); } ChannelBuffer buf = getBuffer(nioBuffer.order(), nioBuffer.remaining()); int pos = nioBuffer.position(); buf.writeBytes(nioBuffer); nioBuffer.position(pos); return buf; }
@Override public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) { PaxosMessage msg; ChannelBuffer sendBuf; byte[] data; msg = (PaxosMessage) e.getMessage(); // The original message. // Serialize the message. data = msg.serialize(); sendBuf = ChannelBuffers.buffer(data.length); sendBuf.writeBytes(data); // Write the actual msg. // Send the message upstream. Channels.write(ctx, e.getFuture(), sendBuf); }
public ChannelBuffer getBuffer(ByteOrder order, byte[] array, int offset, int length) { if (array == null) { throw new NullPointerException("array"); } if (offset < 0) { throw new IndexOutOfBoundsException("offset: " + offset); } if (length == 0) { return ChannelBuffers.EMPTY_BUFFER; } if (offset + length > array.length) { throw new IndexOutOfBoundsException("length: " + length); } ChannelBuffer buf = getBuffer(order, length); buf.writeBytes(array, offset, length); return buf; }