3
votes

The topic is rather simple, but I might have confused myself a bit here. Network byte order is Big-Endian. Java by default uses Big-endian as well (as in the case of class files). My windows machine uses Intel processors, which are Little-Endian and windows itself also uses Little-Endian. So, if I uses a java.nio.ByteBuffer.allocateDirect(), then the default Endianness is Little-Endian (because of the OS and/or the processor), however, if I want to send this data on network, on reading data from ByteBuffer, do I need to use ByteBuffer.order(ByteOrder.BIG_ENDIAN)? Additionally, Do i have to do the reverse while reading the data from a Channel to a ByteBuffer? I almost ignored it when using java.io package

Another part of the question is also related to working on the machine itself.

  1. Should I always be aware of the Endianness of my host machine? So, whatever local data storage format is used, its fine within this machine's environment, however, convert the data to BIG_ENDIAN when sending to network.
  2. In terms of Endianness, for default file formats, say on windows, its always Little-Endian. However, now using the same file on a MAC or a Linux with Big-Endian, should I save it with a Byte-order-Marking (BOM)?
2
Your last question doesn't make sense. Character sets don't have anything to do with endian-ness. - user207421
Edited to remove the UTF-8 confusion. Can you pls. also see my comments to @TwoThe. Thanks. - Ashley

2 Answers

8
votes

The topic is rather simple, but I might have confused myself a bit here. Network byte order is Big-Endian. Java by default uses Big-endian as well (as in the case of class files).

In the case of class files, DataOutputStream, DataInputStream, ObjectInputStream, ObjectOutputStream, and java.nio.*, and I've probably left something out.

If I uses a java.nio.ByteBuffer.allocateDirect(), then the default Endianness is Little-Endian (because of the OS and/or the processor)

No. 'The order of a newly created ByteBuffer is always big-endian'. See the Javadoc.

If I want to send this data on network, on reading data from ByteBuffer, do I need to use ByteBuffer.order(ByteOrder.BIG_ENDIAN)?

It already is.

Additionally, Do i have to do the reverse while reading the data from a Channel to a ByteBuffer?

It already is.

I almost ignored it when using java.io package

DataInputStream and DataOutputStream use big-endian. See the Javadoc.

Should I always be aware of the Endianness of my host machine?

Not in Java.

So, whatever local data storage format is used, its fine within this machine's environment, however, convert the data to BIG_ENDIAN when sending to network.

That already happens by default.

In terms of Endianness, for default file formats, say on windows, its always Little-Endian.

No it isn't. See above.

However, now using the same file on a MAC or a Linux with Big-Endian, should I save it with a Byte-order-Marking (BOM)?

See above. Not necessary.

-2
votes

You need to always send the same Endian style. Which one you choose should be based on how fast it can be processed on the host. If your code is i.E. primarily used on x86 based machines, it is recommended to use little Endian all over the place. Internally the JVM will do the same (on this OS).