1
votes

I try to understand ByteBuffer.wrap(byte[]) or even ByteBuffer in general:

If I have a byte array which contains some values of various length and different type (for example int16s, int32s, UTF-16 strings all in LITTLE ENDIAN byte order and some ASCII strings aswell) and then wrap it with a ByteBuffer and send it across the network, let's say via an AsynchronousSocketChannel, in which order are my bytes sent then?

Does it send them in BIG ENDIAN? Does it look at the byte array as one big data and changes it's order to big endian or does it perceive the byte order and only adds new elements with big endian byte order?

The background is that I am dealing with a client that sends and receives bytes in little endian order and it seems that it can't deal with the data which I send across the network.

1
The data stays how it was. - user207421
The byte order is significant when getting/putting multi-byte values (e.g. putInt, getLong, etc.). - Slaw

1 Answers

0
votes

If the data is in little endian within the wrapped buffer then it remains in little endian order. If you add integers values then it depends on the order of the buffer, which defaults to big endian or "network order".

The byte order of the buffer instance only matters when primitive values are read or written using the various get and set methods, such as getInt and putInt (with or without position). The buffer and the data already stored within stays untouched if the byte order is changed.

Basically when retrieving data from the buffer, the byte with the lowest index is retrieved (and send) first, then the next one, etc. Commonly that's thought of as the leftmost byte. The position, in other words, always goes up when bytes are retrieved from the buffer and send, until the limit is reached.