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 TypeMethodDescriptionprivate static int
read
(InputStream input) Reads the next byte from the input stream.static double
readSwappedDouble
(byte[] data, int offset) Reads a "double" value from a byte array at a given offset.static double
readSwappedDouble
(InputStream input) Reads a "double" value from an InputStream.static float
readSwappedFloat
(byte[] data, int offset) Reads a "float" value from a byte array at a given offset.static float
readSwappedFloat
(InputStream input) Reads a "float" value from an InputStream.static int
readSwappedInteger
(byte[] data, int offset) Reads a "int" value from a byte array at a given offset.static int
readSwappedInteger
(InputStream input) Reads a "int" value from an InputStream.static long
readSwappedLong
(byte[] data, int offset) Reads a "long" value from a byte array at a given offset.static long
readSwappedLong
(InputStream input) Reads a "long" value from an InputStream.static short
readSwappedShort
(byte[] data, int offset) Reads a "short" value from a byte array at a given offset.static short
readSwappedShort
(InputStream input) Reads a "short" value from an InputStream.static long
readSwappedUnsignedInteger
(byte[] data, int offset) Reads an unsigned integer (32-bit) value from a byte array at a given offset.static long
Reads a 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 at a given offset.static int
Reads a unsigned short (16-bit) from an InputStream.static double
swapDouble
(double value) Converts a "double" value between endian systems.static float
swapFloat
(float value) Converts a "float" value between endian systems.static int
swapInteger
(int value) Converts a "int" value between endian systems.static long
swapLong
(long value) Converts a "long" value between endian systems.static short
swapShort
(short value) Converts a "short" value between endian systems.static void
writeSwappedDouble
(byte[] data, int offset, double value) Writes a "double" value to a byte array at a given offset.static void
writeSwappedDouble
(OutputStream output, double value) Writes a "double" value to an OutputStream.static void
writeSwappedFloat
(byte[] data, int offset, float value) Writes a "float" value to a byte array at a given offset.static void
writeSwappedFloat
(OutputStream output, float value) Writes a "float" value to an OutputStream.static void
writeSwappedInteger
(byte[] data, int offset, int value) Writes a "int" value to a byte array at a given offset.static void
writeSwappedInteger
(OutputStream output, int value) Writes a "int" value to an OutputStream.static void
writeSwappedLong
(byte[] data, int offset, long value) Writes a "long" value to a byte array at a given offset.static void
writeSwappedLong
(OutputStream output, long value) Writes a "long" value to an OutputStream.static void
writeSwappedShort
(byte[] data, int offset, short value) Writes a "short" value to a byte array at a given offset.static void
writeSwappedShort
(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
public static short swapShort(short value) Converts a "short" value between endian systems.- Parameters:
value
- value to convert- Returns:
- the converted value
-
swapInteger
public static int swapInteger(int value) Converts a "int" value between endian systems.- Parameters:
value
- value to convert- Returns:
- the converted value
-
swapLong
public static long swapLong(long value) Converts a "long" value between endian systems.- Parameters:
value
- value to convert- Returns:
- the converted value
-
swapFloat
public static float swapFloat(float value) Converts a "float" value between endian systems.- Parameters:
value
- value to convert- Returns:
- the converted value
-
swapDouble
public static double swapDouble(double value) Converts a "double" value between endian systems.- Parameters:
value
- value to convert- Returns:
- the converted value
-
writeSwappedShort
public static void writeSwappedShort(byte[] data, int offset, short value) 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
public static short readSwappedShort(byte[] data, int offset) 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
public static int readSwappedUnsignedShort(byte[] data, int offset) 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
public static void writeSwappedInteger(byte[] data, int offset, int value) 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
public static int readSwappedInteger(byte[] data, int offset) 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
public static long readSwappedUnsignedInteger(byte[] data, int offset) 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
public static void writeSwappedLong(byte[] data, int offset, long value) 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
public static long readSwappedLong(byte[] data, int offset) 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
public static void writeSwappedFloat(byte[] data, int offset, float value) 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
public static float readSwappedFloat(byte[] data, int offset) 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
public static void writeSwappedDouble(byte[] data, int offset, double value) 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
public static double readSwappedDouble(byte[] data, int offset) 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
-
read
Reads the next byte from the input stream.- Parameters:
input
- the stream- Returns:
- the byte
- Throws:
IOException
- if the end of file is reached
-