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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic double
readSwappedDouble
(byte[] data, int offset) Reads a little endiandouble
value from a byte array at a given offset.static double
readSwappedDouble
(InputStream input) Reads a little endiandouble
value from an InputStream.static float
readSwappedFloat
(byte[] data, int offset) Reads a little endianfloat
value from a byte array at a given offset.static float
readSwappedFloat
(InputStream input) Reads a little endianfloat
value from an InputStream.static int
readSwappedInteger
(byte[] data, int offset) Reads a little endianint
value from a byte array at a given offset.static int
readSwappedInteger
(InputStream input) Reads a little endianint
value from an InputStream.static long
readSwappedLong
(byte[] data, int offset) Reads a little endianlong
value from a byte array at a given offset.static long
readSwappedLong
(InputStream input) Reads a little endianlong
value from an InputStream.static short
readSwappedShort
(byte[] data, int offset) Reads a little endianshort
value from a byte array at a given offset.static short
readSwappedShort
(InputStream input) Reads a little endianshort
value from an InputStream.static long
readSwappedUnsignedInteger
(byte[] data, int offset) Reads a little endian unsigned integer (32-bit) value from a byte array at a given offset.static long
Reads a little endian unsigned integer (32-bit) from an InputStream.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.static int
Reads an unsigned short (16-bit) from an InputStream in little endian order.static double
swapDouble
(double value) Converts adouble
value from big endian to little endian and vice versa.static float
swapFloat
(float value) Converts afloat
value from big endian to little endian and vice versa.static int
swapInteger
(int value) Converts anint
value from big endian to little endian and vice versa.static long
swapLong
(long value) Converts along
value from big endian to little endian and vice versa.static short
swapShort
(short value) Converts ashort
value from big endian to little endian and vice versa.static void
writeSwappedDouble
(byte[] data, int offset, double value) Writes the 8 bytes of adouble
to a byte array at a given offset in little endian order.static void
writeSwappedDouble
(OutputStream output, double value) Writes the 8 bytes of adouble
to an output stream in little endian order.static void
writeSwappedFloat
(byte[] data, int offset, float value) Writes the 4 bytes of afloat
to a byte array at a given offset in little endian order.static void
writeSwappedFloat
(OutputStream output, float value) Writes the 4 bytes of afloat
to an output stream in little endian order.static void
writeSwappedInteger
(byte[] data, int offset, int value) Writes the 4 bytes of anint
to a byte array at a given offset in little endian order.static void
writeSwappedInteger
(OutputStream output, int value) Writes the 4 bytes of anint
to an output stream in little endian order.static void
writeSwappedLong
(byte[] data, int offset, long value) Writes the 8 bytes of along
to a byte array at a given offset in little endian order.static void
writeSwappedLong
(OutputStream output, long value) Writes the 8 bytes of along
to an output stream in little endian order.static void
writeSwappedShort
(byte[] data, int offset, short value) Writes the 2 bytes of ashort
to a byte array at a given offset in little endian order.static void
writeSwappedShort
(OutputStream output, short value) Writes the 2 bytes of ashort
to an output stream using little endian encoding.
-
Constructor Details
-
EndianUtils
Deprecated.TODO Make private in 3.0.Instances should NOT be constructed in standard programming.
-
-
Method Details
-
readSwappedDouble
Reads a little endiandouble
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:
IllegalArgumentException
- if the part of the byte array starting at offset does not have at least 8 bytes
-
readSwappedDouble
Reads a little endiandouble
value from an InputStream.- Parameters:
input
- source InputStream- Returns:
- the value just read
- Throws:
IOException
- in case of an I/O problem
-
readSwappedFloat
Reads a little endianfloat
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:
IllegalArgumentException
- if the part of the byte array starting at offset does not have at least 4 bytes
-
readSwappedFloat
Reads a little endianfloat
value from an InputStream.- Parameters:
input
- source InputStream- Returns:
- the value just read
- Throws:
IOException
- in case of an I/O problem
-
readSwappedInteger
Reads a little endianint
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:
IllegalArgumentException
- if the part of the byte array starting at offset does not have at least 4 bytes
-
readSwappedInteger
Reads a little endianint
value from an InputStream.- Parameters:
input
- source InputStream- Returns:
- the value just read
- Throws:
IOException
- in case of an I/O problem
-
readSwappedLong
Reads a little endianlong
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:
IllegalArgumentException
- if the part of the byte array starting at offset does not have at least 8 bytes
-
readSwappedLong
Reads a little endianlong
value from an InputStream.- Parameters:
input
- source InputStream- Returns:
- the value just read
- Throws:
IOException
- in case of an I/O problem
-
readSwappedShort
Reads a little endianshort
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:
IllegalArgumentException
- if the part of the byte array starting at offset does not have at least 2 bytes
-
readSwappedShort
Reads a little endianshort
value from an InputStream.- Parameters:
input
- source InputStream- Returns:
- the value just read
- Throws:
IOException
- in case of an I/O problem
-
readSwappedUnsignedInteger
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:
IllegalArgumentException
- if the part of the byte array starting at offset does not have at least 4 bytes
-
readSwappedUnsignedInteger
Reads 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
-
readSwappedUnsignedShort
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:
IllegalArgumentException
- if the part of the byte array starting at offset does not have at least 2 bytes
-
readSwappedUnsignedShort
Reads 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
-
swapDouble
Converts adouble
value from big endian to little endian and vice versa. That is, it converts thedouble
to 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
Converts afloat
value from big endian to little endian and vice versa.- Parameters:
value
- value to convert- Returns:
- the converted value
-
swapInteger
Converts anint
value from big endian to little endian and vice versa.- Parameters:
value
- value to convert- Returns:
- the converted value
-
swapLong
Converts along
value from big endian to little endian and vice versa.- Parameters:
value
- value to convert- Returns:
- the converted value
-
swapShort
Converts ashort
value from big endian to little endian and vice versa.- Parameters:
value
- value to convert- Returns:
- the converted value
-
writeSwappedDouble
Writes the 8 bytes of adouble
to 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:
IllegalArgumentException
- if the part of the byte array starting at offset does not have at least 8 bytes
-
writeSwappedDouble
Writes the 8 bytes of adouble
to an output stream in little endian order.- Parameters:
output
- target OutputStreamvalue
- value to write- Throws:
IOException
- in case of an I/O problem
-
writeSwappedFloat
Writes the 4 bytes of afloat
to 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:
IllegalArgumentException
- if the part of the byte array starting at offset does not have at least 4 bytes
-
writeSwappedFloat
Writes the 4 bytes of afloat
to an output stream in little endian order.- Parameters:
output
- target OutputStreamvalue
- value to write- Throws:
IOException
- in case of an I/O problem
-
writeSwappedInteger
Writes the 4 bytes of anint
to 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:
IllegalArgumentException
- if the part of the byte array starting at offset does not have at least 4 bytes
-
writeSwappedInteger
Writes the 4 bytes of anint
to an output stream in little endian order.- Parameters:
output
- target OutputStreamvalue
- value to write- Throws:
IOException
- in case of an I/O problem
-
writeSwappedLong
Writes the 8 bytes of along
to 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:
IllegalArgumentException
- if the part of the byte array starting at offset does not have at least 8 bytes
-
writeSwappedLong
Writes the 8 bytes of along
to an output stream in little endian order.- Parameters:
output
- target OutputStreamvalue
- value to write- Throws:
IOException
- in case of an I/O problem
-
writeSwappedShort
Writes the 2 bytes of ashort
to 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:
IllegalArgumentException
- if the part of the byte array starting at offset does not have at least 2 bytes
-
writeSwappedShort
Writes the 2 bytes of ashort
to an output stream using little endian encoding.- Parameters:
output
- target OutputStreamvalue
- value to write- Throws:
IOException
- in case of an I/O problem
-