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.

Android Studio:Unknown Host Error

After installing Android Studio, I got the following error: Unknown host 'services.gradle.org'. Please ensure the host name is correct. If you are behind an HTTP proxy, please configure the proxy settings either in Android Studio or Gradle. Consult IDE log for more details (Help | Show Log) Solution File --> Settings --> HTTP Proxy --> Auto-detect proxy settings

Brain as the tool of the spirit

The mind as the bridge between pure consciousness and the body in which that consciousness temporarily resides.