Class ByteSwap

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

public abstract class ByteSwap extends 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

    Modifier and Type
    Method
    Description
    static void
    swapBytes(byte[] bArray, int off, int nBytes, ByteOrder byteOrder)
    Swap the byte order in a byte array.
    static void
    swapBytes(byte[] bArray, ByteOrder byteOrder)
    Swap the byte order in a byte array.
    static double
    swapDouble(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 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.
    static float
    swapFloat(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 a float.
    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.
    static void
    swapFloat(float[] fArray, ByteOrder byteOrder)
    Swap 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 int
    swapInteger(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 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.
    static void
    swapInteger(int[] iArray, ByteOrder byteOrder)
    Swap the byte order (if necessary) in an array of int values "in-place".
    static long
    swapLong(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 long.
    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.
    static short[]
    swapShort(byte[] bytes, int nShort, 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
    swapShort(byte[] twoBytes, 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
    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.
    static void
    swapShort(short[] sArray)
    Swap the byte order in an an array of shorts in place.
    static short[]
    swapShort(short[] sArray, int nShorts, ByteOrder byteOrder)
    Swap the byte order (if necessary) in an array of short values returning a new array.
    static void
    swapShort(short[] sArray, ByteOrder byteOrder)
    Swap the byte order (if necessary) in an array of short values "in-place".
    static long
    swapUInteger(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 int
    swapUShort(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.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public static short swapShort(byte[] twoBytes, 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. 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

      public static short[] swapShort(byte[] bytes, int nShort, 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. 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

      public static void swapShort(short[] sArray, ByteOrder byteOrder)
      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

      public static short[] swapShort(short[] sArray, int nShorts, ByteOrder byteOrder)
      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

      public static int swapUShort(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. 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

      public static void swapInteger(int[] iArray, ByteOrder byteOrder)
      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

      public static int swapInteger(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. 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

      public 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. 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

      public static long swapUInteger(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. 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

      public static long swapLong(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 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

      public static void swapFloat(float[] fArray, ByteOrder byteOrder)
      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

      public static float swapFloat(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 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

      public static double swapDouble(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. 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

      public static void swapBytes(byte[] bArray, 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, 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.