com.xinapse.io
Class ByteSwap

java.lang.Object
  extended by com.xinapse.io.ByteSwap

public abstract class ByteSwap
extends java.lang.Object

Abstract class to provide byte swapping facilities, allowing correct interpretation of multi-byte numbers on machines of different byte orders. The Java Virtual Machine is a big-endian Machine, and therefore byte order swapping is needed if the data to be read or written is in little-endian byte order.


Method Summary
static double Double(byte[] eightBytes, java.nio.ByteOrder byteOrder)
          Swap the byte order (if necessary) in a eight-byte array and return the value represented by the bit pattern as a double.
static double Double(double d)
          Swap the byte order in a double and return the value represented by the bit pattern of the reversed bytes as a double.
static float Float(byte[] fourBytes, java.nio.ByteOrder byteOrder)
          Swap the byte order (if necessary) in a four-byte array and return the value represented by the bit pattern as a float.
static float Float(float f)
          Swap the byte order in a float and return the value represented by the bit pattern of the reversed bytes as a float.
static int Integer(byte[] fourBytes, java.nio.ByteOrder byteOrder)
          Swap the byte order (if necessary) in a four-byte array and return the value represented by the bit pattern as an int.
static int[] Integer(byte[] bytes, int nInt, java.nio.ByteOrder byteOrder)
          Swap the byte order (if necessary) in quartets of bytes in an array of bytes and return the values represented by the bit patterns as an array of ints.
static int Integer(int i)
          Swap the byte order in an an int and return the value represented by the bit pattern of the reversed bytes as an int.
static long Long(byte[] eightBytes, java.nio.ByteOrder byteOrder)
          Swap the byte order (if necessary) in a eight-byte array and return the value represented by the bit pattern as a long.
static long Long(long l)
          Swap the byte order in a long and return the value represented by the bit pattern of the reversed bytes as a long.
static short Short(byte[] twoBytes, java.nio.ByteOrder byteOrder)
          Swap the byte order (if necessary) in a two-byte array and return the value represented by the bit pattern as a short.
static short[] Short(byte[] bytes, int nShort, java.nio.ByteOrder byteOrder)
          Swap the byte order (if necessary) in pairs of bytes in an array of bytes and return the values represented by the bit patterns as an array of shorts.
static short Short(short s)
          Swap the byte order in an a short and return the value represented by the bit pattern of the reversed bytes as a short.
static void Short(short[] sArray)
          Swap the byte order in an an array of shorts in place.
static void swapBytes(byte[] bArray, java.nio.ByteOrder byteOrder)
          Swap the byte order in a byte array.
static void swapBytes(byte[] bArray, int off, int nBytes, java.nio.ByteOrder byteOrder)
          Swap the byte order in a byte array.
static long UInteger(byte[] fourBytes, java.nio.ByteOrder byteOrder)
          Swap the byte order (if necessary) in a four-byte array and return the value represented by the bit pattern (interpreted as an unsigned int) as a long.
static int UShort(byte[] twoBytes, java.nio.ByteOrder byteOrder)
          Swap the byte order (if necessary) in a two-byte array and return the value represented by the bit pattern (interpreted as an unsigned short) as an int.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

Short

public static short Short(short s)
Swap the byte order in an a short and return the value represented by the bit pattern of the reversed bytes as a short.

Parameters:
s - an short whose byte order is to be reversed.
Returns:
the short value represented by the bit pattern of the argument, but with the byte order reversed.

Short

public static void Short(short[] sArray)
Swap the byte order in an an array of shorts in place.

Parameters:
sArray - an array of shorts whose byte order is to be reversed.

Short

public static short Short(byte[] twoBytes,
                          java.nio.ByteOrder byteOrder)
Swap the byte order (if necessary) in a two-byte array and return the value represented by the bit pattern as a short.

Parameters:
twoBytes - an array of length two, holding the bit pattern for the short value.
byteOrder - the byte order of the data source from which the bytes of data were read. If byteOrder is ByteOrder.LITTLE_ENDIAN, then byte reordering takes place; if byteOrder is ByteOrder.BIG_ENDIAN, then no byte reordering takes place.
Returns:
the short represented by the bit pattern of the byte array argument.

Short

public static short[] Short(byte[] bytes,
                            int nShort,
                            java.nio.ByteOrder byteOrder)
Swap the byte order (if necessary) in pairs of bytes in an array of bytes and return the values represented by the bit patterns as an array of shorts.

Parameters:
bytes - an even-length array of bytes, holding the bit patterns for the short values.
nShort - the number of short values to be converted.
byteOrder - the byte order of the data source from which the bytes of data were read. If byteOrder is ByteOrder.LITTLE_ENDIAN, then byte reordering takes place; if byteOrder is ByteOrder.BIG_ENDIAN, then no byte reordering takes place.
Returns:
an array of short values represented by the bit patterns of the byte array argument.

UShort

public static int UShort(byte[] twoBytes,
                         java.nio.ByteOrder byteOrder)
Swap the byte order (if necessary) in a two-byte array and return the value represented by the bit pattern (interpreted as an unsigned short) as an int.

Parameters:
twoBytes - an array of length two, holding the bit pattern for the unsigned short value.
byteOrder - the byte order of the data source from which the bytes of data were read. If byteOrder is ByteOrder.LITTLE_ENDIAN, then byte reordering takes place; if byteOrder is ByteOrder.BIG_ENDIAN, then no byte reordering takes place.
Returns:
the unsigned short represented by the bit pattern of the byte array argument, as an int.

Integer

public static int Integer(int i)
Swap the byte order in an an int and return the value represented by the bit pattern of the reversed bytes as an int.

Parameters:
i - an int whose byte order is to be reversed.
Returns:
the int represented by the bit pattern of the argument, but with the byte order reversed.

Integer

public static int Integer(byte[] fourBytes,
                          java.nio.ByteOrder byteOrder)
Swap the byte order (if necessary) in a four-byte array and return the value represented by the bit pattern as an int.

Parameters:
fourBytes - an array of length four, holding the bit pattern for the signed integer value.
byteOrder - the byte order of the data source from which the bytes of data were read. If byteOrder is ByteOrder.LITTLE_ENDIAN, then byte reordering takes place; if byteOrder is ByteOrder.BIG_ENDIAN, then no byte reordering takes place.
Returns:
the signed int represented by the bit pattern of the byte array argument, as an int.

Integer

public static int[] Integer(byte[] bytes,
                            int nInt,
                            java.nio.ByteOrder byteOrder)
Swap the byte order (if necessary) in quartets of bytes in an array of bytes and return the values represented by the bit patterns as an array of ints.

Parameters:
bytes - an array of bytes, holding the bit patterns for the int values.
nInt - the number of int values to be converted.
byteOrder - the byte order of the data source from which the bytes of data were read. If byteOrder is ByteOrder.LITTLE_ENDIAN, then byte reordering takes place; if byteOrder is ByteOrder.BIG_ENDIAN, then no byte reordering takes place.
Returns:
an array of int values represented by the bit patterns of the byte array argument.

UInteger

public static long UInteger(byte[] fourBytes,
                            java.nio.ByteOrder byteOrder)
Swap the byte order (if necessary) in a four-byte array and return the value represented by the bit pattern (interpreted as an unsigned int) as a long.

Parameters:
fourBytes - an array of length four, holding the bit pattern for the unsigned integer value.
byteOrder - the byte order of the data source from which the bytes of data were read. If byteOrder is ByteOrder.LITTLE_ENDIAN, then byte reordering takes place; if byteOrder is ByteOrder.BIG_ENDIAN, then no byte reordering takes place.
Returns:
the unsigned int represented by the bit pattern of the byte array argument, as a long.

Long

public static long Long(long l)
Swap the byte order in a long and return the value represented by the bit pattern of the reversed bytes as a long.

Parameters:
l - a long whose byte order is to be reversed.
Returns:
the double represented by the bit pattern of the argument, but with the byte order reversed.

Long

public static long Long(byte[] eightBytes,
                        java.nio.ByteOrder byteOrder)
Swap the byte order (if necessary) in a eight-byte array and return the value represented by the bit pattern as a long.

Parameters:
eightBytes - an array of length eight, holding the bit pattern for the signed long value.
byteOrder - the byte order of the data source from which the bytes of data were read. If byteOrder is ByteOrder.LITTLE_ENDIAN, then byte reordering takes place; if byteOrder is ByteOrder.BIG_ENDIAN, then no byte reordering takes place.
Returns:
the signed long value represented by the bit pattern of the byte array argument, as a long.

Float

public static float Float(float f)
Swap the byte order in a float and return the value represented by the bit pattern of the reversed bytes as a float.

Parameters:
f - a float whose byte order is to be reversed.
Returns:
the float represented by the bit pattern of the argument, but with the byte order reversed.

Float

public static float Float(byte[] fourBytes,
                          java.nio.ByteOrder byteOrder)
Swap the byte order (if necessary) in a four-byte array and return the value represented by the bit pattern as a float.

Parameters:
fourBytes - an array of length four, holding the bit pattern for the float value.
byteOrder - the byte order of the data source from which the bytes of data were read. If byteOrder is ByteOrder.LITTLE_ENDIAN, then byte reordering takes place; if byteOrder is ByteOrder.BIG_ENDIAN, then no byte reordering takes place.
Returns:
the float represented by the bit pattern of the byte array argument, as a float.

Double

public static double Double(byte[] eightBytes,
                            java.nio.ByteOrder byteOrder)
Swap the byte order (if necessary) in a eight-byte array and return the value represented by the bit pattern as a double.

Parameters:
eightBytes - an array of length eight, holding the bit pattern for the double value.
byteOrder - the byte order of the data source from which the bytes of data were read. If byteOrder is ByteOrder.LITTLE_ENDIAN, then byte reordering takes place; if byteOrder is ByteOrder.BIG_ENDIAN, then no byte reordering takes place.
Returns:
the double represented by the bit pattern of the byte array argument, as a double.

Double

public static double Double(double d)
Swap the byte order in a double and return the value represented by the bit pattern of the reversed bytes as a double.

Parameters:
d - a double whose byte order is to be reversed.
Returns:
the double value represented by the bit pattern of the argument, but with the byte order reversed.

swapBytes

public static void swapBytes(byte[] bArray,
                             java.nio.ByteOrder byteOrder)
Swap the byte order in a byte array.

Parameters:
bArray - the byte[] for which to swap the order of bytes.
byteOrder - if ByteOrder.LITTLE_ENDIAN, then byte reordering takes place; if ByteOrder.BIG_ENDIAN, then no byte reordering takes place.

swapBytes

public static void swapBytes(byte[] bArray,
                             int off,
                             int nBytes,
                             java.nio.ByteOrder byteOrder)
Swap the byte order in a byte array.

Parameters:
bArray - the byte[] for which to swap the order of bytes.
off - an offset into the array at which byte swapping starts.
nBytes - the number of bytes to swap.
byteOrder - if ByteOrder.LITTLE_ENDIAN, then byte reordering takes place; if ByteOrder.BIG_ENDIAN, then no byte reordering takes place.


Copyright 2006-2008 Xinapse Systems Limited. All Rights Reserved.