Package com.xinapse.io
Class ByteSwap
java.lang.Object
com.xinapse.io.ByteSwap
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
Modifier and TypeMethodDescriptionstatic double
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
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 void
Swap the byte order (if necessary) in an array of float values "in-place".static int[]
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
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
(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 void
Swap the byte order (if necessary) in an array of int values "in-place".static long
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[]
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
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
(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 short[]
Swap the byte order (if necessary) in an array of short values returning a new array.static void
Swap the byte order (if necessary) in an array of short values "in-place".static void
Swap the byte order in a byte array.static void
Swap the byte order in a byte array.static long
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
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.
-
Method Details
-
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
Swap the byte order (if necessary) in a two-byte array and return the value represented by the bit pattern as a short. The returned short will have a byte order matching the byte order in the Java Virtual Machine, which is BIG_ENDIAN.- 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
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. The returned short[] will have a byte order matching the byte order in the Java Virtual Machine, which is BIG_ENDIAN.- 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.
-
Short
Swap the byte order (if necessary) in an array of short values "in-place". On return, the byte order will match the byte order in the Java Virtual Machine, which is BIG_ENDIAN.- Parameters:
sArray
- an array of short, whose bytes are to be swapped.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.
-
Short
Swap the byte order (if necessary) in an array of short values returning a new array. The byte order in the returned array will match the byte order in the Java Virtual Machine, which is BIG_ENDIAN.- Parameters:
sArray
- an array of short, whose bytes are to be swapped.nShorts
- the number of short values to copy/swap bytes.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:
- a new short[] with byte ordering swapped if necessary.
-
UShort
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. The returned int will have a byte order matching the byte order in the Java Virtual Machine, which is BIG_ENDIAN.- 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
Swap the byte order (if necessary) in an array of int values "in-place". On return, the byte order will match the byte order in the Java Virtual Machine, which is BIG_ENDIAN.- Parameters:
iArray
- an array of int, whose bytes are to be swapped.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.
-
Integer
Swap the byte order (if necessary) in a four-byte array and return the value represented by the bit pattern as an int. The returned int will have a byte order matching the byte order in the Java Virtual Machine, which is BIG_ENDIAN.- 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
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. The returned intt[] will have a byte order matching the byte order in the Java Virtual Machine, which is BIG_ENDIAN.- 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
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. The returned long will have a byte order matching the byte order in the Java Virtual Machine, which is BIG_ENDIAN.- 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
Swap the byte order (if necessary) in a eight-byte array and return the value represented by the bit pattern as a long. The returned long will have a byte order matching the byte order in the Java Virtual Machine, which is BIG_ENDIAN.- 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
Swap the byte order (if necessary) in an array of float values "in-place". On return, the byte order will match the byte order in the Java Virtual Machine, which is BIG_ENDIAN.- Parameters:
fArray
- an array of float, whose bytes are to be swapped.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.
-
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
Swap the byte order (if necessary) in a four-byte array and return the value represented by the bit pattern as a float. The returned float will have a byte order matching the byte order in the Java Virtual Machine, which is BIG_ENDIAN.- 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
Swap the byte order (if necessary) in a eight-byte array and return the value represented by the bit pattern as a double. The returned double will have a byte order matching the byte order in the Java Virtual Machine, which is BIG_ENDIAN.- 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
Swap the byte order in a byte array.- Parameters:
bArray
- the byte[] for which to swap the order of bytes.byteOrder
- ifByteOrder.LITTLE_ENDIAN
, then byte reordering takes place; ifByteOrder.BIG_ENDIAN
, then no byte reordering takes place.
-
swapBytes
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
- ifByteOrder.LITTLE_ENDIAN
, then byte reordering takes place; ifByteOrder.BIG_ENDIAN
, then no byte reordering takes place.
-