@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 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(); }
/** * Release the provided number of reference counts. If this is a root buffer, will decrease * accounting if the local reference count returns to zero. */ @Override public synchronized boolean release(int decrement) { if (rootBuffer) { final long newRefCnt = this.rootRefCnt.addAndGet(-decrement); Preconditions.checkArgument(newRefCnt > -1, "Buffer has negative reference count."); if (newRefCnt == 0) { b.release(decrement); acct.release(this, length); return true; } else { return false; } } else { return b.release(decrement); } }
@Override public boolean release(int decrement) { return buf.release(decrement); }
@Override public boolean release() { return buf.release(); }
private static void testDecodeString(String text, Charset charset) { ByteBuf buffer = Unpooled.copiedBuffer(text, charset); Assert.assertEquals(text, ByteBufUtil.decodeString(buffer, 0, buffer.readableBytes(), charset)); buffer.release(); }