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 voidSwap the byte order in a byte array.static voidSwap the byte order in a byte array.static doubleswapDouble(byte[] eightBytes, 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 doubleswapDouble(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 floatSwap the byte order (if necessary) in a four-byte array and return the value represented by the bit pattern as a float.static floatswapFloat(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 voidSwap the byte order (if necessary) in an array of float values "in-place".static int[]swapInteger(byte[] bytes, int nInt, 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 intswapInteger(byte[] fourBytes, 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 intswapInteger(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 voidswapInteger(int[] iArray, ByteOrder byteOrder) Swap the byte order (if necessary) in an array of int values "in-place".static longSwap the byte order (if necessary) in a eight-byte array and return the value represented by the bit pattern as a long.static longswapLong(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 shortSwap the byte order (if necessary) in a two-byte array and return the value represented by the bit pattern as a short.static shortswapShort(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 voidswapShort(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 voidSwap the byte order (if necessary) in an array of short values "in-place".static longswapUInteger(byte[] fourBytes, 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 intswapUShort(byte[] twoBytes, 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.
-
Method Details
-
swapShort
public static short swapShort(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.
-
swapShort
public static void swapShort(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.
-
swapShort
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.
-
swapShort
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.
-
swapShort
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.
-
swapShort
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.
-
swapUShort
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.
-
swapInteger
public static int swapInteger(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.
-
swapInteger
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.
-
swapInteger
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.
-
swapInteger
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.
-
swapUInteger
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.
-
swapLong
public static long swapLong(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.
-
swapLong
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.
-
swapFloat
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.
-
swapFloat
public static float swapFloat(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.
-
swapFloat
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.
-
swapDouble
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.
-
swapDouble
public static double swapDouble(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.
-