Netty: Conversion from ByteBuf to byte[] returns different length arrays for same data

Created on 10 Jun 2019  路  3Comments  路  Source: netty/netty

Hi,

I am experiencing a behavior I would like to explain:
I am reading the same data from my local machine and in a network application as part of adding some code to Apache Pulsar.
The data is in ByteBuf format and needs to be converted to byte[] for further computation.
When reading the data locally, I use:
byte[] msgArray = buf.array();
The network code is as follows:
if(msg.hasArray()) {
msgArray = msg.array();
} else {
int readableBytes = msg.readableBytes();
msgArray = new byte[readableBytes];
int readerIndex = msg.readerIndex();
msg.getBytes(readerIndex, msgArray);
}
}
The same data locally has byte size of 259475, while in the network the size is 62290

Could you, help me understand what is happening here?

Thank you!

Regards,

Milena

Most helpful comment

Also be sure you take readerIndex, arrayOffset and readableBytes into account when acting on the backing array.

All 3 comments

I guess your server (client) didn't receive the complete data, maybe only 62290 bytes of data. Maybe you can try to assemble multiple message fragments in custom Decoder

Thanks a lot for your reply!

Also be sure you take readerIndex, arrayOffset and readableBytes into account when acting on the backing array.

Was this page helpful?
0 / 5 - 0 ratings