public class EndianUtils
extends java.lang.Object
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
SwappedDataInputStream| Constructor and Description |
|---|
EndianUtils()
Deprecated.
TODO Make private in 3.0.
|
| Modifier and Type | Method and Description |
|---|---|
private static int |
read(java.io.InputStream input)
Reads the next byte from the input stream.
|
static double |
readSwappedDouble(byte[] data,
int offset)
Reads a little endian
double value from a byte array at a given offset. |
static double |
readSwappedDouble(java.io.InputStream input)
Reads a little endian
double value from an InputStream. |
static float |
readSwappedFloat(byte[] data,
int offset)
Reads a little endian
float value from a byte array at a given offset. |
static float |
readSwappedFloat(java.io.InputStream input)
Reads a little endian
float value from an InputStream. |
static int |
readSwappedInteger(byte[] data,
int offset)
Reads a little endian
int value from a byte array at a given offset. |
static int |
readSwappedInteger(java.io.InputStream input)
Reads a little endian
int value from an InputStream. |
static long |
readSwappedLong(byte[] data,
int offset)
Reads a little endian
long value from a byte array at a given offset. |
static long |
readSwappedLong(java.io.InputStream input)
Reads a little endian
long value from an InputStream. |
static short |
readSwappedShort(byte[] data,
int offset)
Reads a little endian
short value from a byte array at a given offset. |
static short |
readSwappedShort(java.io.InputStream input)
Reads a little endian
short 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 |
readSwappedUnsignedInteger(java.io.InputStream input)
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 |
readSwappedUnsignedShort(java.io.InputStream input)
Reads an unsigned short (16-bit) from an InputStream in little endian order.
|
static double |
swapDouble(double value)
Converts a
double value from big endian to little endian
and vice versa. |
static float |
swapFloat(float value)
Converts a
float value from big endian to little endian and vice versa. |
static int |
swapInteger(int value)
Converts an
int value from big endian to little endian and vice versa. |
static long |
swapLong(long value)
Converts a
long value from big endian to little endian and vice versa. |
static short |
swapShort(short value)
Converts a
short value from big endian to little endian and vice versa. |
private static void |
validateByteArrayOffset(byte[] data,
int offset,
int byteNeeded)
Validates if the provided byte array has enough data.
|
static void |
writeSwappedDouble(byte[] data,
int offset,
double value)
Writes the 8 bytes of a
double to a byte array at a given offset in little endian order. |
static void |
writeSwappedDouble(java.io.OutputStream output,
double value)
Writes the 8 bytes of a
double to an output stream in little endian order. |
static void |
writeSwappedFloat(byte[] data,
int offset,
float value)
Writes the 4 bytes of a
float to a byte array at a given offset in little endian order. |
static void |
writeSwappedFloat(java.io.OutputStream output,
float value)
Writes the 4 bytes of a
float to an output stream in little endian order. |
static void |
writeSwappedInteger(byte[] data,
int offset,
int value)
Writes the 4 bytes of an
int to a byte array at a given offset in little endian order. |
static void |
writeSwappedInteger(java.io.OutputStream output,
int value)
Writes the 4 bytes of an
int to an output stream in little endian order. |
static void |
writeSwappedLong(byte[] data,
int offset,
long value)
Writes the 8 bytes of a
long to a byte array at a given offset in little endian order. |
static void |
writeSwappedLong(java.io.OutputStream output,
long value)
Writes the 8 bytes of a
long to an output stream in little endian order. |
static void |
writeSwappedShort(byte[] data,
int offset,
short value)
Writes the 2 bytes of a
short to a byte array at a given offset in little endian order. |
static void |
writeSwappedShort(java.io.OutputStream output,
short value)
Writes the 2 bytes of a
short to an output stream using little endian encoding. |
@Deprecated public EndianUtils()
private static int read(java.io.InputStream input)
throws java.io.IOException
input - the streamjava.io.IOException - if the end of file is reachedpublic static double readSwappedDouble(byte[] data,
int offset)
double value from a byte array at a given offset.data - source byte arrayoffset - starting offset in the byte arrayjava.lang.IllegalArgumentException - if the part of the byte array starting at offset does not have at least 8 bytespublic static double readSwappedDouble(java.io.InputStream input)
throws java.io.IOException
double value from an InputStream.input - source InputStreamjava.io.IOException - in case of an I/O problempublic static float readSwappedFloat(byte[] data,
int offset)
float value from a byte array at a given offset.data - source byte arrayoffset - starting offset in the byte arrayjava.lang.IllegalArgumentException - if the part of the byte array starting at offset does not have at least 4 bytespublic static float readSwappedFloat(java.io.InputStream input)
throws java.io.IOException
float value from an InputStream.input - source InputStreamjava.io.IOException - in case of an I/O problempublic static int readSwappedInteger(byte[] data,
int offset)
int value from a byte array at a given offset.data - source byte arrayoffset - starting offset in the byte arrayjava.lang.IllegalArgumentException - if the part of the byte array starting at offset does not have at least 4 bytespublic static int readSwappedInteger(java.io.InputStream input)
throws java.io.IOException
int value from an InputStream.input - source InputStreamjava.io.IOException - in case of an I/O problempublic static long readSwappedLong(byte[] data,
int offset)
long value from a byte array at a given offset.data - source byte arrayoffset - starting offset in the byte arrayjava.lang.IllegalArgumentException - if the part of the byte array starting at offset does not have at least 8 bytespublic static long readSwappedLong(java.io.InputStream input)
throws java.io.IOException
long value from an InputStream.input - source InputStreamjava.io.IOException - in case of an I/O problempublic static short readSwappedShort(byte[] data,
int offset)
short value from a byte array at a given offset.data - source byte arrayoffset - starting offset in the byte arrayjava.lang.IllegalArgumentException - if the part of the byte array starting at offset does not have at least 2 bytespublic static short readSwappedShort(java.io.InputStream input)
throws java.io.IOException
short value from an InputStream.input - source InputStreamjava.io.IOException - in case of an I/O problempublic static long readSwappedUnsignedInteger(byte[] data,
int offset)
data - source byte arrayoffset - starting offset in the byte arrayjava.lang.IllegalArgumentException - if the part of the byte array starting at offset does not have at least 4 bytespublic static long readSwappedUnsignedInteger(java.io.InputStream input)
throws java.io.IOException
input - source InputStreamjava.io.IOException - in case of an I/O problempublic static int readSwappedUnsignedShort(byte[] data,
int offset)
data - source byte arrayoffset - starting offset in the byte arrayjava.lang.IllegalArgumentException - if the part of the byte array starting at offset does not have at least 2 bytespublic static int readSwappedUnsignedShort(java.io.InputStream input)
throws java.io.IOException
input - source InputStreamjava.io.IOException - in case of an I/O problempublic static double swapDouble(double value)
double value from big endian to little endian
and vice versa. That is, it converts the double to bytes,
reverses the bytes, and then reinterprets those bytes as a new double.
This can be useful if you have a number that was read from the
underlying source in the wrong endianness.value - value to convertpublic static float swapFloat(float value)
float value from big endian to little endian and vice versa.value - value to convertpublic static int swapInteger(int value)
int value from big endian to little endian and vice versa.value - value to convertpublic static long swapLong(long value)
long value from big endian to little endian and vice versa.value - value to convertpublic static short swapShort(short value)
short value from big endian to little endian and vice versa.value - value to convertprivate static void validateByteArrayOffset(byte[] data,
int offset,
int byteNeeded)
data - the input byte arrayoffset - the input offsetbyteNeeded - the needed number of bytesjava.lang.IllegalArgumentException - if the byte array does not have enough datapublic static void writeSwappedDouble(byte[] data,
int offset,
double value)
double to a byte array at a given offset in little endian order.data - target byte arrayoffset - starting offset in the byte arrayvalue - value to writejava.lang.IllegalArgumentException - if the part of the byte array starting at offset does not have at least 8 bytespublic static void writeSwappedDouble(java.io.OutputStream output,
double value)
throws java.io.IOException
double to an output stream in little endian order.output - target OutputStreamvalue - value to writejava.io.IOException - in case of an I/O problempublic static void writeSwappedFloat(byte[] data,
int offset,
float value)
float to a byte array at a given offset in little endian order.data - target byte arrayoffset - starting offset in the byte arrayvalue - value to writejava.lang.IllegalArgumentException - if the part of the byte array starting at offset does not have at least 4 bytespublic static void writeSwappedFloat(java.io.OutputStream output,
float value)
throws java.io.IOException
float to an output stream in little endian order.output - target OutputStreamvalue - value to writejava.io.IOException - in case of an I/O problempublic static void writeSwappedInteger(byte[] data,
int offset,
int value)
int to a byte array at a given offset in little endian order.data - target byte arrayoffset - starting offset in the byte arrayvalue - value to writejava.lang.IllegalArgumentException - if the part of the byte array starting at offset does not have at least 4 bytespublic static void writeSwappedInteger(java.io.OutputStream output,
int value)
throws java.io.IOException
int to an output stream in little endian order.output - target OutputStreamvalue - value to writejava.io.IOException - in case of an I/O problempublic static void writeSwappedLong(byte[] data,
int offset,
long value)
long to a byte array at a given offset in little endian order.data - target byte arrayoffset - starting offset in the byte arrayvalue - value to writejava.lang.IllegalArgumentException - if the part of the byte array starting at offset does not have at least 8 bytespublic static void writeSwappedLong(java.io.OutputStream output,
long value)
throws java.io.IOException
long to an output stream in little endian order.output - target OutputStreamvalue - value to writejava.io.IOException - in case of an I/O problempublic static void writeSwappedShort(byte[] data,
int offset,
short value)
short to a byte array at a given offset in little endian order.data - target byte arrayoffset - starting offset in the byte arrayvalue - value to writejava.lang.IllegalArgumentException - if the part of the byte array starting at offset does not have at least 2 bytespublic static void writeSwappedShort(java.io.OutputStream output,
short value)
throws java.io.IOException
short to an output stream using little endian encoding.output - target OutputStreamvalue - value to writejava.io.IOException - in case of an I/O problem