Package edu.iris.dmc.seedcodec
Class Utility
java.lang.Object
edu.iris.dmc.seedcodec.Utility
Generic class providing static methods for converting between integer numbers
and byte arrays.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic 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[]
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 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.
-
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 byteb
- low order byteswapBytes
- 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 byteb
- low order byteswapBytes
- 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 byteb
- second-highest order bytec
- lowest order byteswapBytes
- 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 byteb
- second-highest order bytec
- second-lowest order byted
- lowest order byteswapBytes
- 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 byteb
- second-highest order bytec
- next order byted
- next order bytee
- next order bytef
- next order byteg
- next order byteh
- lowest order byteswapBytes
- 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 byteb
- low order unsigned byteswapBytes
- 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 byteb
- second-highest order bytec
- second-lowest order byted
- lowest order byteswapBytes
- 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 torequiredBytes
- the length in bytes of the returned byte arraypaddingByte
- 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 arraystart
- starting index, inclusiveend
- ending index, exclusive- Returns:
- byte array of length start-end
-