Skip to main content

Endianness of Java

In Java, multibyte data items or streams are stored in big endian order.


Using Java to write bytes directly to the wire will require byte-order transformation if the recipient talks in little endian. The same is true if you are reading directly from the wire from a sender who talks in little endian.


If you are using a byte array wrapped in ByteBuffer,



byte[] data = new byte[] { 12, 19, 83, 05, -05, 55, -55 };
ByteBuffer byteBuffer = ByteBuffer.wrap(data);

Transforming from big endian to little endian is easy,



byteBuffer.order(java.nio.ByteOrder.LITTLE_ENDIAN);

If you are unsure of the endianness of the underlying platform, just invoke the native order method.



java.nio.ByteOrder.nativeOrder();

Aside from streams, operands in classes that are above 1 byte in size are in big endian order.

Comments

Popular posts from this blog

Architecture Complexity

Here are the items to consider: Coding to an interface Service Oriented Architecture Automated Testing Domain Driven Design Custom Data Access Layer Layered architecture Complexity is relatively equal the number of lines of code. Note that complexity is not bad. It must be justified.

Repair Windows 7 System Files

8 out of 10 average PC users have their box’s system files altered by malwares, viruses, etc. We usually reinstall the OS if the antivirus and anti malware software did not perform their job well. Here’s one way to fix the corrupted system files without the need of restarting your Windows 7 box. 1. Run the Command Prompt as Administrator 2. Type the following command C:\Windows\system32\> sfc /scannow 3. After the verification phase, you will receive a message about your system files’ integrity Windows Resource Protection did not find any integrity violations.