Bytes.fromByteBuf() incorrectly interpret ByteBuf content
See original GitHub issueTo reproduce
ByteBuf buf = Unpooled.buffer(100).writeByte(1);
Bytes bytes = Bytes.wrapByteBuf(buf);
System.out.println(bytes.size());
System.out.println(bytes.toArray().length);
Expected behavior
1
1
The buf
content is only 1 byte, other 99 reserved bytes are not the part of the buffer content
Observed behavior
100
100
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Netty java getting data from ByteBuf - Stack Overflow
2 Answers 2 ; @user2132106 would like to know if he can do byte[] ar= buf.array() (if I understand the post correctly). –...
Read more >ByteBuf (Netty API Reference (4.0.56.Final))
Returns true if and only if this buffer has a backing byte array. abstract int, hashCode(). Returns a hash code which was calculated...
Read more >SdkBytes (AWS SDK for Java - 2.18.38)
Create SdkBytes from a Byte array without copying the contents of the byte array. static SdkBytes. fromByteBuffer(ByteBuffer byteBuffer).
Read more >Working with Amazon S3 objects - AWS SDK for Java 2.x
These code snippets assume that you understand the material in basics, ... a RequestBody that contains the object content and the PutObjectRequest object....
Read more >ByteBuffer get() method in Java with Examples - GeeksforGeeks
The get() method of java.nio.ByteBuffer class is used to read the byte at the buffer's current position, and then increments the position.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
And this is exactly the reason why I would expect from
Bytes
to wrap only readableByteBuf
contentUnpooled.wrappedBuffer(byteBuf)
is actually was just an example. It is normally used to ‘concat’ severalButeBuf
s. And it concats only readable bytes.It is absolutely regular use case for Netty to receive a wire frame in a
ByteBuf
e.g.| header | payload |
and then a lower-level decoder reads the| header |
and then passes the sameByteBuf
to a higher-level decoder with areaderIndex
positioned to the beginning of| payload |
and the higher-level decoder treat thatByteBuf
exactly as just only| payload |
. So in that case I would expectBytes.wrapByteBuf()
to return only payload bytes.You may also check Google results on “netty bytebuf to array”
If you think the existing behavior is the one which is expected (or may be already relied on by other libs), I would add a Javadoc comment and and an alternative method which wraps the
ByteBuf
readable contents only.Hey, I would still suggest to address this issue in one or another way. It’s definitely not a showstopper, but I’m pretty sure there would definitely be misreading and erroneous usage of this method in the future.