Class Utility

java.lang.Object
edu.iris.dmc.seedcodec.Utility

public class Utility extends Object
Generic class providing static methods for converting between integer numbers and byte arrays.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    bytesToInt(byte a)
    Convert a single byte to a 32-bit int, with sign extension.
    static int
    bytesToInt(byte a, byte b, boolean swapBytes)
    Concatenate two bytes to a 32-bit int value.
    static int
    bytesToInt(byte a, byte b, byte c, boolean swapBytes)
    Concatenate three bytes to a 32-bit int value.
    static int
    bytesToInt(byte a, byte b, byte c, byte d, boolean swapBytes)
    Concatenate four bytes to a 32-bit int value.
    static long
    bytesToLong(byte a, byte b, byte c, byte d, byte e, byte f, byte g, byte h, boolean swapBytes)
    Concatenate eight bytes to a 64-bit int value.
    static short
    bytesToShort(byte a, byte b, boolean swapBytes)
    Concatenate two bytes to a short integer value.
    static byte[]
    format(byte[] source, int start, int end)
    Return a byte array which is a subset of bytes from source beginning with index start and stopping just before index end.
    static byte[]
    Convert an int value to a 2-byte array.
    static byte[]
    Convert a long value to a 4-byte array.
    static byte[]
    pad(byte[] source, int requiredBytes, byte paddingByte)
    Return a byte array of length requiredBytes that contains the contents of source and is padded on the end with paddingByte.
    static int
    uBytesToInt(byte a)
    Treat byte value as an unsigned value and convert to a 32-bit int value.
    static int
    uBytesToInt(byte a, byte b, boolean swapBytes)
    Conatenate two unsigned byte values into a 32-bit integer.
    static long
    uBytesToLong(byte a, byte b, byte c, byte d, boolean swapBytes)
    Conacatenate four unsigned byte values into a long integer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Utility

      public Utility()
  • Method Details

    • bytesToShort

      public static short bytesToShort(byte a, byte b, boolean swapBytes)
      Concatenate two bytes to a short integer value. Accepts a high and low byte to be converted to a 16-bit integer. if swapBytes is true, then b becomes the high order byte.
      Parameters:
      a - high order byte
      b - low order byte
      swapBytes - reverse the roles of the first two parameters
      Returns:
      short integer representation of the concatenated bytes
    • bytesToInt

      public static int bytesToInt(byte a)
      Convert a single byte to a 32-bit int, with sign extension.
      Parameters:
      a - signed byte value
      Returns:
      32-bit integer
    • bytesToInt

      public static int bytesToInt(byte a, byte b, boolean swapBytes)
      Concatenate two bytes to a 32-bit int value. a is the high order byte in the resulting int representation, unless swapBytes is true, in which b is the high order byte.
      Parameters:
      a - high order byte
      b - low order byte
      swapBytes - byte order swap flag
      Returns:
      32-bit integer
    • bytesToInt

      public static int bytesToInt(byte a, byte b, byte c, boolean swapBytes)
      Concatenate three bytes to a 32-bit int value. Byte order is a,b,c unless swapBytes is true, in which case the order is c,b,a.
      Parameters:
      a - highest order byte
      b - second-highest order byte
      c - lowest order byte
      swapBytes - byte order swap flag
      Returns:
      32-bit integer
    • bytesToInt

      public static int bytesToInt(byte a, byte b, byte c, byte d, boolean swapBytes)
      Concatenate four bytes to a 32-bit int value. Byte order is a,b,c,d unless swapBytes is true, in which case the order is d,c,b,a. Note: This method will accept unsigned and signed byte representations, since high bit extension is not a concern here. Java does not support unsigned integers, so the maximum value is not as high as would be the case with an unsigned integer. To hold an unsigned 32-bit value, use uBytesToLong().
      Parameters:
      a - highest order byte
      b - second-highest order byte
      c - second-lowest order byte
      d - lowest order byte
      swapBytes - byte order swap flag
      Returns:
      32-bit integer see edu.iris.Fissures.seed.util.Utility#uBytesToLong(byte,byte,byte,byte,boolean)
    • bytesToLong

      public static long bytesToLong(byte a, byte b, byte c, byte d, byte e, byte f, byte g, byte h, boolean swapBytes)
      Concatenate eight bytes to a 64-bit int value. Byte order is a,b,c,d,e,f,g,h unless swapBytes is true, in which case the order is h,g,f,e,d,c,b,a. Note: This method will accept unsigned and signed byte representations, since high bit extension is not a concern here. Java does not support unsigned long integers, so the maximum value is not as high as would be the case with an unsigned integer.
      Parameters:
      a - highest order byte
      b - second-highest order byte
      c - next order byte
      d - next order byte
      e - next order byte
      f - next order byte
      g - next order byte
      h - lowest order byte
      swapBytes - byte order swap flag
      Returns:
      64-bit long see edu.iris.Fissures.seed.util.Utility#uBytesToLong(byte,byte,byte,byte,boolean)
    • uBytesToInt

      public static int uBytesToInt(byte a)
      Treat byte value as an unsigned value and convert to a 32-bit int value.
      Parameters:
      a - unsigned byte value
      Returns:
      positive 32-bit integer
    • uBytesToInt

      public static int uBytesToInt(byte a, byte b, boolean swapBytes)
      Conatenate two unsigned byte values into a 32-bit integer.
      Parameters:
      a - high order unsigned byte
      b - low order unsigned byte
      swapBytes - if true, b becomes the high order byte
      Returns:
      positive 32-bit integer
    • uBytesToLong

      public static long uBytesToLong(byte a, byte b, byte c, byte d, boolean swapBytes)
      Conacatenate four unsigned byte values into a long integer. This method puts out a long value because a large unsigned 32-bit value would exceed the capacity of an int, which is considered signed in Java.
      Parameters:
      a - highest-order byte
      b - second-highest order byte
      c - second-lowest order byte
      d - lowest order byte
      swapBytes - if true, byte order is d,c,b,a, else order is a,b,c,d
      Returns:
      positive long integer
    • longToIntBytes

      public static byte[] longToIntBytes(long a)
      Convert a long value to a 4-byte array.
      Parameters:
      a - long integer
      Returns:
      byte[4] array
    • intToShortBytes

      public static byte[] intToShortBytes(int a)
      Convert an int value to a 2-byte array.
      Parameters:
      a - int value
      Returns:
      byte[2] array
    • pad

      public static byte[] pad(byte[] source, int requiredBytes, byte paddingByte)
      Return a byte array of length requiredBytes that contains the contents of source and is padded on the end with paddingByte. If requiredBytes is less than or equal to the length of source, then source will simply be returned.
      Parameters:
      source - byte array to have paddingByte(s) appended to
      requiredBytes - the length in bytes of the returned byte array
      paddingByte - the byte value that will be appended to the array to fill out the required byte size of the return array
      Returns:
      byte array of size requiredBytes
    • format

      public static byte[] format(byte[] source, int start, int end)
      Return a byte array which is a subset of bytes from source beginning with index start and stopping just before index end.
      Parameters:
      source - source byte array
      start - starting index, inclusive
      end - ending index, exclusive
      Returns:
      byte array of length start-end