Top Description Constructors Methods
org.apache.commons.io

public Class EndianUtils

extends Object
Class Inheritance
Imports
java.io.EOFException, .IOException, .InputStream, .OutputStream

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

Author
Peter Donald
Version
$Id: EndianUtils.java 539638 2007-05-18 23:44:30Z bayard $
See Also
org.apache.commons.io.input.SwappedDataInputStream

Constructor Summary

AccessConstructor and Description
public
EndianUtils()

Instances should NOT be constructed in standard programming.

Method Summary

Modifier and TypeMethod and Description
private static int

Returns:

the byte
read
(InputStream
the stream
input
)

Reads the next byte from the input stream.

public static double

Returns:

the value read
readSwappedDouble
(byte[]
source byte array
data
,
int
starting offset in the byte array
offset
)

Reads a "double" value from a byte array at a given offset.

public static double

Returns:

the value just read
readSwappedDouble
(InputStream
source InputStream
input
)

Reads a "double" value from an InputStream.

public static float

Returns:

the value read
readSwappedFloat
(byte[]
source byte array
data
,
int
starting offset in the byte array
offset
)

Reads a "float" value from a byte array at a given offset.

public static float

Returns:

the value just read
readSwappedFloat
(InputStream
source InputStream
input
)

Reads a "float" value from an InputStream.

public static int

Returns:

the value read
readSwappedInteger
(byte[]
source byte array
data
,
int
starting offset in the byte array
offset
)

Reads a "int" value from a byte array at a given offset.

public static int

Returns:

the value just read
readSwappedInteger
(InputStream
source InputStream
input
)

Reads a "int" value from an InputStream.

public static long

Returns:

the value read
readSwappedLong
(byte[]
source byte array
data
,
int
starting offset in the byte array
offset
)

Reads a "long" value from a byte array at a given offset.

public static long

Returns:

the value just read
readSwappedLong
(InputStream
source InputStream
input
)

Reads a "long" value from an InputStream.

public static short

Returns:

the value read
readSwappedShort
(byte[]
source byte array
data
,
int
starting offset in the byte array
offset
)

Reads a "short" value from a byte array at a given offset.

public static short

Returns:

the value just read
readSwappedShort
(InputStream
source InputStream
input
)

Reads a "short" value from an InputStream.

public static long

Returns:

the value read
readSwappedUnsignedInteger
(byte[]
source byte array
data
,
int
starting offset in the byte array
offset
)

Reads an unsigned integer (32-bit) value from a byte array at a given offset.

public static long

Returns:

the value just read
readSwappedUnsignedInteger
(InputStream
source InputStream
input
)

Reads a unsigned integer (32-bit) from an InputStream.

public static int

Returns:

the value read
readSwappedUnsignedShort
(byte[]
source byte array
data
,
int
starting offset in the byte array
offset
)

Reads an unsigned short (16-bit) value from a byte array at a given offset.

public static int

Returns:

the value just read
readSwappedUnsignedShort
(InputStream
source InputStream
input
)

Reads a unsigned short (16-bit) from an InputStream.

public static double

Returns:

the converted value
swapDouble
(double
value to convert
value
)

Converts a "double" value between endian systems.

public static float

Returns:

the converted value
swapFloat
(float
value to convert
value
)

Converts a "float" value between endian systems.

public static int

Returns:

the converted value
swapInteger
(int
value to convert
value
)

Converts a "int" value between endian systems.

public static long

Returns:

the converted value
swapLong
(long
value to convert
value
)

Converts a "long" value between endian systems.

public static short

Returns:

the converted value
swapShort
(short
value to convert
value
)

Converts a "short" value between endian systems.

public static void
writeSwappedDouble(byte[]
target byte array
data
,
int
starting offset in the byte array
offset
,
double
value to write
value
)

Writes a "double" value to a byte array at a given offset.

public static void
writeSwappedDouble(OutputStream
target OutputStream
output
,
double
value to write
value
)

Writes a "double" value to an OutputStream.

public static void
writeSwappedFloat(byte[]
target byte array
data
,
int
starting offset in the byte array
offset
,
float
value to write
value
)

Writes a "float" value to a byte array at a given offset.

public static void
writeSwappedFloat(OutputStream
target OutputStream
output
,
float
value to write
value
)

Writes a "float" value to an OutputStream.

public static void
writeSwappedInteger(byte[]
target byte array
data
,
int
starting offset in the byte array
offset
,
int
value to write
value
)

Writes a "int" value to a byte array at a given offset.

public static void
writeSwappedInteger(OutputStream
target OutputStream
output
,
int
value to write
value
)

Writes a "int" value to an OutputStream.

public static void
writeSwappedLong(byte[]
target byte array
data
,
int
starting offset in the byte array
offset
,
long
value to write
value
)

Writes a "long" value to a byte array at a given offset.

public static void
writeSwappedLong(OutputStream
target OutputStream
output
,
long
value to write
value
)

Writes a "long" value to an OutputStream.

public static void
writeSwappedShort(byte[]
target byte array
data
,
int
starting offset in the byte array
offset
,
short
value to write
value
)

Writes a "short" value to a byte array at a given offset.

public static void
writeSwappedShort(OutputStream
target OutputStream
output
,
short
value to write
value
)

Writes a "short" value to an OutputStream.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

EndianUtilsback to summary
public EndianUtils()

Instances should NOT be constructed in standard programming.

Method Detail

readback to summary
private static int read(InputStream input) throws IOException

Reads the next byte from the input stream.

Parameters
input:InputStream

the stream

Returns:int

the byte

Exceptions
IOException:
if the end of file is reached
readSwappedDoubleback to summary
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:byte[]

source byte array

offset:int

starting offset in the byte array

Returns:double

the value read

readSwappedDoubleback to summary
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.

Parameters
input:InputStream

source InputStream

Returns:double

the value just read

Exceptions
IOException:
in case of an I/O problem
readSwappedFloatback to summary
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:byte[]

source byte array

offset:int

starting offset in the byte array

Returns:float

the value read

readSwappedFloatback to summary
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.

Parameters
input:InputStream

source InputStream

Returns:float

the value just read

Exceptions
IOException:
in case of an I/O problem
readSwappedIntegerback to summary
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:byte[]

source byte array

offset:int

starting offset in the byte array

Returns:int

the value read

readSwappedIntegerback to summary
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.

Parameters
input:InputStream

source InputStream

Returns:int

the value just read

Exceptions
IOException:
in case of an I/O problem
readSwappedLongback to summary
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:byte[]

source byte array

offset:int

starting offset in the byte array

Returns:long

the value read

readSwappedLongback to summary
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.

Parameters
input:InputStream

source InputStream

Returns:long

the value just read

Exceptions
IOException:
in case of an I/O problem
readSwappedShortback to summary
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:byte[]

source byte array

offset:int

starting offset in the byte array

Returns:short

the value read

readSwappedShortback to summary
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.

Parameters
input:InputStream

source InputStream

Returns:short

the value just read

Exceptions
IOException:
in case of an I/O problem
readSwappedUnsignedIntegerback to summary
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:byte[]

source byte array

offset:int

starting offset in the byte array

Returns:long

the value read

readSwappedUnsignedIntegerback to summary
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.

Parameters
input:InputStream

source InputStream

Returns:long

the value just read

Exceptions
IOException:
in case of an I/O problem
readSwappedUnsignedShortback to summary
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:byte[]

source byte array

offset:int

starting offset in the byte array

Returns:int

the value read

readSwappedUnsignedShortback to summary
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.

Parameters
input:InputStream

source InputStream

Returns:int

the value just read

Exceptions
IOException:
in case of an I/O problem
swapDoubleback to summary
public static double swapDouble(double value)

Converts a "double" value between endian systems.

Parameters
value:double

value to convert

Returns:double

the converted value

swapFloatback to summary
public static float swapFloat(float value)

Converts a "float" value between endian systems.

Parameters
value:float

value to convert

Returns:float

the converted value

swapIntegerback to summary
public static int swapInteger(int value)

Converts a "int" value between endian systems.

Parameters
value:int

value to convert

Returns:int

the converted value

swapLongback to summary
public static long swapLong(long value)

Converts a "long" value between endian systems.

Parameters
value:long

value to convert

Returns:long

the converted value

swapShortback to summary
public static short swapShort(short value)

Converts a "short" value between endian systems.

Parameters
value:short

value to convert

Returns:short

the converted value

writeSwappedDoubleback to summary
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:byte[]

target byte array

offset:int

starting offset in the byte array

value:double

value to write

writeSwappedDoubleback to summary
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.

Parameters
output:OutputStream

target OutputStream

value:double

value to write

Exceptions
IOException:
in case of an I/O problem
writeSwappedFloatback to summary
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:byte[]

target byte array

offset:int

starting offset in the byte array

value:float

value to write

writeSwappedFloatback to summary
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.

Parameters
output:OutputStream

target OutputStream

value:float

value to write

Exceptions
IOException:
in case of an I/O problem
writeSwappedIntegerback to summary
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:byte[]

target byte array

offset:int

starting offset in the byte array

value:int

value to write

writeSwappedIntegerback to summary
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.

Parameters
output:OutputStream

target OutputStream

value:int

value to write

Exceptions
IOException:
in case of an I/O problem
writeSwappedLongback to summary
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:byte[]

target byte array

offset:int

starting offset in the byte array

value:long

value to write

writeSwappedLongback to summary
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.

Parameters
output:OutputStream

target OutputStream

value:long

value to write

Exceptions
IOException:
in case of an I/O problem
writeSwappedShortback to summary
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:byte[]

target byte array

offset:int

starting offset in the byte array

value:short

value to write

writeSwappedShortback to summary
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.

Parameters
output:OutputStream

target OutputStream

value:short

value to write

Exceptions
IOException:
in case of an I/O problem