org.apache.commons.io

Class EndianUtils

public class EndianUtils extends Object

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

Version: $Id: EndianUtils.java 539632 2007-05-18 23:37:59Z bayard $

Author: Peter Donald

See Also: SwappedDataInputStream

Constructor Summary
EndianUtils()
Instances should NOT be constructed in standard programming.
Method Summary
static intread(InputStream input)
Reads the next byte from the input stream.
static 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 longreadSwappedUnsignedInteger(InputStream input)
Reads 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 intreadSwappedUnsignedShort(InputStream input)
Reads 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 Detail

EndianUtils

public EndianUtils()
Instances should NOT be constructed in standard programming.

Method Detail

read

private static int read(InputStream input)
Reads the next byte from the input stream.

Parameters: input the stream

Returns: the byte

Throws: IOException if the end of file is reached

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 array offset starting offset in the byte array

Returns: the value read

readSwappedDouble

public static double readSwappedDouble(InputStream input)
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

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 array offset starting offset in the byte array

Returns: the value read

readSwappedFloat

public static float readSwappedFloat(InputStream input)
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

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 array offset starting offset in the byte array

Returns: the value read

readSwappedInteger

public static int readSwappedInteger(InputStream input)
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

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 array offset starting offset in the byte array

Returns: the value read

readSwappedLong

public static long readSwappedLong(InputStream input)
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

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 array offset starting offset in the byte array

Returns: the value read

readSwappedShort

public static short readSwappedShort(InputStream input)
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

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 array offset starting offset in the byte array

Returns: the value read

readSwappedUnsignedInteger

public static long readSwappedUnsignedInteger(InputStream input)
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

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 array offset starting offset in the byte array

Returns: the value read

readSwappedUnsignedShort

public static int readSwappedUnsignedShort(InputStream input)
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

swapDouble

public static double swapDouble(double value)
Converts a "double" 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

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

swapShort

public static short swapShort(short value)
Converts a "short" value between endian systems.

Parameters: value value to convert

Returns: the converted value

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 array offset starting offset in the byte array value value to write

writeSwappedDouble

public static void writeSwappedDouble(OutputStream output, double value)
Writes a "double" value to an OutputStream. The value is converted to the opposed endian system while writing.

Parameters: output target OutputStream value value to write

Throws: IOException in case of an I/O problem

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 array offset starting offset in the byte array value value to write

writeSwappedFloat

public static void writeSwappedFloat(OutputStream output, float value)
Writes a "float" value to an OutputStream. The value is converted to the opposed endian system while writing.

Parameters: output target OutputStream value value to write

Throws: IOException in case of an I/O problem

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 array offset starting offset in the byte array value value to write

writeSwappedInteger

public static void writeSwappedInteger(OutputStream output, int value)
Writes a "int" value to an OutputStream. The value is converted to the opposed endian system while writing.

Parameters: output target OutputStream value value to write

Throws: IOException in case of an I/O problem

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 array offset starting offset in the byte array value value to write

writeSwappedLong

public static void writeSwappedLong(OutputStream output, long value)
Writes a "long" value to an OutputStream. The value is converted to the opposed endian system while writing.

Parameters: output target OutputStream value value to write

Throws: IOException in case of an I/O problem

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 array offset starting offset in the byte array value value to write

writeSwappedShort

public static void writeSwappedShort(OutputStream output, short value)
Writes a "short" value to an OutputStream. The value is converted to the opposed endian system while writing.

Parameters: output target OutputStream value value to write

Throws: IOException in case of an I/O problem

Copyright (c) 2002-2011 Apache Software Foundation