ByteBufHttpData implements HttpData, ResourceLeakHint { @Override public ByteBuf byteBuf(ByteBufAccessMode mode) { switch (requireNonNull(mode, "mode")) { case DUPLICATE: return buf.duplicate(); case RETAINED_DUPLICATE: return buf.retainedDuplicate(); case FOR_IO: if (buf.isDirect()) { return buf.retainedDuplicate(); } final ByteBuf copy = newDirectByteBuf(); copy.writeBytes(buf, buf.readerIndex(), buf.readableBytes()); return copy; } throw new Error(); } ByteBufHttpData(ByteBuf buf, boolean pooled); private  ByteBufHttpData(ByteBuf buf, int flags, @Nullable byte[] array); @Override byte[] array(); @Override int length(); @Override String toString(Charset charset); @Override String toString(); @Override String toHintString(); @Override InputStream toInputStream(); @Override boolean isEndOfStream(); @Override ByteBufHttpData withEndOfStream(boolean endOfStream); @Override boolean isPooled(); @Override ByteBuf byteBuf(ByteBufAccessMode mode); @Override ByteBuf byteBuf(int offset, int length, ByteBufAccessMode mode); @Override void touch(@Nullable Object hint); @Override void close(); @Override int hashCode(); @Override boolean equals(Object obj);  }
@Test void byteBuf() { final ByteBuf buf = Unpooled.buffer(4).writeInt(0x01020304); final ByteBufHttpData data = new ByteBufHttpData(buf, true); assertThat(data.isPooled()).isTrue(); final ByteBuf duplicate = data.byteBuf(); assertThat(duplicate.isDirect()).isFalse(); assertThat(duplicate.refCnt()).isOne(); assertThat(duplicate.readInt()).isEqualTo(0x01020304); assertThat(data.byteBuf().readableBytes()).isEqualTo(4); final ByteBuf retained = data.byteBuf(ByteBufAccessMode.RETAINED_DUPLICATE); assertThat(retained.isDirect()).isFalse(); assertThat(retained.refCnt()).isEqualTo(2); assertThat(retained.readInt()).isEqualTo(0x01020304); final ByteBuf copied = data.byteBuf(ByteBufAccessMode.FOR_IO); assertThat(copied.isDirect()).isTrue(); assertThat(copied.refCnt()).isOne(); assertThat(copied.readInt()).isEqualTo(0x01020304); assertThat(buf.refCnt()).isEqualTo(2); copied.release(); assertThat(buf.refCnt()).isEqualTo(2); retained.release(); assertThat(buf.refCnt()).isOne(); data.close(); assertThat(buf.refCnt()).isZero(); }