3

I wanted to print io.netty.buffer.ByteBuf as string. But io.netty.buffer.ByteBuf.array() throws exception as follows.

java.lang.UnsupportedOperationException: direct buffer at io.netty.buffer.PooledUnsafeDirectByteBuf.array(PooledUnsafeDirectByteBuf.java:343)

Could some one help me to get the byte[] from byte io.netty.buffer.ByteBuf.

Thanks.

3 Answers 3

3

OK, I have figured out a way to print the values. If anyone is interested,

byte[] bytes = new byte[buf.readableBytes()];
buf.duplicate().readBytes(bytes);
System.out.println(new String(bytes));
Sign up to request clarification or add additional context in comments.

Comments

2

To print the contents of a ByteBuf as a string, you can use buf.toString(java.nio.charset.Charset).

Comments

0

Netty has a method for this

io.netty.buffer.ByteBufUtil.hexDump(buf)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.