@Override protected ByteBuf newBuffer(int length) { buffer = PooledByteBufAllocator.DEFAULT.directBuffer(length).order(ByteOrder.LITTLE_ENDIAN); assertSame(ByteOrder.LITTLE_ENDIAN, buffer.order()); assertEquals(0, buffer.writerIndex()); return buffer; }
@Test public void shouldHaveSameByteOrder() { ByteBuf buf = Unpooled.buffer(1); assertSame(Unpooled.BIG_ENDIAN, Unpooled.unmodifiableBuffer(buf).order()); buf = buf.order(LITTLE_ENDIAN); assertSame(Unpooled.LITTLE_ENDIAN, Unpooled.unmodifiableBuffer(buf).order()); }
@Test public void shouldReturnReadOnlyDerivedBuffer() { ByteBuf buf = Unpooled.unmodifiableBuffer(Unpooled.buffer(1)); assertTrue(buf.duplicate() instanceof ReadOnlyByteBuf); assertTrue(buf.slice() instanceof ReadOnlyByteBuf); assertTrue(buf.slice(0, 1) instanceof ReadOnlyByteBuf); assertTrue(buf.duplicate() instanceof ReadOnlyByteBuf); }
// Test for https://github.com/netty/netty/issues/1060 @Test public void testReadWithEmptyCompositeBuffer() { ByteBuf buf = freeLater(compositeBuffer()); int n = 65; for (int i = 0; i < n; i++) { buf.writeByte(1); assertEquals(1, buf.readByte()); } }
@Test public void testReferenceCounts3() { ByteBuf c1 = buffer().writeByte(1); ByteBuf c2 = buffer().writeByte(2).retain(); ByteBuf c3 = buffer().writeByte(3).retain(2); CompositeByteBuf buf = freeLater(compositeBuffer()); assertThat(buf.refCnt(), is(1)); List<ByteBuf> components = new ArrayList<ByteBuf>(); Collections.addAll(components, c1, c2, c3); buf.addComponents(components); // Ensure that c[123]'s refCount did not change. assertThat(c1.refCnt(), is(1)); assertThat(c2.refCnt(), is(2)); assertThat(c3.refCnt(), is(3)); assertThat(buf.component(0).refCnt(), is(1)); assertThat(buf.component(1).refCnt(), is(2)); assertThat(buf.component(2).refCnt(), is(3)); c3.release(2); c2.release(); }
@Test public void testCompositeWrappedBuffer() { ByteBuf header = buffer(12).order(order); ByteBuf payload = buffer(512).order(order); header.writeBytes(new byte[12]); payload.writeBytes(new byte[512]); ByteBuf buffer = wrappedBuffer(header, payload); assertEquals(12, header.readableBytes()); assertEquals(512, payload.readableBytes()); assertEquals(12 + 512, buffer.readableBytes()); assertEquals(2, buffer.nioBufferCount()); }
/** Tests the "getBufferFor" method */ @Test public void testComponentAtOffset() { CompositeByteBuf buf = (CompositeByteBuf) wrappedBuffer(new byte[] {1, 2, 3, 4, 5}, new byte[] {4, 5, 6, 7, 8, 9, 26}); // Ensure that a random place will be fine assertEquals(5, buf.componentAtOffset(2).capacity()); // Loop through each byte byte index = 0; while (index < buf.capacity()) { ByteBuf _buf = buf.componentAtOffset(index++); assertNotNull(_buf); assertTrue(_buf.capacity() > 0); assertNotNull(_buf.getByte(0)); assertNotNull(_buf.getByte(_buf.readableBytes() - 1)); } }
@Override protected ByteBuf newBuffer(int length) { buffers = new ArrayList<ByteBuf>(); for (int i = 0; i < length + 45; i += 45) { buffers.add(EMPTY_BUFFER); buffers.add(wrappedBuffer(new byte[1])); buffers.add(EMPTY_BUFFER); buffers.add(wrappedBuffer(new byte[2])); buffers.add(EMPTY_BUFFER); buffers.add(wrappedBuffer(new byte[3])); buffers.add(EMPTY_BUFFER); buffers.add(wrappedBuffer(new byte[4])); buffers.add(EMPTY_BUFFER); buffers.add(wrappedBuffer(new byte[5])); buffers.add(EMPTY_BUFFER); buffers.add(wrappedBuffer(new byte[6])); buffers.add(EMPTY_BUFFER); buffers.add(wrappedBuffer(new byte[7])); buffers.add(EMPTY_BUFFER); buffers.add(wrappedBuffer(new byte[8])); buffers.add(EMPTY_BUFFER); buffers.add(wrappedBuffer(new byte[9])); buffers.add(EMPTY_BUFFER); } buffer = Unpooled.wrappedBuffer(Integer.MAX_VALUE, buffers.toArray(new ByteBuf[buffers.size()])) .order(order); // Truncate to the requested capacity. buffer.capacity(length); assertEquals(length, buffer.capacity()); assertEquals(length, buffer.readableBytes()); assertFalse(buffer.isWritable()); buffer.writerIndex(0); return 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); }
@Test public void shouldReturnWritableCopy() { ByteBuf buf = Unpooled.unmodifiableBuffer(Unpooled.buffer(1)); assertFalse(buf.copy() instanceof ReadOnlyByteBuf); }
@Test public void testReferenceCounts2() { ByteBuf c1 = buffer().writeByte(1); ByteBuf c2 = buffer().writeByte(2).retain(); ByteBuf c3 = buffer().writeByte(3).retain(2); CompositeByteBuf bufA = compositeBuffer(); bufA.addComponents(c1, c2, c3).writerIndex(3); CompositeByteBuf bufB = compositeBuffer(); bufB.addComponents(bufA); // Ensure that bufA.refCnt() did not change. assertThat(bufA.refCnt(), is(1)); // Ensure that c[123]'s refCnt did not change. assertThat(c1.refCnt(), is(1)); assertThat(c2.refCnt(), is(2)); assertThat(c3.refCnt(), is(3)); // This should decrease bufA.refCnt(). bufB.release(); assertThat(bufB.refCnt(), is(0)); // Ensure bufA.refCnt() changed. assertThat(bufA.refCnt(), is(0)); // Ensure that c[123]'s refCnt also changed due to the deallocation of bufA. assertThat(c1.refCnt(), is(0)); assertThat(c2.refCnt(), is(1)); assertThat(c3.refCnt(), is(2)); c3.release(2); c2.release(); }
@Test public void testEmptyBuffer() { ByteBuf b = freeLater(wrappedBuffer(new byte[] {1, 2}, new byte[] {3, 4})); b.readBytes(new byte[4]); b.readBytes(EMPTY_BYTES); }
@Test public void testWrittenBuffersEquals() { // XXX Same tests than testEquals with written AggregateChannelBuffers ByteBuf a, b; // Different length. a = wrappedBuffer(new byte[] {1}).order(order); b = wrappedBuffer(wrappedBuffer(new byte[] {1}, new byte[1]).order(order)); // to enable writeBytes b.writerIndex(b.writerIndex() - 1); b.writeBytes(wrappedBuffer(new byte[] {2}).order(order)); assertFalse(ByteBufUtil.equals(a, b)); // Same content, same firstIndex, short length. a = wrappedBuffer(new byte[] {1, 2, 3}).order(order); b = wrappedBuffer(wrappedBuffer(new byte[] {1}, new byte[2]).order(order)); // to enable writeBytes b.writerIndex(b.writerIndex() - 2); b.writeBytes(wrappedBuffer(new byte[] {2}).order(order)); b.writeBytes(wrappedBuffer(new byte[] {3}).order(order)); assertTrue(ByteBufUtil.equals(a, b)); // Same content, different firstIndex, short length. a = wrappedBuffer(new byte[] {1, 2, 3}).order(order); b = wrappedBuffer(wrappedBuffer(new byte[] {0, 1, 2, 3, 4}, 1, 3).order(order)); // to enable writeBytes b.writerIndex(b.writerIndex() - 1); b.writeBytes(wrappedBuffer(new byte[] {0, 1, 2, 3, 4}, 3, 1).order(order)); assertTrue(ByteBufUtil.equals(a, b)); // Different content, same firstIndex, short length. a = wrappedBuffer(new byte[] {1, 2, 3}).order(order); b = freeLater(wrappedBuffer(wrappedBuffer(new byte[] {1, 2}, new byte[1]).order(order))); // to enable writeBytes b.writerIndex(b.writerIndex() - 1); b.writeBytes(wrappedBuffer(new byte[] {4}).order(order)); assertFalse(ByteBufUtil.equals(a, b)); // Different content, different firstIndex, short length. a = wrappedBuffer(new byte[] {1, 2, 3}).order(order); b = wrappedBuffer(wrappedBuffer(new byte[] {0, 1, 2, 4, 5}, 1, 3).order(order)); // to enable writeBytes b.writerIndex(b.writerIndex() - 1); b.writeBytes(wrappedBuffer(new byte[] {0, 1, 2, 4, 5}, 3, 1).order(order)); assertFalse(ByteBufUtil.equals(a, b)); // Same content, same firstIndex, long length. 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}, new byte[7])).order(order)); // to enable writeBytes b.writerIndex(b.writerIndex() - 7); b.writeBytes(wrappedBuffer(new byte[] {4, 5, 6}).order(order)); b.writeBytes(wrappedBuffer(new byte[] {7, 8, 9, 10}).order(order)); assertTrue(ByteBufUtil.equals(a, b)); // Same content, different firstIndex, long length. a = wrappedBuffer(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).order(order); b = wrappedBuffer( wrappedBuffer(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 1, 10).order(order)); // to enable writeBytes b.writerIndex(b.writerIndex() - 5); b.writeBytes( wrappedBuffer(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 6, 5).order(order)); assertTrue(ByteBufUtil.equals(a, b)); // Different content, same firstIndex, long length. 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, 6}, new byte[5])).order(order)); // to enable writeBytes b.writerIndex(b.writerIndex() - 5); b.writeBytes(wrappedBuffer(new byte[] {7, 8, 5, 9, 10}).order(order)); assertFalse(ByteBufUtil.equals(a, b)); // Different content, different firstIndex, long length. a = wrappedBuffer(new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).order(order); b = wrappedBuffer( wrappedBuffer(new byte[] {0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11}, 1, 10).order(order)); // to enable writeBytes b.writerIndex(b.writerIndex() - 5); b.writeBytes( wrappedBuffer(new byte[] {0, 1, 2, 3, 4, 6, 7, 8, 5, 9, 10, 11}, 6, 5).order(order)); assertFalse(ByteBufUtil.equals(a, 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)); }