Nikolay Kuznetsov
Nikolay Kuznetsov

Reputation: 9579

ByteBuffer vs ChannelBuffer

I am using some framework implemented on the top of netty. I am sending a message from client to server using two options below. I suppose that this two snippets should write same bytes to the socket it behavior at server side is different. How is it different?

Option 1: okay

ChannelBuffer buf = ChannelBuffers.buffer(1);
buf.writeByte(0x1c);
e.getChannel().write(buf);

Option 2: fails

ByteBuffer buf = ByteBuffer.allocate(1);
buf.put(0x1c);
e.getChannel().write(ChannelBuffers.wrappedBuffer(buf));

Upvotes: 3

Views: 2401

Answers (1)

sebastian
sebastian

Reputation: 2507

Before you can write the ByteBuffer to the Channel you have to call

buf.flip();

This is making the bytes visible for write.

Upvotes: 6

Related Questions