Package edu.rit.util

Class Packing

java.lang.Object
edu.rit.util.Packing

public class Packing extends Object
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 Type
    Method
    Description
    static int
    packIntBigEndian(byte[] src, int srcPos)
    Pack bytes from the given array into an integer in big-endian order.
    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.
    static int
    packIntLittleEndian(byte[] src, int srcPos)
    Pack bytes from the given array into an integer in little-endian order.
    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.
    static long
    packLongBigEndian(byte[] src, int srcPos)
    Pack bytes from the given array into a long integer in big-endian order.
    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.
    static long
    packLongLittleEndian(byte[] src, int srcPos)
    Pack bytes from the given array into a long integer in little-endian order.
    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.
    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.
    static void
    unpackIntBigEndian(int src, byte[] dst, int dstPos)
    Unpack the given integer into the given array of bytes in big-endian order.
    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.
    static void
    unpackIntLittleEndian(int src, byte[] dst, int dstPos)
    Unpack the given integer into the given array of bytes in little-endian order.
    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.
    static void
    unpackLongBigEndian(long src, byte[] dst, int dstPos)
    Unpack the given long integer into the given array of bytes in big-endian order.
    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.
    static void
    unpackLongLittleEndian(long src, byte[] dst, int dstPos)
    Unpack the given long integer into the given array of bytes in little-endian order.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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] through src[srcPos+3] packed into an integer in little-endian order.
      Throws:
      NullPointerException - (unchecked exception) Thrown if src is 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] through src[srcPos+3] packed into an integer in big-endian order.
      Throws:
      NullPointerException - (unchecked exception) Thrown if src is 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. Elements src[srcPos] through src[srcPos+4*len-1] are packed into dst[dstPos] through dst[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 if src is null. Thrown if dst is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if packing would cause accessing array elements out of bounds; in this case dst is 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. Elements src[srcPos] through src[srcPos+4*len-1] are packed into dst[dstPos] through dst[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 if src is null. Thrown if dst is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if packing would cause accessing array elements out of bounds; in this case dst is 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] through src[srcPos+7] packed into a long integer in little-endian order.
      Throws:
      NullPointerException - (unchecked exception) Thrown if src is 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] through src[srcPos+7] packed into a long integer in big-endian order.
      Throws:
      NullPointerException - (unchecked exception) Thrown if src is 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. Elements src[srcPos] through src[srcPos+8*len-1] are packed into dst[dstPos] through dst[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 if src is null. Thrown if dst is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if packing would cause accessing array elements out of bounds; in this case dst is 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. Elements src[srcPos] through src[srcPos+8*len-1] are packed into dst[dstPos] through dst[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 if src is null. Thrown if dst is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if packing would cause accessing array elements out of bounds; in this case dst is 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 elements dst[dstPos] through dst[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 if dst is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this case dst is 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 elements dst[dstPos] through dst[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 if dst is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this case dst is 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. Elements src[srcPos] through src[srcPos+len-1] are unpacked into dst[dstPos] through dst[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 if src is null. Thrown if dst is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this case dst is 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. Elements src[srcPos] through src[srcPos+len-1] are unpacked into dst[dstPos] through dst[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 if src is null. Thrown if dst is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this case dst is 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 elements dst[dstPos] through dst[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 if dst is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this case dst is 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 elements dst[dstPos] through dst[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 if dst is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this case dst is 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. Elements src[srcPos] through src[srcPos+len-1] are unpacked into dst[dstPos] through dst[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 if src is null. Thrown if dst is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this case dst is 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. Elements src[srcPos] through src[srcPos+len-1] are unpacked into dst[dstPos] through dst[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 if src is null. Thrown if dst is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if unpacking would cause accessing array elements out of bounds; in this case dst is not altered.