/** * Creates a new big-endian direct buffer with the specified * {@code capacity}. The new buffer's {@code readerIndex} and * {@code writerIndex} are {@code 0}. */ public static ChannelBuffer directBuffer(int capacity) { return directBuffer(BIG_ENDIAN, capacity); }
private ChannelBuffer allocateBigEndianBuffer(int capacity) { ChannelBuffer slice; synchronized (bigEndianLock) { if (preallocatedBEBuf == null) { preallocatedBEBuf = ChannelBuffers.directBuffer(ByteOrder.BIG_ENDIAN, preallocatedBufCapacity); slice = preallocatedBEBuf.slice(0, capacity); preallocatedBEBufPos = capacity; } else if (preallocatedBEBuf.capacity() - preallocatedBEBufPos >= capacity) { slice = preallocatedBEBuf.slice(preallocatedBEBufPos, capacity); preallocatedBEBufPos += capacity; } else { preallocatedBEBuf = ChannelBuffers.directBuffer(ByteOrder.BIG_ENDIAN, preallocatedBufCapacity); slice = preallocatedBEBuf.slice(0, capacity); preallocatedBEBufPos = capacity; } } return slice; }
private ChannelBuffer allocateLittleEndianBuffer(int capacity) { ChannelBuffer slice; synchronized (littleEndianLock) { if (preallocatedLEBuf == null) { preallocatedLEBuf = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, preallocatedBufCapacity); slice = preallocatedLEBuf.slice(0, capacity); preallocatedLEBufPos = capacity; } else if (preallocatedLEBuf.capacity() - preallocatedLEBufPos >= capacity) { slice = preallocatedLEBuf.slice(preallocatedLEBufPos, capacity); preallocatedLEBufPos += capacity; } else { preallocatedLEBuf = ChannelBuffers.directBuffer(ByteOrder.LITTLE_ENDIAN, preallocatedBufCapacity); slice = preallocatedLEBuf.slice(0, capacity); preallocatedLEBufPos = capacity; } } return slice; } }
public ChannelBuffer getBuffer(ByteOrder order, int capacity) { if (order == null) { throw new NullPointerException("order"); } if (capacity < 0) { throw new IllegalArgumentException("capacity: " + capacity); } if (capacity == 0) { return ChannelBuffers.EMPTY_BUFFER; } if (capacity >= preallocatedBufCapacity) { return ChannelBuffers.directBuffer(order, capacity); } ChannelBuffer slice; if (order == ByteOrder.BIG_ENDIAN) { slice = allocateBigEndianBuffer(capacity); } else { slice = allocateLittleEndianBuffer(capacity); } slice.clear(); return slice; }
/** * encode the current control message into a channel buffer */ @Override public ChannelBuffer buffer() throws Exception { ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.directBuffer(getEncodedLength())); write(bout); bout.close(); return bout.buffer(); }
/** * create a buffer containing the encoding of this batch */ @Override public ChannelBuffer buffer() throws Exception { int payloadLen = 0; if (_message != null) payloadLen = _message.length; int totalLen = 8 + payloadLen; ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.directBuffer(totalLen)); bout.writeShort(_type); if (_task > Short.MAX_VALUE) throw new RuntimeException("Task ID should not exceed " + Short.MAX_VALUE); bout.writeShort((short) _task); bout.writeInt(payloadLen); if (payloadLen > 0) bout.write(_message); bout.close(); return bout.buffer(); }
/** * create a buffer containing the encoding of this batch */ @Override public ChannelBuffer buffer() throws Exception { ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.directBuffer(encodedLength)); for (Object msg : msgs) if (msg instanceof TaskMessage) writeTaskMessage(bout, (TaskMessage) msg); else { // LOG.debug("Write one non-TaskMessage {}", msg ); ((ControlMessage) msg).write(bout); } // add a END_OF_BATCH indicator ControlMessage.EOB_MESSAGE.write(bout); // LOG.debug("ControlMessage.EOB_MESSAGE " ); bout.close(); return bout.buffer(); }
static ChannelBuffer directBuffer(byte[] array) { final ChannelBuffer buffer = ChannelBuffers.directBuffer(array.length); buffer.writeBytes(array); return buffer; }
/** * create a buffer containing the encoding of this batch */ ChannelBuffer buffer() throws IOException { ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.directBuffer(encoded_length)); try { for (TaskMessage msg : messages) { writeTaskMessage(bout, msg); } return bout.buffer(); } catch (IOException e) { log.error("Error while writing Tasks to Channel Buffer - {}", e.getMessage()); } finally { Closeables.close(bout, false); } return null; }
/** * create a buffer containing the encoding of this batch */ ChannelBuffer buffer() throws IOException { ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.directBuffer(encoded_length)); try { for (TaskMessage msg : messages) { writeTaskMessage(bout, msg); } return bout.buffer(); } catch (IOException e) { log.error("Error while writing Tasks to Channel Buffer - {}", e.getMessage()); } finally { Closeables.close(bout, false); } return null; }
/** * encode the current Control Message into a channel buffer * * @throws Exception */ @Override public ChannelBuffer buffer() throws Exception { ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.directBuffer(getEncodedLength())); write(bout); bout.close(); return bout.buffer(); }
/** * create a buffer containing the encoding of this batch */ ChannelBuffer buffer() throws Exception { ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.directBuffer(encoded_length)); for (TaskMessage msg : msgs) writeTaskMessage(bout, msg); //add a END_OF_BATCH indicator ControlMessage.EOB_MESSAGE.write(bout); bout.close(); return bout.buffer(); }
/** * encode the current Control Message into a channel buffer * @throws Exception */ ChannelBuffer buffer() throws Exception { ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.directBuffer(encodeLength())); write(bout); bout.close(); return bout.buffer(); }
/** * create a buffer containing the encoding of this batch */ @Override public ChannelBuffer buffer() throws Exception { int payloadLen = 0; if (_message != null) payloadLen = _message.length; int totalLen = 8 + payloadLen; ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.directBuffer(totalLen)); bout.writeShort(_type); if (_task > Short.MAX_VALUE) throw new RuntimeException("Task ID should not exceed " + Short.MAX_VALUE); bout.writeShort((short) _task); bout.writeInt(payloadLen); if (payloadLen > 0) bout.write(_message); bout.close(); return bout.buffer(); }
/** * create a buffer containing the encoding of this batch */ @Override public ChannelBuffer buffer() throws Exception { ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.directBuffer(encodedLength)); for (Object msg : msgs) if (msg instanceof TaskMessage) writeTaskMessage(bout, (TaskMessage) msg); else { // LOG.debug("Write one non-TaskMessage {}", msg ); ((ControlMessage) msg).write(bout); } // add a END_OF_BATCH indicator ControlMessage.EOB_MESSAGE.write(bout); // LOG.debug("ControlMessage.EOB_MESSAGE " ); bout.close(); return bout.buffer(); }