Package edu.rit.util
Class Packing
java.lang.Object
edu.rit.util.Packing
Class Packing provides static methods for packing and unpacking arrays of
bytes into and out of integers, long integers, and arrays thereof.
Note: The operations in class Packing are not multiple thread safe.
- Version:
- 26-Mar-2012
- Author:
- Alan Kaminsky
-
Method Summary
Modifier and TypeMethodDescriptionstatic intpackIntBigEndian(byte[] src, int srcPos) Pack bytes from the given array into an integer in big-endian order.static voidpackIntBigEndian(byte[] src, int srcPos, int[] dst, int dstPos, int len) Pack bytes from the given array into the given array of integers in big-endian order.static intpackIntLittleEndian(byte[] src, int srcPos) Pack bytes from the given array into an integer in little-endian order.static voidpackIntLittleEndian(byte[] src, int srcPos, int[] dst, int dstPos, int len) Pack bytes from the given array into the given array of integers in little-endian order.static longpackLongBigEndian(byte[] src, int srcPos) Pack bytes from the given array into a long integer in big-endian order.static voidpackLongBigEndian(byte[] src, int srcPos, long[] dst, int dstPos, int len) Pack bytes from the given array into the given array of long integers in big-endian order.static longpackLongLittleEndian(byte[] src, int srcPos) Pack bytes from the given array into a long integer in little-endian order.static voidpackLongLittleEndian(byte[] src, int srcPos, long[] dst, int dstPos, int len) Pack bytes from the given array into the given array of long integers in little-endian order.static voidunpackIntBigEndian(int[] src, int srcPos, byte[] dst, int dstPos, int len) Unpack integers from the given array into the given array of bytes in big-endian order.static voidunpackIntBigEndian(int src, byte[] dst, int dstPos) Unpack the given integer into the given array of bytes in big-endian order.static voidunpackIntLittleEndian(int[] src, int srcPos, byte[] dst, int dstPos, int len) Unpack integers from the given array into the given array of bytes in little-endian order.static voidunpackIntLittleEndian(int src, byte[] dst, int dstPos) Unpack the given integer into the given array of bytes in little-endian order.static voidunpackLongBigEndian(long[] src, int srcPos, byte[] dst, int dstPos, int len) Unpack long integers from the given array into the given array of bytes in big-endian order.static voidunpackLongBigEndian(long src, byte[] dst, int dstPos) Unpack the given long integer into the given array of bytes in big-endian order.static voidunpackLongLittleEndian(long[] src, int srcPos, byte[] dst, int dstPos, int len) Unpack long integers from the given array into the given array of bytes in little-endian order.static voidunpackLongLittleEndian(long src, byte[] dst, int dstPos) Unpack the given long integer into the given array of bytes in little-endian order.
-
Method Details
-
packIntLittleEndian
public static int packIntLittleEndian(byte[] src, int srcPos) Pack bytes from the given array into an integer in little-endian order.- Parameters:
src- Source array of bytes to pack.srcPos- Index of first byte to pack.- Returns:
- Elements
src[srcPos]throughsrc[srcPos+3]packed into an integer in little-endian order. - Throws:
NullPointerException- (unchecked exception) Thrown ifsrcis null.IndexOutOfBoundsException- (unchecked exception) Thrown if packing would cause accessing array elements out of bounds.
-
packIntBigEndian
public static int packIntBigEndian(byte[] src, int srcPos) Pack bytes from the given array into an integer in big-endian order.- Parameters:
src- Source array of bytes to pack.srcPos- Index of first byte to pack.- Returns:
- Elements
src[srcPos]throughsrc[srcPos+3]packed into an integer in big-endian order. - Throws:
NullPointerException- (unchecked exception) Thrown ifsrcis null.IndexOutOfBoundsException- (unchecked exception) Thrown if packing would cause accessing array elements out of bounds.
-
packIntLittleEndian
public static void packIntLittleEndian(byte[] src, int srcPos, int[] dst, int dstPos, int len) Pack bytes from the given array into the given array of integers in little-endian order. Elementssrc[srcPos]throughsrc[srcPos+4*len-1]are packed intodst[dstPos]throughdst[dstPos+len-1].- Parameters:
src- Source array of bytes to pack.srcPos- Index of first byte to pack.dst- Destination array of packed integers.dstPos- Index of first packed integer.len- Number of integers (not bytes!) to pack.- Throws:
NullPointerException- (unchecked exception) Thrown ifsrcis null. Thrown ifdstis null.IndexOutOfBoundsException- (unchecked exception) Thrown if packing would cause accessing array elements out of bounds; in this casedstis not altered.
-
packIntBigEndian
public static void packIntBigEndian(byte[] src, int srcPos, int[] dst, int dstPos, int len) Pack bytes from the given array into the given array of integers in big-endian order. Elementssrc[srcPos]throughsrc[srcPos+4*len-1]are packed intodst[dstPos]throughdst[dstPos+len-1].- Parameters:
src- Source array of bytes to pack.srcPos- Index of first byte to pack.dst- Destination array of packed integers.dstPos- Index of first packed integer.len- Number of integers (not bytes!) to pack.- Throws:
NullPointerException- (unchecked exception) Thrown ifsrcis null. Thrown ifdstis null.IndexOutOfBoundsException- (unchecked exception) Thrown if packing would cause accessing array elements out of bounds; in this casedstis not altered.
-
packLongLittleEndian
public static long packLongLittleEndian(byte[] src, int srcPos) Pack bytes from the given array into a long integer in little-endian order.- Parameters:
src- Source array of bytes to pack.srcPos- Index of first byte to pack.- Returns:
- Elements
src[srcPos]throughsrc[srcPos+7]packed into a long integer in little-endian order. - Throws:
NullPointerException- (unchecked exception) Thrown ifsrcis null.IndexOutOfBoundsException- (unchecked exception) Thrown if packing would cause accessing array elements out of bounds.
-
packLongBigEndian
public static long packLongBigEndian(byte[] src, int srcPos) Pack bytes from the given array into a long integer in big-endian order.- Parameters:
src- Source array of bytes to pack.srcPos- Index of first byte to pack.- Returns:
- Elements
src[srcPos]throughsrc[srcPos+7]packed into a long integer in big-endian order. - Throws:
NullPointerException- (unchecked exception) Thrown ifsrcis null.IndexOutOfBoundsException- (unchecked exception) Thrown if packing would cause accessing array elements out of bounds.
-
packLongLittleEndian
public static void packLongLittleEndian(byte[] src, int srcPos, long[] dst, int dstPos, int len) Pack bytes from the given array into the given array of long integers in little-endian order. Elementssrc[srcPos]throughsrc[srcPos+8*len-1]are packed intodst[dstPos]throughdst[dstPos+len-1].- Parameters:
src- Source array of bytes to pack.srcPos- Index of first byte to pack.dst- Destination array of packed long integers.dstPos- Index of first packed long integer.len- Number of long integers (not bytes!) to pack.- Throws:
NullPointerException- (unchecked exception) Thrown ifsrcis null. Thrown ifdstis null.IndexOutOfBoundsException- (unchecked exception) Thrown if packing would cause accessing array elements out of bounds; in this casedstis not altered.
-
packLongBigEndian
public static void packLongBigEndian(byte[] src, int srcPos, long[] dst, int dstPos, int len) Pack bytes from the given array into the given array of long integers in big-endian order. Elementssrc[srcPos]throughsrc[srcPos+8*len-1]are packed intodst[dstPos]throughdst[dstPos+len-1].- Parameters:
src- Source array of bytes to pack.srcPos- Index of first byte to pack.dst- Destination array of packed long integers.dstPos- Index of first packed long integer.len- Number of long integers (not bytes!) to pack.- Throws:
NullPointerException- (unchecked exception) Thrown ifsrcis null. Thrown ifdstis null.IndexOutOfBoundsException- (unchecked exception) Thrown if packing would cause accessing array elements out of bounds; in this casedstis not altered.
-
unpackIntLittleEndian
public static void unpackIntLittleEndian(int src, byte[] dst, int dstPos) Unpack the given integer into the given array of bytes in little-endian order. The integer is unpacked into elementsdst[dstPos]throughdst[dstPos+3].- Parameters:
src- Source integer to unpack.dst- Destination array to receive unpacked bytes.dstPos- Index of first unpacked byte.- Throws:
NullPointerException- (unchecked exception) Thrown ifdstis null.IndexOutOfBoundsException- (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this casedstis not altered.
-
unpackIntBigEndian
public static void unpackIntBigEndian(int src, byte[] dst, int dstPos) Unpack the given integer into the given array of bytes in big-endian order. The integer is unpacked into elementsdst[dstPos]throughdst[dstPos+3].- Parameters:
src- Source integer to unpack.dst- Destination array to receive unpacked bytes.dstPos- Index of first unpacked byte.- Throws:
NullPointerException- (unchecked exception) Thrown ifdstis null.IndexOutOfBoundsException- (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this casedstis not altered.
-
unpackIntLittleEndian
public static void unpackIntLittleEndian(int[] src, int srcPos, byte[] dst, int dstPos, int len) Unpack integers from the given array into the given array of bytes in little-endian order. Elementssrc[srcPos]throughsrc[srcPos+len-1]are unpacked intodst[dstPos]throughdst[dstPos+4*len-1].- Parameters:
src- Source array of integers to unpack.srcPos- Index of first integer to unpack.dst- Destination array to receive unpacked bytes.dstPos- Index of first unpacked byte.len- Number of integers (not bytes!) to unpack.- Throws:
NullPointerException- (unchecked exception) Thrown ifsrcis null. Thrown ifdstis null.IndexOutOfBoundsException- (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this casedstis not altered.
-
unpackIntBigEndian
public static void unpackIntBigEndian(int[] src, int srcPos, byte[] dst, int dstPos, int len) Unpack integers from the given array into the given array of bytes in big-endian order. Elementssrc[srcPos]throughsrc[srcPos+len-1]are unpacked intodst[dstPos]throughdst[dstPos+4*len-1].- Parameters:
src- Source array of integers to unpack.srcPos- Index of first integer to unpack.dst- Destination array to receive unpacked bytes.dstPos- Index of first unpacked byte.len- Number of integers (not bytes!) to unpack.- Throws:
NullPointerException- (unchecked exception) Thrown ifsrcis null. Thrown ifdstis null.IndexOutOfBoundsException- (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this casedstis not altered.
-
unpackLongLittleEndian
public static void unpackLongLittleEndian(long src, byte[] dst, int dstPos) Unpack the given long integer into the given array of bytes in little-endian order. The long integer is unpacked into elementsdst[dstPos]throughdst[dstPos+7].- Parameters:
src- Source long integer to unpack.dst- Destination array to receive unpacked bytes.dstPos- Index of first unpacked byte.- Throws:
NullPointerException- (unchecked exception) Thrown ifdstis null.IndexOutOfBoundsException- (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this casedstis not altered.
-
unpackLongBigEndian
public static void unpackLongBigEndian(long src, byte[] dst, int dstPos) Unpack the given long integer into the given array of bytes in big-endian order. The long integer is unpacked into elementsdst[dstPos]throughdst[dstPos+3].- Parameters:
src- Source long integer to unpack.dst- Destination array to receive unpacked bytes.dstPos- Index of first unpacked byte.- Throws:
NullPointerException- (unchecked exception) Thrown ifdstis null.IndexOutOfBoundsException- (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this casedstis not altered.
-
unpackLongLittleEndian
public static void unpackLongLittleEndian(long[] src, int srcPos, byte[] dst, int dstPos, int len) Unpack long integers from the given array into the given array of bytes in little-endian order. Elementssrc[srcPos]throughsrc[srcPos+len-1]are unpacked intodst[dstPos]throughdst[dstPos+8*len-1].- Parameters:
src- Source array of long integers to unpack.srcPos- Index of first long integer to unpack.dst- Destination array to receive unpacked bytes.dstPos- Index of first unpacked byte.len- Number of integers (not bytes!) to unpack.- Throws:
NullPointerException- (unchecked exception) Thrown ifsrcis null. Thrown ifdstis null.IndexOutOfBoundsException- (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this casedstis not altered.
-
unpackLongBigEndian
public static void unpackLongBigEndian(long[] src, int srcPos, byte[] dst, int dstPos, int len) Unpack long integers from the given array into the given array of bytes in big-endian order. Elementssrc[srcPos]throughsrc[srcPos+len-1]are unpacked intodst[dstPos]throughdst[dstPos+8*len-1].- Parameters:
src- Source array of long integers to unpack.srcPos- Index of first long integer to unpack.dst- Destination array to receive unpacked bytes.dstPos- Index of first unpacked byte.len- Number of integers (not bytes!) to unpack.- Throws:
NullPointerException- (unchecked exception) Thrown ifsrcis null. Thrown ifdstis null.IndexOutOfBoundsException- (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this casedstis not altered.
-