Package org.apache.commons.io
Class EndianUtils
java.lang.Object
org.apache.commons.io.EndianUtils
Utility code for dealing with different endian systems.
Different computer architectures adopt different conventions for byte ordering. In so-called "Little Endian" architectures (eg Intel), the low-order byte is stored in memory at the lowest address, and subsequent bytes at higher addresses. For "Big Endian" architectures (eg Motorola), the situation is reversed. This class helps you solve this incompatibility.
Origin of code: Excalibur
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic doublereadSwappedDouble(byte[] data, int offset) Reads a "double" value from a byte array at a given offset.static doublereadSwappedDouble(InputStream input) Reads a "double" value from an InputStream.static floatreadSwappedFloat(byte[] data, int offset) Reads a "float" value from a byte array at a given offset.static floatreadSwappedFloat(InputStream input) Reads a "float" value from an InputStream.static intreadSwappedInteger(byte[] data, int offset) Reads a "int" value from a byte array at a given offset.static intreadSwappedInteger(InputStream input) Reads a "int" value from an InputStream.static longreadSwappedLong(byte[] data, int offset) Reads a "long" value from a byte array at a given offset.static longreadSwappedLong(InputStream input) Reads a "long" value from an InputStream.static shortreadSwappedShort(byte[] data, int offset) Reads a "short" value from a byte array at a given offset.static shortreadSwappedShort(InputStream input) Reads a "short" value from an InputStream.static longreadSwappedUnsignedInteger(byte[] data, int offset) Reads an unsigned integer (32-bit) value from a byte array at a given offset.static longReads a unsigned integer (32-bit) from an InputStream.static intreadSwappedUnsignedShort(byte[] data, int offset) Reads an unsigned short (16-bit) value from a byte array at a given offset.static intReads a unsigned short (16-bit) from an InputStream.static doubleswapDouble(double value) Converts a "double" value between endian systems.static floatswapFloat(float value) Converts a "float" value between endian systems.static intswapInteger(int value) Converts a "int" value between endian systems.static longswapLong(long value) Converts a "long" value between endian systems.static shortswapShort(short value) Converts a "short" value between endian systems.static voidwriteSwappedDouble(byte[] data, int offset, double value) Writes a "double" value to a byte array at a given offset.static voidwriteSwappedDouble(OutputStream output, double value) Writes a "double" value to an OutputStream.static voidwriteSwappedFloat(byte[] data, int offset, float value) Writes a "float" value to a byte array at a given offset.static voidwriteSwappedFloat(OutputStream output, float value) Writes a "float" value to an OutputStream.static voidwriteSwappedInteger(byte[] data, int offset, int value) Writes a "int" value to a byte array at a given offset.static voidwriteSwappedInteger(OutputStream output, int value) Writes a "int" value to an OutputStream.static voidwriteSwappedLong(byte[] data, int offset, long value) Writes a "long" value to a byte array at a given offset.static voidwriteSwappedLong(OutputStream output, long value) Writes a "long" value to an OutputStream.static voidwriteSwappedShort(byte[] data, int offset, short value) Writes a "short" value to a byte array at a given offset.static voidwriteSwappedShort(OutputStream output, short value) Writes a "short" value to an OutputStream.
-
Constructor Details
-
EndianUtils
public EndianUtils()Instances should NOT be constructed in standard programming.
-
-
Method Details
-
swapShort
Converts a "short" value between endian systems.- Parameters:
value- value to convert- Returns:
- the converted value
-
swapInteger
Converts a "int" value between endian systems.- Parameters:
value- value to convert- Returns:
- the converted value
-
swapLong
Converts a "long" value between endian systems.- Parameters:
value- value to convert- Returns:
- the converted value
-
swapFloat
Converts a "float" value between endian systems.- Parameters:
value- value to convert- Returns:
- the converted value
-
swapDouble
Converts a "double" value between endian systems.- Parameters:
value- value to convert- Returns:
- the converted value
-
writeSwappedShort
Writes a "short" value to a byte array at a given offset. The value is converted to the opposed endian system while writing.- Parameters:
data- target byte arrayoffset- starting offset in the byte arrayvalue- value to write
-
readSwappedShort
Reads a "short" value from a byte array at a given offset. The value is converted to the opposed endian system while reading.- Parameters:
data- source byte arrayoffset- starting offset in the byte array- Returns:
- the value read
-
readSwappedUnsignedShort
Reads an unsigned short (16-bit) value from a byte array at a given offset. The value is converted to the opposed endian system while reading.- Parameters:
data- source byte arrayoffset- starting offset in the byte array- Returns:
- the value read
-
writeSwappedInteger
Writes a "int" value to a byte array at a given offset. The value is converted to the opposed endian system while writing.- Parameters:
data- target byte arrayoffset- starting offset in the byte arrayvalue- value to write
-
readSwappedInteger
Reads a "int" value from a byte array at a given offset. The value is converted to the opposed endian system while reading.- Parameters:
data- source byte arrayoffset- starting offset in the byte array- Returns:
- the value read
-
readSwappedUnsignedInteger
Reads an unsigned integer (32-bit) value from a byte array at a given offset. The value is converted to the opposed endian system while reading.- Parameters:
data- source byte arrayoffset- starting offset in the byte array- Returns:
- the value read
-
writeSwappedLong
Writes a "long" value to a byte array at a given offset. The value is converted to the opposed endian system while writing.- Parameters:
data- target byte arrayoffset- starting offset in the byte arrayvalue- value to write
-
readSwappedLong
Reads a "long" value from a byte array at a given offset. The value is converted to the opposed endian system while reading.- Parameters:
data- source byte arrayoffset- starting offset in the byte array- Returns:
- the value read
-
writeSwappedFloat
Writes a "float" value to a byte array at a given offset. The value is converted to the opposed endian system while writing.- Parameters:
data- target byte arrayoffset- starting offset in the byte arrayvalue- value to write
-
readSwappedFloat
Reads a "float" value from a byte array at a given offset. The value is converted to the opposed endian system while reading.- Parameters:
data- source byte arrayoffset- starting offset in the byte array- Returns:
- the value read
-
writeSwappedDouble
Writes a "double" value to a byte array at a given offset. The value is converted to the opposed endian system while writing.- Parameters:
data- target byte arrayoffset- starting offset in the byte arrayvalue- value to write
-
readSwappedDouble
Reads a "double" value from a byte array at a given offset. The value is converted to the opposed endian system while reading.- Parameters:
data- source byte arrayoffset- starting offset in the byte array- Returns:
- the value read
-
writeSwappedShort
Writes a "short" value to an OutputStream. The value is converted to the opposed endian system while writing.- Parameters:
output- target OutputStreamvalue- value to write- Throws:
IOException- in case of an I/O problem
-
readSwappedShort
Reads a "short" value from an InputStream. The value is converted to the opposed endian system while reading.- Parameters:
input- source InputStream- Returns:
- the value just read
- Throws:
IOException- in case of an I/O problem
-
readSwappedUnsignedShort
Reads a unsigned short (16-bit) from an InputStream. The value is converted to the opposed endian system while reading.- Parameters:
input- source InputStream- Returns:
- the value just read
- Throws:
IOException- in case of an I/O problem
-
writeSwappedInteger
Writes a "int" value to an OutputStream. The value is converted to the opposed endian system while writing.- Parameters:
output- target OutputStreamvalue- value to write- Throws:
IOException- in case of an I/O problem
-
readSwappedInteger
Reads a "int" value from an InputStream. The value is converted to the opposed endian system while reading.- Parameters:
input- source InputStream- Returns:
- the value just read
- Throws:
IOException- in case of an I/O problem
-
readSwappedUnsignedInteger
Reads a unsigned integer (32-bit) from an InputStream. The value is converted to the opposed endian system while reading.- Parameters:
input- source InputStream- Returns:
- the value just read
- Throws:
IOException- in case of an I/O problem
-
writeSwappedLong
Writes a "long" value to an OutputStream. The value is converted to the opposed endian system while writing.- Parameters:
output- target OutputStreamvalue- value to write- Throws:
IOException- in case of an I/O problem
-
readSwappedLong
Reads a "long" value from an InputStream. The value is converted to the opposed endian system while reading.- Parameters:
input- source InputStream- Returns:
- the value just read
- Throws:
IOException- in case of an I/O problem
-
writeSwappedFloat
Writes a "float" value to an OutputStream. The value is converted to the opposed endian system while writing.- Parameters:
output- target OutputStreamvalue- value to write- Throws:
IOException- in case of an I/O problem
-
readSwappedFloat
Reads a "float" value from an InputStream. The value is converted to the opposed endian system while reading.- Parameters:
input- source InputStream- Returns:
- the value just read
- Throws:
IOException- in case of an I/O problem
-
writeSwappedDouble
Writes a "double" value to an OutputStream. The value is converted to the opposed endian system while writing.- Parameters:
output- target OutputStreamvalue- value to write- Throws:
IOException- in case of an I/O problem
-
readSwappedDouble
Reads a "double" value from an InputStream. The value is converted to the opposed endian system while reading.- Parameters:
input- source InputStream- Returns:
- the value just read
- Throws:
IOException- in case of an I/O problem
-