ByteBuf buffer = Unpooled.buffer(4); buffer.writeInt(1234); byte b = buffer.getByte(2); System.out.println(b);
ByteBuf buffer = Unpooled.buffer(); buffer.writeBytes(new byte[] {0x12, 0x34, 0x56, 0x78}); byte[] bytes = new byte[2]; buffer.getBytes(1, bytes); System.out.println(Arrays.toString(bytes));In this example, we create a ByteBuf and write four byte values to it. We then use the getBytes() method to retrieve two bytes starting at index 1 (which correspond to the second and third bytes of the four that we wrote) and store them in a byte array. Finally, we print out the contents of the byte array using the Arrays.toString() method. The output of this code would be "[52, 86]" (since the binary representation of the bytes we retrieved is 0x34 0x56). In both examples, we import the io.netty.buffer package to use the ByteBuf class.