Package org.apache.commons.io
Class EndianUtils
java.lang.Object
org.apache.commons.io.EndianUtils
Helps with reading and writing primitive numeric types (
short,
 int, long, float, and double) 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. DataInputStream and
 Double.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:
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionstatic doublereadSwappedDouble(byte[] data, int offset) Reads a little endiandoublevalue from a byte array at a given offset.static doublereadSwappedDouble(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(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(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(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(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 longReads 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 intReads 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(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(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(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(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(OutputStream output, short value) Writes the 2 bytes of ashortto an output stream using little endian encoding.
- 
Constructor Details- 
EndianUtilsDeprecated.TODO Make private in 3.0.Instances should NOT be constructed in standard programming.
 
- 
- 
Method Details- 
readSwappedDoubleReads a little endiandoublevalue from a byte array at a given offset.- Parameters:
- data- source byte array
- offset- starting offset in the byte array
- Returns:
- the value read
- Throws:
- IllegalArgumentException- if the part of the byte array starting at offset does not have at least 8 bytes
 
- 
readSwappedDoubleReads a little endiandoublevalue from an InputStream.- Parameters:
- input- source InputStream
- Returns:
- the value just read
- Throws:
- IOException- in case of an I/O problem
 
- 
readSwappedFloatReads a little endianfloatvalue from a byte array at a given offset.- Parameters:
- data- source byte array
- offset- starting offset in the byte array
- Returns:
- the value read
- Throws:
- IllegalArgumentException- if the part of the byte array starting at offset does not have at least 4 bytes
 
- 
readSwappedFloatReads a little endianfloatvalue from an InputStream.- Parameters:
- input- source InputStream
- Returns:
- the value just read
- Throws:
- IOException- in case of an I/O problem
 
- 
readSwappedIntegerReads a little endianintvalue from a byte array at a given offset.- Parameters:
- data- source byte array
- offset- starting offset in the byte array
- Returns:
- the value read
- Throws:
- IllegalArgumentException- if the part of the byte array starting at offset does not have at least 4 bytes
 
- 
readSwappedIntegerReads a little endianintvalue from an InputStream.- Parameters:
- input- source InputStream
- Returns:
- the value just read
- Throws:
- IOException- in case of an I/O problem
 
- 
readSwappedLongReads a little endianlongvalue from a byte array at a given offset.- Parameters:
- data- source byte array
- offset- starting offset in the byte array
- Returns:
- the value read
- Throws:
- IllegalArgumentException- if the part of the byte array starting at offset does not have at least 8 bytes
 
- 
readSwappedLongReads a little endianlongvalue from an InputStream.- Parameters:
- input- source InputStream
- Returns:
- the value just read
- Throws:
- IOException- in case of an I/O problem
 
- 
readSwappedShortReads a little endianshortvalue from a byte array at a given offset.- Parameters:
- data- source byte array
- offset- starting offset in the byte array
- Returns:
- the value read
- Throws:
- IllegalArgumentException- if the part of the byte array starting at offset does not have at least 2 bytes
 
- 
readSwappedShortReads a little endianshortvalue from an InputStream.- Parameters:
- input- source InputStream
- Returns:
- the value just read
- Throws:
- IOException- in case of an I/O problem
 
- 
readSwappedUnsignedIntegerReads a little endian unsigned integer (32-bit) value from a byte array at a given offset.- Parameters:
- data- source byte array
- offset- starting offset in the byte array
- Returns:
- the value read
- Throws:
- IllegalArgumentException- if the part of the byte array starting at offset does not have at least 4 bytes
 
- 
readSwappedUnsignedIntegerReads a little endian unsigned integer (32-bit) from an InputStream.- Parameters:
- input- source InputStream
- Returns:
- the value just read
- Throws:
- IOException- in case of an I/O problem
 
- 
readSwappedUnsignedShortReads an unsigned short (16-bit) value from a byte array in little endian order at a given offset.- Parameters:
- data- source byte array
- offset- starting offset in the byte array
- Returns:
- the value read
- Throws:
- IllegalArgumentException- if the part of the byte array starting at offset does not have at least 2 bytes
 
- 
readSwappedUnsignedShortReads an unsigned short (16-bit) from an InputStream in little endian order.- Parameters:
- input- source InputStream
- Returns:
- the value just read
- Throws:
- IOException- in case of an I/O problem
 
- 
swapDoubleConverts 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
 
- 
swapFloatConverts afloatvalue from big endian to little endian and vice versa.- Parameters:
- value- value to convert
- Returns:
- the converted value
 
- 
swapIntegerConverts anintvalue from big endian to little endian and vice versa.- Parameters:
- value- value to convert
- Returns:
- the converted value
 
- 
swapLongConverts alongvalue from big endian to little endian and vice versa.- Parameters:
- value- value to convert
- Returns:
- the converted value
 
- 
swapShortConverts ashortvalue from big endian to little endian and vice versa.- Parameters:
- value- value to convert
- Returns:
- the converted value
 
- 
writeSwappedDoubleWrites the 8 bytes of adoubleto a byte array at a given offset in little endian order.- Parameters:
- data- target byte array
- offset- starting offset in the byte array
- value- value to write
- Throws:
- IllegalArgumentException- if the part of the byte array starting at offset does not have at least 8 bytes
 
- 
writeSwappedDoubleWrites the 8 bytes of adoubleto an output stream in little endian order.- Parameters:
- output- target OutputStream
- value- value to write
- Throws:
- IOException- in case of an I/O problem
 
- 
writeSwappedFloatWrites the 4 bytes of afloatto a byte array at a given offset in little endian order.- Parameters:
- data- target byte array
- offset- starting offset in the byte array
- value- value to write
- Throws:
- IllegalArgumentException- if the part of the byte array starting at offset does not have at least 4 bytes
 
- 
writeSwappedFloatWrites the 4 bytes of afloatto an output stream in little endian order.- Parameters:
- output- target OutputStream
- value- value to write
- Throws:
- IOException- in case of an I/O problem
 
- 
writeSwappedIntegerWrites the 4 bytes of anintto a byte array at a given offset in little endian order.- Parameters:
- data- target byte array
- offset- starting offset in the byte array
- value- value to write
- Throws:
- IllegalArgumentException- if the part of the byte array starting at offset does not have at least 4 bytes
 
- 
writeSwappedIntegerWrites the 4 bytes of anintto an output stream in little endian order.- Parameters:
- output- target OutputStream
- value- value to write
- Throws:
- IOException- in case of an I/O problem
 
- 
writeSwappedLongWrites the 8 bytes of alongto a byte array at a given offset in little endian order.- Parameters:
- data- target byte array
- offset- starting offset in the byte array
- value- value to write
- Throws:
- IllegalArgumentException- if the part of the byte array starting at offset does not have at least 8 bytes
 
- 
writeSwappedLongWrites the 8 bytes of alongto an output stream in little endian order.- Parameters:
- output- target OutputStream
- value- value to write
- Throws:
- IOException- in case of an I/O problem
 
- 
writeSwappedShortWrites the 2 bytes of ashortto a byte array at a given offset in little endian order.- Parameters:
- data- target byte array
- offset- starting offset in the byte array
- value- value to write
- Throws:
- IllegalArgumentException- if the part of the byte array starting at offset does not have at least 2 bytes
 
- 
writeSwappedShortWrites the 2 bytes of ashortto an output stream using little endian encoding.- Parameters:
- output- target OutputStream
- value- value to write
- Throws:
- IOException- in case of an I/O problem
 
 
-