paillierp
Class ByteUtils

java.lang.Object
  extended by paillierp.ByteUtils

public class ByteUtils
extends java.lang.Object

A set of static utilities for manipulating Bytes. The methods used in this class are used primarily for encoding integers and BigIntegers into byte arrays and vice versa.

Author:
James Garrity

Field Summary
static java.lang.String[] base2
          A string array for representing binary numbers.
 
Constructor Summary
ByteUtils()
           
 
Method Summary
static byte[] appendBigInt(byte[] b, java.math.BigInteger... bigIntegers)
          Returns a byte array where the first bytes are b, followed by each of bigIntegers in byte array form, each with its length encoded in 4 bytes.
static byte[] appendInt(byte[] b, int... ints)
          Returns a byte array where the first bytes are b, followed by each of ints in 4-byte array form.
static java.math.BigInteger getBigInt(byte[] b, int offset, int size)
          Creates a BigInteger equivalent to the size bytes given in b starting at offset.
static int getInt(byte[] b, int offset)
          Determines the integer given in the first four bytes of a given array starting with the offset.
static byte[] getLowerLayer(byte[] b)
          Returns the first x bytes of b, where x is the base 10 equivalent to the last four bytes of b.
static byte[] intToByte(int i)
          Converts an integer into its 4-byte equivalent in Big-endan form.
static java.lang.String printByte(byte b)
          Displays the given byte in a string of 0's and 1's.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

base2

public static java.lang.String[] base2
A string array for representing binary numbers.

Constructor Detail

ByteUtils

public ByteUtils()
Method Detail

intToByte

public static byte[] intToByte(int i)
Converts an integer into its 4-byte equivalent in Big-endan form.

Parameters:
i - 32 bit integer
Returns:
a byte array of size 4 with most sig. bits first

getInt

public static int getInt(byte[] b,
                         int offset)
Determines the integer given in the first four bytes of a given array starting with the offset.

Parameters:
b - Byte array of size greater than 4
offset - Index of b
Returns:
an integer equivalent to the four subsequent bytes starting at offset

getBigInt

public static java.math.BigInteger getBigInt(byte[] b,
                                             int offset,
                                             int size)
Creates a BigInteger equivalent to the size bytes given in b starting at offset.

Parameters:
b - Byte array
offset - Index of b; where to start
size - how long to go
Returns:
a BigInteger equivalent to the subsequent bytes starting at offset

getLowerLayer

public static byte[] getLowerLayer(byte[] b)
Returns the first x bytes of b, where x is the base 10 equivalent to the last four bytes of b.

Parameters:
b - A byte array, the last four of which represent an integer x
Returns:
The first x bytes of b.

appendBigInt

public static byte[] appendBigInt(byte[] b,
                                  java.math.BigInteger... bigIntegers)
Returns a byte array where the first bytes are b, followed by each of bigIntegers in byte array form, each with its length encoded in 4 bytes.

Parameters:
b - A byte array
bigIntegers - BigIntegers i1, ... im
Returns:
A byte array of b || len[ i1 ] || i1 || ... || len[ im ] || im. A byte array of length 0 is returned if (1) the concatenation would be too large for an integer index or (2) b is of length 0.
See Also:
BigInteger.toByteArray()

appendInt

public static byte[] appendInt(byte[] b,
                               int... ints)
Returns a byte array where the first bytes are b, followed by each of ints in 4-byte array form.

Parameters:
b - A byte array
ints - Integers i1, ... im
Returns:
A byte array of b || i1 || ... || im. A byte array of length 0 is returned if (1) the concatenation would be too large for an integer index or (2) b is of length 0.

printByte

public static java.lang.String printByte(byte b)
Displays the given byte in a string of 0's and 1's.

Parameters:
b - A simple byte
Returns:
A string of the binary representation of b