edu.iris.dmc.seedcodec
Class Utility

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

public class Utility
extends java.lang.Object

Generic class providing static methods for converting between integer numbers and byte arrays.


Constructor Summary
Utility()
           
 
Method Summary
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 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[] intToShortBytes(int a)
          Convert an int value to a 2-byte array.
static byte[] longToIntBytes(long a)
          Convert a long value to a 4-byte array.
static void main(java.lang.String[] args)
          Test method.
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 Detail

Utility

public Utility()
Method Detail

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 Also:
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

main

public static void main(java.lang.String[] args)
Test method.

Parameters:
args - not used.