public ReadOnlyChannelBuffer(ChannelBuffer buffer) { if (buffer == null) { throw new NullPointerException("buffer"); } this.buffer = buffer; setIndex(buffer.readerIndex(), buffer.writerIndex()); }
/** * Creates a new buffer whose content is a copy of the specified {@code buffer}'s readable bytes. * The new buffer's {@code readerIndex} and {@code writerIndex} are {@code 0} and {@code * buffer.readableBytes} respectively. */ public static ChannelBuffer copiedBuffer(ChannelBuffer buffer) { if (buffer.readable()) { return buffer.copy(); } else { return EMPTY_BUFFER; } }
/** * Creates a new buffer which wraps the specified buffer's readable bytes. A modification on the * specified buffer's content will be visible to the returned buffer. */ public static ChannelBuffer wrappedBuffer(ChannelBuffer buffer) { if (buffer.readable()) { return buffer.slice(); } else { return EMPTY_BUFFER; } }
private static ChannelBuffer copiedBuffer( ByteOrder endianness, CharBuffer buffer, Charset charset) { CharBuffer src = buffer; ByteBuffer dst = ChannelBuffers.encodeString(src, charset); ChannelBuffer result = wrappedBuffer(endianness, dst.array()); result.writerIndex(dst.remaining()); return result; }
@Test public void shouldReturnReadOnlyDerivedBuffer() { ChannelBuffer buf = ChannelBuffers.unmodifiableBuffer(ChannelBuffers.buffer(1)); assertTrue(buf.duplicate() instanceof ReadOnlyChannelBuffer); assertTrue(buf.slice() instanceof ReadOnlyChannelBuffer); assertTrue(buf.slice(0, 1) instanceof ReadOnlyChannelBuffer); assertTrue(buf.duplicate() instanceof ReadOnlyChannelBuffer); }
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; }
/** * 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; }
private static int lastIndexOf(ChannelBuffer buffer, int fromIndex, int toIndex, byte value) { fromIndex = Math.min(fromIndex, buffer.capacity()); if (fromIndex < 0 || buffer.capacity() == 0) { return -1; } for (int i = fromIndex - 1; i >= toIndex; i--) { if (buffer.getByte(i) == value) { return i; } } return -1; }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { ChannelBuffer buf; PaxosMessage msg; buf = (ChannelBuffer) e.getMessage(); // Read in the message string. msg = new PaxosMessage(); msg.unSerialize(buf.array()); // Send the message upstream to the server handler. Channels.fireMessageReceived(ctx, msg); }
private static int firstIndexOf(ChannelBuffer buffer, int fromIndex, int toIndex, byte value) { fromIndex = Math.max(fromIndex, 0); if (fromIndex >= toIndex || buffer.capacity() == 0) { return -1; } for (int i = fromIndex; i < toIndex; i++) { if (buffer.getByte(i) == value) { return i; } } return -1; }
private static int lastIndexOf( ChannelBuffer buffer, int fromIndex, int toIndex, ChannelBufferIndexFinder indexFinder) { fromIndex = Math.min(fromIndex, buffer.capacity()); if (fromIndex < 0 || buffer.capacity() == 0) { return -1; } for (int i = fromIndex - 1; i >= toIndex; i--) { if (indexFinder.find(buffer, i)) { return i; } } return -1; }
@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); }
@Override protected ChannelBuffer newBuffer(int length) { buffer = ChannelBuffers.wrappedBuffer(new byte[length * 2], random.nextInt(length - 1) + 1, length); assertEquals(length, buffer.writerIndex()); return buffer; }
@Override public ByteBuffer[] toByteBuffers(int index, int length) { ByteBuffer[] bufs = buffer.toByteBuffers(index, length); for (int i = 0; i < bufs.length; i++) { bufs[i] = bufs[i].asReadOnlyBuffer(); } return bufs; }
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; }
private ChannelBuffer allocateBigEndianBuffer(int capacity) { ChannelBuffer slice; synchronized (bigEndianLock) { if (preallocatedBigEndianBuffer == null) { preallocatedBigEndianBuffer = ChannelBuffers.directBuffer(ByteOrder.BIG_ENDIAN, preallocatedBufferCapacity); slice = preallocatedBigEndianBuffer.slice(0, capacity); preallocatedBigEndianBufferPosition = capacity; } else if (preallocatedBigEndianBuffer.capacity() - preallocatedBigEndianBufferPosition >= capacity) { slice = preallocatedBigEndianBuffer.slice(preallocatedBigEndianBufferPosition, capacity); preallocatedBigEndianBufferPosition += capacity; } else { preallocatedBigEndianBuffer = ChannelBuffers.directBuffer(ByteOrder.BIG_ENDIAN, preallocatedBufferCapacity); slice = preallocatedBigEndianBuffer.slice(0, capacity); preallocatedBigEndianBufferPosition = capacity; } } return slice; }
/** * Calculates the hash code of the specified buffer. This method is useful when implementing a new * buffer type. */ public static int hashCode(ChannelBuffer buffer) { final int aLen = buffer.readableBytes(); final int intCount = aLen >>> 2; final int byteCount = aLen & 3; int hashCode = 1; int arrayIndex = buffer.readerIndex(); if (buffer.order() == BIG_ENDIAN) { for (int i = intCount; i > 0; i--) { hashCode = 31 * hashCode + buffer.getInt(arrayIndex); arrayIndex += 4; } } else { for (int i = intCount; i > 0; i--) { hashCode = 31 * hashCode + swapInt(buffer.getInt(arrayIndex)); arrayIndex += 4; } } for (int i = byteCount; i > 0; i--) { hashCode = 31 * hashCode + buffer.getByte(arrayIndex++); } if (hashCode == 0) { hashCode = 1; } return hashCode; }
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 >= preallocatedBufferCapacity) { return ChannelBuffers.directBuffer(order, capacity); } ChannelBuffer slice; if (order == ByteOrder.BIG_ENDIAN) { slice = allocateBigEndianBuffer(capacity); } else { slice = allocateLittleEndianBuffer(capacity); } slice.clear(); return slice; }
private static int firstIndexOf( ChannelBuffer buffer, int fromIndex, int toIndex, ChannelBufferIndexFinder indexFinder) { fromIndex = Math.max(fromIndex, 0); if (fromIndex >= toIndex || buffer.capacity() == 0) { return -1; } for (int i = fromIndex; i < toIndex; i++) { if (indexFinder.find(buffer, i)) { return i; } } return -1; }
/** * Returns a <a href="http://en.wikipedia.org/wiki/Hex_dump">hex dump</a> of the specified * buffer's sub-region. */ public static String hexDump(ChannelBuffer buffer, int fromIndex, int length) { if (length < 0) { throw new IllegalArgumentException("length: " + length); } if (length == 0) { return ""; } int endIndex = fromIndex + length; char[] buf = new char[length << 1]; int srcIdx = fromIndex; int dstIdx = 0; for (; srcIdx < endIndex; srcIdx++, dstIdx += 2) { System.arraycopy(HEXDUMP_TABLE, buffer.getUnsignedByte(srcIdx) << 1, buf, dstIdx, 2); } return new String(buf); }
/** * Creates a new composite buffer which wraps the readable bytes of the specified buffers without * copying them. A modification on the content of the specified buffers will be visible to the * returned buffer. * * @throws IllegalArgumentException if the specified buffers' endianness are different from each * other */ public static ChannelBuffer wrappedBuffer(ChannelBuffer... buffers) { switch (buffers.length) { case 0: break; case 1: if (buffers[0].readable()) { return wrappedBuffer(buffers[0]); } break; default: ByteOrder order = null; final List<ChannelBuffer> components = new ArrayList<ChannelBuffer>(buffers.length); for (ChannelBuffer c : buffers) { if (c == null) { break; } if (c.readable()) { if (order != null) { if (!order.equals(c.order())) { throw new IllegalArgumentException("inconsistent byte order"); } } else { order = c.order(); } if (c instanceof CompositeChannelBuffer) { // Expand nested composition. components.addAll( ((CompositeChannelBuffer) c).decompose(c.readerIndex(), c.readableBytes())); } else { // An ordinary buffer (non-composite) components.add(c.slice()); } } } return compositeBuffer(order, components); } return EMPTY_BUFFER; }
public ChannelBufferFactory factory() { return buffer.factory(); }
@Override protected ChannelBuffer newBuffer(int length) { buffer = ChannelBuffers.buffer(ByteOrder.LITTLE_ENDIAN, length); assertEquals(0, buffer.writerIndex()); return buffer; }
public ByteOrder order() { return buffer.order(); }
/** * Compares the two specified buffers as described in {@link * ChannelBuffer#compareTo(ChannelBuffer)}. This method is useful when implementing a new buffer * type. */ public static int compare(ChannelBuffer bufferA, ChannelBuffer bufferB) { final int aLen = bufferA.readableBytes(); final int bLen = bufferB.readableBytes(); final int minLength = Math.min(aLen, bLen); final int uintCount = minLength >>> 2; final int byteCount = minLength & 3; int aIndex = bufferA.readerIndex(); int bIndex = bufferB.readerIndex(); if (bufferA.order() == bufferB.order()) { for (int i = uintCount; i > 0; i--) { long va = bufferA.getUnsignedInt(aIndex); long vb = bufferB.getUnsignedInt(bIndex); if (va > vb) { return 1; } else if (va < vb) { return -1; } aIndex += 4; bIndex += 4; } } else { for (int i = uintCount; i > 0; i--) { long va = bufferA.getUnsignedInt(aIndex); long vb = swapInt(bufferB.getInt(bIndex)) & 0xFFFFFFFFL; if (va > vb) { return 1; } else if (va < vb) { return -1; } aIndex += 4; bIndex += 4; } } for (int i = byteCount; i > 0; i--) { short va = bufferA.getUnsignedByte(aIndex); short vb = bufferB.getUnsignedByte(bIndex); if (va > vb) { return 1; } else if (va < vb) { return -1; } aIndex++; bIndex++; } return aLen - bLen; }
/** * Returns {@code true} if and only if the two specified buffers are identical to each other as * described in {@code ChannelBuffer#equals(Object)}. This method is useful when implementing a * new buffer type. */ public static boolean equals(ChannelBuffer bufferA, ChannelBuffer bufferB) { final int aLen = bufferA.readableBytes(); if (aLen != bufferB.readableBytes()) { return false; } final int longCount = aLen >>> 3; final int byteCount = aLen & 7; int aIndex = bufferA.readerIndex(); int bIndex = bufferB.readerIndex(); if (bufferA.order() == bufferB.order()) { for (int i = longCount; i > 0; i--) { if (bufferA.getLong(aIndex) != bufferB.getLong(bIndex)) { return false; } aIndex += 8; bIndex += 8; } } else { for (int i = longCount; i > 0; i--) { if (bufferA.getLong(aIndex) != swapLong(bufferB.getLong(bIndex))) { return false; } aIndex += 8; bIndex += 8; } } for (int i = byteCount; i > 0; i--) { if (bufferA.getByte(aIndex) != bufferB.getByte(bIndex)) { return false; } aIndex++; bIndex++; } return true; }
/** * Returns a <a href="http://en.wikipedia.org/wiki/Hex_dump">hex dump</a> of the specified * buffer's readable bytes. */ public static String hexDump(ChannelBuffer buffer) { return hexDump(buffer, buffer.readerIndex(), buffer.readableBytes()); }
@Test public void shouldReturnWritableCopy() { ChannelBuffer buf = ChannelBuffers.unmodifiableBuffer(ChannelBuffers.buffer(1)); assertFalse(buf.copy() instanceof ReadOnlyChannelBuffer); }
@Test public void shouldForwardReadCallsBlindly() throws Exception { ChannelBuffer buf = createStrictMock(ChannelBuffer.class); expect(buf.readerIndex()).andReturn(0).anyTimes(); expect(buf.writerIndex()).andReturn(0).anyTimes(); expect(buf.capacity()).andReturn(0).anyTimes(); expect(buf.getBytes(1, (GatheringByteChannel) null, 2)).andReturn(3); buf.getBytes(4, (OutputStream) null, 5); buf.getBytes(6, (byte[]) null, 7, 8); buf.getBytes(9, (ChannelBuffer) null, 10, 11); buf.getBytes(12, (ByteBuffer) null); expect(buf.getByte(13)).andReturn(Byte.valueOf((byte) 14)); expect(buf.getShort(15)).andReturn(Short.valueOf((short) 16)); expect(buf.getUnsignedMedium(17)).andReturn(18); expect(buf.getInt(19)).andReturn(20); expect(buf.getLong(21)).andReturn(22L); ByteBuffer bb = ByteBuffer.allocate(100); ByteBuffer[] bbs = {ByteBuffer.allocate(101), ByteBuffer.allocate(102)}; expect(buf.toByteBuffer(23, 24)).andReturn(bb); expect(buf.toByteBuffers(25, 26)).andReturn(bbs); expect(buf.capacity()).andReturn(27); replay(buf); ChannelBuffer roBuf = unmodifiableBuffer(buf); assertEquals(3, roBuf.getBytes(1, (GatheringByteChannel) null, 2)); roBuf.getBytes(4, (OutputStream) null, 5); roBuf.getBytes(6, (byte[]) null, 7, 8); roBuf.getBytes(9, (ChannelBuffer) null, 10, 11); roBuf.getBytes(12, (ByteBuffer) null); assertEquals((byte) 14, roBuf.getByte(13)); assertEquals((short) 16, roBuf.getShort(15)); assertEquals(18, roBuf.getUnsignedMedium(17)); assertEquals(20, roBuf.getInt(19)); assertEquals(22L, roBuf.getLong(21)); ByteBuffer roBB = roBuf.toByteBuffer(23, 24); assertEquals(100, roBB.capacity()); assertTrue(roBB.isReadOnly()); ByteBuffer[] roBBs = roBuf.toByteBuffers(25, 26); assertEquals(2, roBBs.length); assertEquals(101, roBBs[0].capacity()); assertTrue(roBBs[0].isReadOnly()); assertEquals(102, roBBs[1].capacity()); assertTrue(roBBs[1].isReadOnly()); assertEquals(27, roBuf.capacity()); verify(buf); }
public int capacity() { return buffer.capacity(); }