ByteArrayHttpData implements HttpData { @Override public ByteBuf byteBuf(ByteBufAccessMode mode) { requireNonNull(mode, "mode"); if (isEmpty()) { return Unpooled.EMPTY_BUFFER; } if (mode != ByteBufAccessMode.FOR_IO) { return Unpooled.wrappedBuffer(array); } else { final ByteBuf copy = newDirectByteBuf(); copy.writeBytes(array); return copy; } } ByteArrayHttpData(byte[] array); private  ByteArrayHttpData(byte[] array, boolean endOfStream); @Override byte[] array(); @Override int length(); @Override String toString(Charset charset); @Override String toString(); @Override InputStream toInputStream(); @Override boolean isEndOfStream(); @Override ByteArrayHttpData withEndOfStream(boolean endOfStream); @Override boolean isPooled(); @Override ByteBuf byteBuf(ByteBufAccessMode mode); @Override ByteBuf byteBuf(int offset, int length, ByteBufAccessMode mode); @Override void close(); @Override int hashCode(); @Override boolean equals(Object obj);  }
@Test void directCopy() { final ByteArrayHttpData data = new ByteArrayHttpData(new byte[] { 1, 2, 3, 4 }); final ByteBuf buf = data.byteBuf(ByteBufAccessMode.FOR_IO); assertThat(buf.isDirect()).isTrue(); assertThat(buf.readableBytes()).isEqualTo(4); assertThat(buf.readInt()).isEqualTo(0x01020304); buf.release(); final ByteBuf slicedBuf = data.byteBuf(1, 2, ByteBufAccessMode.FOR_IO); assertThat(slicedBuf.isDirect()).isTrue(); assertThat(slicedBuf.readableBytes()).isEqualTo(2); assertThat(slicedBuf.readUnsignedShort()).isEqualTo(0x0203); slicedBuf.release(); }