@Override public List<ByteBuf> decompose(int offset, int length) { if (length == 0) { return Collections.emptyList(); } if (offset + length > capacity()) { throw new IndexOutOfBoundsException( "Too many bytes to decompose - Need " + (offset + length) + ", capacity is " + capacity()); } int componentId = toComponentIndex(offset); List<ByteBuf> slice = new ArrayList<ByteBuf>(components.size()); // The first component Component firstC = components.get(componentId); ByteBuf first = firstC.buf.duplicate(); first.readerIndex(offset - firstC.offset); ByteBuf buf = first; int bytesToSlice = length; do { int readableBytes = buf.readableBytes(); if (bytesToSlice <= readableBytes) { // Last component buf.writerIndex(buf.readerIndex() + bytesToSlice); slice.add(buf); break; } else { // Not the last component slice.add(buf); bytesToSlice -= readableBytes; componentId++; // Fetch the next component. buf = components.get(componentId).buf.duplicate(); } } while (bytesToSlice > 0); // Slice all components because only readable bytes are interesting. for (int i = 0; i < slice.size(); i++) { slice.set(i, slice.get(i).slice()); } return slice; }
public OFMessage readFrom(ByteBuf bb) throws OFParseError { if(!bb.isReadable()) return null; short wireVersion = U8.f(bb.getByte(bb.readerIndex())); OFFactory factory; switch (wireVersion) { //:: for v in versions: case ${v.int_version}:
@Override public void addComponent(int cIndex, ByteBuf buffer) { checkComponentIndex(cIndex); if (buffer == null) { throw new NullPointerException("buffer"); } if (buffer instanceof Iterable) { @SuppressWarnings("unchecked") Iterable<ByteBuf> composite = (Iterable<ByteBuf>) buffer; addComponents(cIndex, composite); return; } int readableBytes = buffer.readableBytes(); if (readableBytes == 0) { return; } // Consolidate if the number of components will exceed the allowed maximum by the current // operation. final int numComponents = components.size(); if (numComponents >= maxNumComponents) { final int capacity = components.get(numComponents - 1).endOffset + readableBytes; ByteBuf consolidated = buffer.unsafe().newBuffer(capacity); for (int i = 0; i < numComponents; i++) { ByteBuf b = components.get(i).buf; consolidated.writeBytes(b); b.unsafe().release(); } consolidated.writeBytes(buffer, buffer.readerIndex(), readableBytes); Component c = new Component(consolidated); c.endOffset = c.length; components.clear(); components.add(c); return; } // No need to consolidate - just add a component to the list. Component c = new Component(buffer.order(ByteOrder.BIG_ENDIAN).slice()); if (cIndex == components.size()) { components.add(c); if (cIndex == 0) { c.endOffset = readableBytes; } else { Component prev = components.get(cIndex - 1); c.offset = prev.endOffset; c.endOffset = c.offset + readableBytes; } } else { components.add(cIndex, c); updateComponentOffsets(cIndex); } }
/** * 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 ByteBuf copiedBuffer(ByteBuf buffer) { int readable = buffer.readableBytes(); if (readable > 0) { ByteBuf copy = buffer(readable); copy.writeBytes(buffer, buffer.readerIndex(), readable); return copy; } else { return EMPTY_BUFFER; } }
@Test public void shouldForwardReadCallsBlindly() throws Exception { ByteBuf buf = createStrictMock(ByteBuf.class); expect(buf.order()).andReturn(BIG_ENDIAN).anyTimes(); expect(buf.maxCapacity()).andReturn(65536).anyTimes(); 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); expect(buf.getBytes(4, (OutputStream) null, 5)).andReturn(buf); expect(buf.getBytes(6, (byte[]) null, 7, 8)).andReturn(buf); expect(buf.getBytes(9, (ByteBuf) null, 10, 11)).andReturn(buf); expect(buf.getBytes(12, (ByteBuffer) null)).andReturn(buf); 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); expect(buf.nioBuffer(23, 24)).andReturn(bb); expect(buf.capacity()).andReturn(27); replay(buf); ByteBuf 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, (ByteBuf) 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.nioBuffer(23, 24); assertEquals(100, roBB.capacity()); assertTrue(roBB.isReadOnly()); assertEquals(27, roBuf.capacity()); verify(buf); }
/** * Creates a new buffer whose content is a merged copy of the specified {@code buffers}' readable * bytes. The new buffer's {@code readerIndex} and {@code writerIndex} are {@code 0} and the sum * of all buffers' {@code readableBytes} respectively. * * @throws IllegalArgumentException if the specified buffers' endianness are different from each * other */ public static ByteBuf copiedBuffer(ByteBuf... buffers) { switch (buffers.length) { case 0: return EMPTY_BUFFER; case 1: return copiedBuffer(buffers[0]); } // Merge the specified buffers into one buffer. ByteOrder order = null; int length = 0; for (ByteBuf b : buffers) { int bLen = b.readableBytes(); if (bLen <= 0) { continue; } if (Integer.MAX_VALUE - length < bLen) { throw new IllegalArgumentException("The total length of the specified buffers is too big."); } length += bLen; if (order != null) { if (!order.equals(b.order())) { throw new IllegalArgumentException("inconsistent byte order"); } } else { order = b.order(); } } if (length == 0) { return EMPTY_BUFFER; } byte[] mergedArray = new byte[length]; for (int i = 0, j = 0; i < buffers.length; i++) { ByteBuf b = buffers[i]; int bLen = b.readableBytes(); b.getBytes(b.readerIndex(), mergedArray, j, bLen); j += bLen; } return wrappedBuffer(mergedArray).order(order); }
@Override public ByteBuf readerIndex(int readerIndex) { buf.readerIndex(readerIndex); return this; }
@Override public int readerIndex() { return buf.readerIndex(); }
@Override public void addComponents(int cIndex, ByteBuf... buffers) { checkComponentIndex(cIndex); if (buffers == null) { throw new NullPointerException("buffers"); } ByteBuf lastBuf = null; int cnt = 0; int readableBytes = 0; for (ByteBuf b : buffers) { if (b == null) { break; } lastBuf = b; cnt++; readableBytes += b.readableBytes(); } if (readableBytes == 0) { return; } // Consolidate if the number of components will exceed the maximum by this operation. final int numComponents = components.size(); if (numComponents + cnt > maxNumComponents) { final ByteBuf consolidated; if (numComponents != 0) { final int capacity = components.get(numComponents - 1).endOffset + readableBytes; consolidated = lastBuf.unsafe().newBuffer(capacity); for (int i = 0; i < cIndex; i++) { ByteBuf b = components.get(i).buf; consolidated.writeBytes(b); b.unsafe().release(); } for (ByteBuf b : buffers) { if (b == null) { break; } consolidated.writeBytes(b, b.readerIndex(), b.readableBytes()); } for (int i = cIndex; i < numComponents; i++) { ByteBuf b = components.get(i).buf; consolidated.writeBytes(b); b.unsafe().release(); } } else { consolidated = lastBuf.unsafe().newBuffer(readableBytes); for (ByteBuf b : buffers) { if (b == null) { break; } consolidated.writeBytes(b, b.readerIndex(), b.readableBytes()); } } Component c = new Component(consolidated); c.endOffset = c.length; components.clear(); components.add(c); updateComponentOffsets(0); return; } // No need for consolidation for (ByteBuf b : buffers) { if (b == null) { break; } if (b.readable()) { addComponent(cIndex++, b); } } }
@Test public void testDiscardReadBytes3() { ByteBuf a, b; a = wrappedBuffer(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).order(order); b = freeLater( wrappedBuffer( wrappedBuffer(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 0, 5).order(order), wrappedBuffer(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 5, 5).order(order))); a.skipBytes(6); a.markReaderIndex(); b.skipBytes(6); b.markReaderIndex(); assertEquals(a.readerIndex(), b.readerIndex()); a.readerIndex(a.readerIndex() - 1); b.readerIndex(b.readerIndex() - 1); assertEquals(a.readerIndex(), b.readerIndex()); a.writerIndex(a.writerIndex() - 1); a.markWriterIndex(); b.writerIndex(b.writerIndex() - 1); b.markWriterIndex(); assertEquals(a.writerIndex(), b.writerIndex()); a.writerIndex(a.writerIndex() + 1); b.writerIndex(b.writerIndex() + 1); assertEquals(a.writerIndex(), b.writerIndex()); assertTrue(ByteBufUtil.equals(a, b)); // now discard a.discardReadBytes(); b.discardReadBytes(); assertEquals(a.readerIndex(), b.readerIndex()); assertEquals(a.writerIndex(), b.writerIndex()); assertTrue(ByteBufUtil.equals(a, b)); a.resetReaderIndex(); b.resetReaderIndex(); assertEquals(a.readerIndex(), b.readerIndex()); a.resetWriterIndex(); b.resetWriterIndex(); assertEquals(a.writerIndex(), b.writerIndex()); assertTrue(ByteBufUtil.equals(a, b)); }