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 incompatability.
Origin of code: Excalibur
read
private static int read(InputStream input)
throws IOException
Reads the next byte from the input stream.
readSwappedDouble
public static double readSwappedDouble(InputStream input)
throws IOException
Reads a "double" value from an InputStream. The value is
converted to the opposed endian system while reading.
input
- source InputStream
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.
data
- source byte arrayoffset
- starting offset in the byte array
readSwappedFloat
public static float readSwappedFloat(InputStream input)
throws IOException
Reads a "float" value from an InputStream. The value is
converted to the opposed endian system while reading.
input
- source InputStream
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.
data
- source byte arrayoffset
- starting offset in the byte array
readSwappedInteger
public static int readSwappedInteger(InputStream input)
throws IOException
Reads a "int" value from an InputStream. The value is
converted to the opposed endian system while reading.
input
- source InputStream
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.
data
- source byte arrayoffset
- starting offset in the byte array
readSwappedLong
public static long readSwappedLong(InputStream input)
throws IOException
Reads a "long" value from an InputStream. The value is
converted to the opposed endian system while reading.
input
- source InputStream
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.
data
- source byte arrayoffset
- starting offset in the byte array
readSwappedShort
public static short readSwappedShort(InputStream input)
throws IOException
Reads a "short" value from an InputStream. The value is
converted to the opposed endian system while reading.
input
- source InputStream
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.
data
- source byte arrayoffset
- starting offset in the byte array
readSwappedUnsignedInteger
public static long readSwappedUnsignedInteger(InputStream input)
throws IOException
Reads a unsigned integer (32-bit) from an InputStream. The value is
converted to the opposed endian system while reading.
input
- source InputStream
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.
data
- source byte arrayoffset
- starting offset in the byte array
readSwappedUnsignedShort
public static int readSwappedUnsignedShort(InputStream input)
throws IOException
Reads a unsigned short (16-bit) from an InputStream. The value is
converted to the opposed endian system while reading.
input
- source InputStream
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.
data
- source byte arrayoffset
- starting offset in the byte array
swapDouble
public static double swapDouble(double value)
Converts a "double" value between endian systems.
swapFloat
public static float swapFloat(float value)
Converts a "float" value between endian systems.
swapInteger
public static int swapInteger(int value)
Converts a "int" value between endian systems.
swapLong
public static long swapLong(long value)
Converts a "long" value between endian systems.
swapShort
public static short swapShort(short value)
Converts a "short" value between endian systems.
writeSwappedDouble
public static void writeSwappedDouble(OutputStream output,
double value)
throws IOException
Writes a "double" value to an OutputStream. The value is
converted to the opposed endian system while writing.
output
- target OutputStreamvalue
- value to write
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.
data
- target byte arrayoffset
- starting offset in the byte arrayvalue
- value to write
writeSwappedFloat
public static void writeSwappedFloat(OutputStream output,
float value)
throws IOException
Writes a "float" value to an OutputStream. The value is
converted to the opposed endian system while writing.
output
- target OutputStreamvalue
- value to write
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.
data
- target byte arrayoffset
- starting offset in the byte arrayvalue
- value to write
writeSwappedInteger
public static void writeSwappedInteger(OutputStream output,
int value)
throws IOException
Writes a "int" value to an OutputStream. The value is
converted to the opposed endian system while writing.
output
- target OutputStreamvalue
- value to write
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.
data
- target byte arrayoffset
- starting offset in the byte arrayvalue
- value to write
writeSwappedLong
public static void writeSwappedLong(OutputStream output,
long value)
throws IOException
Writes a "long" value to an OutputStream. The value is
converted to the opposed endian system while writing.
output
- target OutputStreamvalue
- value to write
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.
data
- target byte arrayoffset
- starting offset in the byte arrayvalue
- value to write
writeSwappedShort
public static void writeSwappedShort(OutputStream output,
short value)
throws IOException
Writes a "short" value to an OutputStream. The value is
converted to the opposed endian system while writing.
output
- target OutputStreamvalue
- value to write
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.
data
- target byte arrayoffset
- starting offset in the byte arrayvalue
- value to write