Package org.apache.commons.io
Class EndianUtils
- java.lang.Object
 - 
- org.apache.commons.io.EndianUtils
 
 
- 
public class EndianUtils extends java.lang.ObjectHelps with reading and writing primitive numeric types (short,int,long,float, anddouble) that are encoded in little endian using two's complement or unsigned representations.Different computer architectures have different conventions for byte ordering. In "Little Endian" architectures (e.g. X86), the low-order byte is stored in memory at the lowest address, and subsequent bytes at higher addresses. In "Big Endian" architectures (e.g. Motorola 680X0), the situation is reversed. Most methods and classes throughout Java — e.g.
DataInputStreamandDouble.longBitsToDouble()— assume data is laid out in big endian order with the most significant byte first. The methods in this class read and write data in little endian order, generally by reversing the bytes and then using the regular Java methods to convert the swapped bytes to a primitive type.Provenance: Excalibur
- See Also:
 SwappedDataInputStream
 
- 
- 
Constructor Summary
Constructors Constructor Description EndianUtils()Deprecated.TODO Make private in 3.0. 
- 
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static doublereadSwappedDouble(byte[] data, int offset)Reads a little endiandoublevalue from a byte array at a given offset.static doublereadSwappedDouble(java.io.InputStream input)Reads a little endiandoublevalue from an InputStream.static floatreadSwappedFloat(byte[] data, int offset)Reads a little endianfloatvalue from a byte array at a given offset.static floatreadSwappedFloat(java.io.InputStream input)Reads a little endianfloatvalue from an InputStream.static intreadSwappedInteger(byte[] data, int offset)Reads a little endianintvalue from a byte array at a given offset.static intreadSwappedInteger(java.io.InputStream input)Reads a little endianintvalue from an InputStream.static longreadSwappedLong(byte[] data, int offset)Reads a little endianlongvalue from a byte array at a given offset.static longreadSwappedLong(java.io.InputStream input)Reads a little endianlongvalue from an InputStream.static shortreadSwappedShort(byte[] data, int offset)Reads a little endianshortvalue from a byte array at a given offset.static shortreadSwappedShort(java.io.InputStream input)Reads a little endianshortvalue from an InputStream.static longreadSwappedUnsignedInteger(byte[] data, int offset)Reads a little endian unsigned integer (32-bit) value from a byte array at a given offset.static longreadSwappedUnsignedInteger(java.io.InputStream input)Reads a little endian unsigned integer (32-bit) from an InputStream.static intreadSwappedUnsignedShort(byte[] data, int offset)Reads an unsigned short (16-bit) value from a byte array in little endian order at a given offset.static intreadSwappedUnsignedShort(java.io.InputStream input)Reads an unsigned short (16-bit) from an InputStream in little endian order.static doubleswapDouble(double value)Converts adoublevalue from big endian to little endian and vice versa.static floatswapFloat(float value)Converts afloatvalue from big endian to little endian and vice versa.static intswapInteger(int value)Converts anintvalue from big endian to little endian and vice versa.static longswapLong(long value)Converts alongvalue from big endian to little endian and vice versa.static shortswapShort(short value)Converts ashortvalue from big endian to little endian and vice versa.static voidwriteSwappedDouble(byte[] data, int offset, double value)Writes the 8 bytes of adoubleto a byte array at a given offset in little endian order.static voidwriteSwappedDouble(java.io.OutputStream output, double value)Writes the 8 bytes of adoubleto an output stream in little endian order.static voidwriteSwappedFloat(byte[] data, int offset, float value)Writes the 4 bytes of afloatto a byte array at a given offset in little endian order.static voidwriteSwappedFloat(java.io.OutputStream output, float value)Writes the 4 bytes of afloatto an output stream in little endian order.static voidwriteSwappedInteger(byte[] data, int offset, int value)Writes the 4 bytes of anintto a byte array at a given offset in little endian order.static voidwriteSwappedInteger(java.io.OutputStream output, int value)Writes the 4 bytes of anintto an output stream in little endian order.static voidwriteSwappedLong(byte[] data, int offset, long value)Writes the 8 bytes of alongto a byte array at a given offset in little endian order.static voidwriteSwappedLong(java.io.OutputStream output, long value)Writes the 8 bytes of alongto an output stream in little endian order.static voidwriteSwappedShort(byte[] data, int offset, short value)Writes the 2 bytes of ashortto a byte array at a given offset in little endian order.static voidwriteSwappedShort(java.io.OutputStream output, short value)Writes the 2 bytes of ashortto an output stream using little endian encoding. 
 - 
 
- 
- 
Method Detail
- 
readSwappedDouble
public static double readSwappedDouble(byte[] data, int offset)Reads a little endiandoublevalue from a byte array at a given offset.- Parameters:
 data- source byte arrayoffset- starting offset in the byte array- Returns:
 - the value read
 - Throws:
 java.lang.IllegalArgumentException- if the part of the byte array starting at offset does not have at least 8 bytes
 
- 
readSwappedDouble
public static double readSwappedDouble(java.io.InputStream input) throws java.io.IOExceptionReads a little endiandoublevalue from an InputStream.- Parameters:
 input- source InputStream- Returns:
 - the value just read
 - Throws:
 java.io.IOException- in case of an I/O problem
 
- 
readSwappedFloat
public static float readSwappedFloat(byte[] data, int offset)Reads a little endianfloatvalue from a byte array at a given offset.- Parameters:
 data- source byte arrayoffset- starting offset in the byte array- Returns:
 - the value read
 - Throws:
 java.lang.IllegalArgumentException- if the part of the byte array starting at offset does not have at least 4 bytes
 
- 
readSwappedFloat
public static float readSwappedFloat(java.io.InputStream input) throws java.io.IOExceptionReads a little endianfloatvalue from an InputStream.- Parameters:
 input- source InputStream- Returns:
 - the value just read
 - Throws:
 java.io.IOException- in case of an I/O problem
 
- 
readSwappedInteger
public static int readSwappedInteger(byte[] data, int offset)Reads a little endianintvalue from a byte array at a given offset.- Parameters:
 data- source byte arrayoffset- starting offset in the byte array- Returns:
 - the value read
 - Throws:
 java.lang.IllegalArgumentException- if the part of the byte array starting at offset does not have at least 4 bytes
 
- 
readSwappedInteger
public static int readSwappedInteger(java.io.InputStream input) throws java.io.IOExceptionReads a little endianintvalue from an InputStream.- Parameters:
 input- source InputStream- Returns:
 - the value just read
 - Throws:
 java.io.IOException- in case of an I/O problem
 
- 
readSwappedLong
public static long readSwappedLong(byte[] data, int offset)Reads a little endianlongvalue from a byte array at a given offset.- Parameters:
 data- source byte arrayoffset- starting offset in the byte array- Returns:
 - the value read
 - Throws:
 java.lang.IllegalArgumentException- if the part of the byte array starting at offset does not have at least 8 bytes
 
- 
readSwappedLong
public static long readSwappedLong(java.io.InputStream input) throws java.io.IOExceptionReads a little endianlongvalue from an InputStream.- Parameters:
 input- source InputStream- Returns:
 - the value just read
 - Throws:
 java.io.IOException- in case of an I/O problem
 
- 
readSwappedShort
public static short readSwappedShort(byte[] data, int offset)Reads a little endianshortvalue from a byte array at a given offset.- Parameters:
 data- source byte arrayoffset- starting offset in the byte array- Returns:
 - the value read
 - Throws:
 java.lang.IllegalArgumentException- if the part of the byte array starting at offset does not have at least 2 bytes
 
- 
readSwappedShort
public static short readSwappedShort(java.io.InputStream input) throws java.io.IOExceptionReads a little endianshortvalue from an InputStream.- Parameters:
 input- source InputStream- Returns:
 - the value just read
 - Throws:
 java.io.IOException- in case of an I/O problem
 
- 
readSwappedUnsignedInteger
public static long readSwappedUnsignedInteger(byte[] data, int offset)Reads a little endian unsigned integer (32-bit) value from a byte array at a given offset.- Parameters:
 data- source byte arrayoffset- starting offset in the byte array- Returns:
 - the value read
 - Throws:
 java.lang.IllegalArgumentException- if the part of the byte array starting at offset does not have at least 4 bytes
 
- 
readSwappedUnsignedInteger
public static long readSwappedUnsignedInteger(java.io.InputStream input) throws java.io.IOExceptionReads a little endian unsigned integer (32-bit) from an InputStream.- Parameters:
 input- source InputStream- Returns:
 - the value just read
 - Throws:
 java.io.IOException- in case of an I/O problem
 
- 
readSwappedUnsignedShort
public static int readSwappedUnsignedShort(byte[] data, int offset)Reads an unsigned short (16-bit) value from a byte array in little endian order at a given offset.- Parameters:
 data- source byte arrayoffset- starting offset in the byte array- Returns:
 - the value read
 - Throws:
 java.lang.IllegalArgumentException- if the part of the byte array starting at offset does not have at least 2 bytes
 
- 
readSwappedUnsignedShort
public static int readSwappedUnsignedShort(java.io.InputStream input) throws java.io.IOExceptionReads an unsigned short (16-bit) from an InputStream in little endian order.- Parameters:
 input- source InputStream- Returns:
 - the value just read
 - Throws:
 java.io.IOException- in case of an I/O problem
 
- 
swapDouble
public static double swapDouble(double value)
Converts adoublevalue from big endian to little endian and vice versa. That is, it converts thedoubleto bytes, reverses the bytes, and then reinterprets those bytes as a newdouble. This can be useful if you have a number that was read from the underlying source in the wrong endianness.- Parameters:
 value- value to convert- Returns:
 - the converted value
 
 
- 
swapFloat
public static float swapFloat(float value)
Converts afloatvalue from big endian to little endian and vice versa.- Parameters:
 value- value to convert- Returns:
 - the converted value
 
 
- 
swapInteger
public static int swapInteger(int value)
Converts anintvalue from big endian to little endian and vice versa.- Parameters:
 value- value to convert- Returns:
 - the converted value
 
 
- 
swapLong
public static long swapLong(long value)
Converts alongvalue from big endian to little endian and vice versa.- Parameters:
 value- value to convert- Returns:
 - the converted value
 
 
- 
swapShort
public static short swapShort(short value)
Converts ashortvalue from big endian to little endian and vice versa.- Parameters:
 value- value to convert- Returns:
 - the converted value
 
 
- 
writeSwappedDouble
public static void writeSwappedDouble(byte[] data, int offset, double value)Writes the 8 bytes of adoubleto a byte array at a given offset in little endian order.- Parameters:
 data- target byte arrayoffset- starting offset in the byte arrayvalue- value to write- Throws:
 java.lang.IllegalArgumentException- if the part of the byte array starting at offset does not have at least 8 bytes
 
- 
writeSwappedDouble
public static void writeSwappedDouble(java.io.OutputStream output, double value) throws java.io.IOExceptionWrites the 8 bytes of adoubleto an output stream in little endian order.- Parameters:
 output- target OutputStreamvalue- value to write- Throws:
 java.io.IOException- in case of an I/O problem
 
- 
writeSwappedFloat
public static void writeSwappedFloat(byte[] data, int offset, float value)Writes the 4 bytes of afloatto a byte array at a given offset in little endian order.- Parameters:
 data- target byte arrayoffset- starting offset in the byte arrayvalue- value to write- Throws:
 java.lang.IllegalArgumentException- if the part of the byte array starting at offset does not have at least 4 bytes
 
- 
writeSwappedFloat
public static void writeSwappedFloat(java.io.OutputStream output, float value) throws java.io.IOExceptionWrites the 4 bytes of afloatto an output stream in little endian order.- Parameters:
 output- target OutputStreamvalue- value to write- Throws:
 java.io.IOException- in case of an I/O problem
 
- 
writeSwappedInteger
public static void writeSwappedInteger(byte[] data, int offset, int value)Writes the 4 bytes of anintto a byte array at a given offset in little endian order.- Parameters:
 data- target byte arrayoffset- starting offset in the byte arrayvalue- value to write- Throws:
 java.lang.IllegalArgumentException- if the part of the byte array starting at offset does not have at least 4 bytes
 
- 
writeSwappedInteger
public static void writeSwappedInteger(java.io.OutputStream output, int value) throws java.io.IOExceptionWrites the 4 bytes of anintto an output stream in little endian order.- Parameters:
 output- target OutputStreamvalue- value to write- Throws:
 java.io.IOException- in case of an I/O problem
 
- 
writeSwappedLong
public static void writeSwappedLong(byte[] data, int offset, long value)Writes the 8 bytes of alongto a byte array at a given offset in little endian order.- Parameters:
 data- target byte arrayoffset- starting offset in the byte arrayvalue- value to write- Throws:
 java.lang.IllegalArgumentException- if the part of the byte array starting at offset does not have at least 8 bytes
 
- 
writeSwappedLong
public static void writeSwappedLong(java.io.OutputStream output, long value) throws java.io.IOExceptionWrites the 8 bytes of alongto an output stream in little endian order.- Parameters:
 output- target OutputStreamvalue- value to write- Throws:
 java.io.IOException- in case of an I/O problem
 
- 
writeSwappedShort
public static void writeSwappedShort(byte[] data, int offset, short value)Writes the 2 bytes of ashortto a byte array at a given offset in little endian order.- Parameters:
 data- target byte arrayoffset- starting offset in the byte arrayvalue- value to write- Throws:
 java.lang.IllegalArgumentException- if the part of the byte array starting at offset does not have at least 2 bytes
 
- 
writeSwappedShort
public static void writeSwappedShort(java.io.OutputStream output, short value) throws java.io.IOExceptionWrites the 2 bytes of ashortto an output stream using little endian encoding.- Parameters:
 output- target OutputStreamvalue- value to write- Throws:
 java.io.IOException- in case of an I/O problem
 
 - 
 
 -