public ChannelBuffer duplicate() { return new ByteBufferBackedChannelBuffer(this); }
public ChannelBuffer copy(int index, int length) { ByteBuffer src; try { src = (ByteBuffer) buffer.duplicate().position(index).limit(index + length); } catch (IllegalArgumentException e) { throw new IndexOutOfBoundsException("Too many bytes to read - Need " + (index + length)); } ByteBuffer dst = buffer.isDirect() ? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length); dst.put(src); dst.order(order()); dst.clear(); return new ByteBufferBackedChannelBuffer(dst); } }
/** * Creates a new buffer which wraps the specified NIO buffer's current * slice. A modification on the specified buffer's content will be * visible to the returned buffer. */ public static ChannelBuffer wrappedBuffer(ByteBuffer buffer) { if (!buffer.hasRemaining()) { return EMPTY_BUFFER; } if (buffer.hasArray()) { return wrappedBuffer( buffer.order(), buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } else { return new ByteBufferBackedChannelBuffer(buffer); } }
/** * Creates a new direct buffer with the specified {@code endianness} and * {@code capacity}. The new buffer's {@code readerIndex} and * {@code writerIndex} are {@code 0}. */ public static ChannelBuffer directBuffer(ByteOrder endianness, int capacity) { if (endianness == null) { throw new NullPointerException("endianness"); } if (capacity == 0) { return EMPTY_BUFFER; } ChannelBuffer buffer = new ByteBufferBackedChannelBuffer( ByteBuffer.allocateDirect(capacity).order(endianness)); buffer.clear(); return buffer; }
public ChannelBuffer slice(int index, int length) { if (index == 0 && length == capacity()) { ChannelBuffer slice = duplicate(); slice.setIndex(0, length); return slice; } else { if (index >= 0 && length == 0) { return ChannelBuffers.EMPTY_BUFFER; } return new ByteBufferBackedChannelBuffer( ((ByteBuffer) buffer.duplicate().position( index).limit(index + length)).order(order())); } }