question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bytes.fromByteBuf() incorrectly interpret ByteBuf content

See original GitHub issue

To 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:open
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Nashatyrevcommented, Jul 14, 2020

These are not features of the Bytes implementation in Tuweni

And this is exactly the reason why I would expect from Bytes to wrap only readable ByteBuf content

Given Netty provides methods to present just the “readable” bytes as a ByteBuf, e.g. Unpooled.wrappedBuffer(byteBuf)

Unpooled.wrappedBuffer(byteBuf) is actually was just an example. It is normally used to ‘concat’ several ButeBufs. 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 same ByteBuf to a higher-level decoder with a readerIndex positioned to the beginning of | payload | and the higher-level decoder treat that ByteBuf exactly as just only | payload |. So in that case I would expect Bytes.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.

0reactions
Nashatyrevcommented, Dec 17, 2020

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found