webfunds.token.algorithm
Class EncodeDecodeUtil

java.lang.Object
  |
  +--webfunds.token.algorithm.EncodeDecodeUtil

public final class EncodeDecodeUtil
extends java.lang.Object

Encode and decode utility methods.

This class provides a number of static methods to encode and decode various types and objects. For all of the methods, little-endian encoding will be used.


Method Summary
static byte decodeByte(byte[] data, int start, int len)
          Decode a signed byte.
static byte[] decodeByteArray(byte[] data, int start, int len)
          Decode a Byte Array
static java.util.Date decodeDateTime(byte[] data, int start, int len)
          Decode a date/time
static int decodeInt(byte[] data, int start, int len)
          Decode a signed int.
static long decodeLong(byte[] data, int start, int len)
          Decode a signed long.
static java.math.BigInteger decodeMPI(byte[] data, int start, int len)
          Decode an MPI (Multi Precision Integer, or BigInteger in Java).
static short decodeShort(byte[] data, int start, int len)
          Decode a signed short.
static short decodeUnsignedByte(byte[] data, int start, int len)
          Decode an unsigned byte.
static long decodeUnsignedInt(byte[] data, int start, int len)
          Decode an unsigned int.
static int decodeUnsignedShort(byte[] data, int start, int len)
          Decode an unsigned short.
static void encodeByte(byte x, byte[] data, int start)
          Encode a byte
static int encodeByteArray(byte[] bytes, byte[] data, int start)
          Encode a byte array
static void encodeDateTime(java.util.Date x, byte[] data, int start)
          Encode a date/time value
static void encodeInt(int x, byte[] data, int start)
          Encode an int
static void encodeLong(long x, byte[] data, int start)
          Encode a long
static int encodeMPI(java.math.BigInteger x, byte[] data, int start)
          Encode an MPI (Multi Precision Integer, or BigInteger in Java).
static void encodeShort(short x, byte[] data, int start)
          Encode a short
static int getByteArrayLength(byte[] x)
          Return the number of bytes the encoded byte array will occupy.
static int getByteArrayLength(byte[] data, int start, int len)
          Return the number of bytes the encoded byte array occupies.
static int getMPILength(java.math.BigInteger x)
          Return the number of bytes the encoded MPI will occupy.
static int getMPILength(byte[] data, int start, int len)
          Return the number of bytes the encoded MPI occupies.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getMPILength

public static int getMPILength(byte[] data,
                               int start,
                               int len)
                        throws DataFormatException
Return the number of bytes the encoded MPI occupies.

This is a helper method for decodeMPI(...): it should be called with the same parameters. See decodeMPI for more information on how to use this method.


decodeMPI

public static java.math.BigInteger decodeMPI(byte[] data,
                                             int start,
                                             int len)
                                      throws DataFormatException
Decode an MPI (Multi Precision Integer, or BigInteger in Java).

Example usage:

     BigInteger x = decodeMPI(data, start, len);
     int delta = getMPILength(data, start, len);
     start += delta; len -= delta;
 

If you use this method as outlined above, you don't have to any bounds checking yourself. As soon as something goes wrong, this method will throw a DataFormatException.

Parameters:
data - Byte array which contains the MPI to decode
start - Starting position of the MPI to decode
len - Maximum number of bytes this method is allowed to read. If this method wants more bytes, a DataFormatException will be thrown.
Throws:
DataFormatException - If an improperly formatted MPI is encountered or if not enough bytes are available.

getMPILength

public static int getMPILength(java.math.BigInteger x)
Return the number of bytes the encoded MPI will occupy.

This is a helper method for encodeMPI(...): it should be called with the same parameters to determine how many bytes to reserve. See encodeMPI for an example on how to use this method.


encodeMPI

public static int encodeMPI(java.math.BigInteger x,
                            byte[] data,
                            int start)
Encode an MPI (Multi Precision Integer, or BigInteger in Java).

The MPI will be encoded in PGP compatible format. First two bytes that encode the number of bits (not the number of bytes), followed by the binary representation of the MPI.

Note: only positive MPI's are supported. No sign information will be stored. The maximum size of the MPI is 65535 bits.

Example: encoding two MPI's: bigA and bigB:

     int len = getMPILength(bigA) + getMPILength(bigB);
     byte[] data = new byte[len];
     int start = 0;
     start += encodeMPI(bigA, data, start);
     start += encodeMPI(bigB, data, start);
 

Parameters:
x - The MPI to be encoded
data - Byte array into which the MPI has to be encoded
start - Starting position in data where to start encoding

decodeDateTime

public static java.util.Date decodeDateTime(byte[] data,
                                            int start,
                                            int len)
                                     throws DataFormatException
Decode a date/time

This method always decodes 4 bytes.

Parameters:
data - The byte array which contains the date to decode
start - The position of the date
len - The maximum length this date is allowed to occupy
Throws:
DataFormatException - If len

encodeDateTime

public static void encodeDateTime(java.util.Date x,
                                  byte[] data,
                                  int start)
Encode a date/time value

The date/time value will be encoded as an unsigned int, containing the number of seconds since January 1, 1970, 00:00:00 GMT. Because it is unsigned, this should be sufficient to encode dates until the year 2106.

The caller should make sure that from the starting position start at least four bytes are available in the byte array data.

Parameters:
x - The date to encode
data - The byte array into where the date should be encoded
start - The starting position inside data where the encoding should start.

getByteArrayLength

public static int getByteArrayLength(byte[] data,
                                     int start,
                                     int len)
                              throws DataFormatException
Return the number of bytes the encoded byte array occupies.

This is a helper method for decodeByteArray(...): it should be called with the same parameters. See decodeByteArray for more information on how to use this method.


decodeByteArray

public static byte[] decodeByteArray(byte[] data,
                                     int start,
                                     int len)
                              throws DataFormatException
Decode a Byte Array

Example usage:

     byte[] x = decodeByteArray(data, start, len);
     int delta = getByteArrayLength(data, start, len);
     start += delta; len -= delta;
 

If you use this method as outlined above, you don't have to any bounds checking yourself. As soon as something goes wrong, this method will throw a DataFormatException.

Parameters:
data - Byte array which contains the byte array to decode
start - Starting position of the byte array to decode
len - Maximum number of bytes this method is allowed to read. If this method wants more bytes, a DataFormatException will be thrown.
Throws:
DataFormatException - If an improperly formatted MPI is encountered or if not enough bytes are available.

getByteArrayLength

public static int getByteArrayLength(byte[] x)
Return the number of bytes the encoded byte array will occupy.

This is a helper method for encodeByteArray(...): it should be called with the same parameters to determine how many bytes to reserve. See encodeByteArray for an example on how to use this method.


encodeByteArray

public static int encodeByteArray(byte[] bytes,
                                  byte[] data,
                                  int start)
Encode a byte array

The encoding is pretty simple: the byte array is prefixed with a 4 byte little endian length.

Example: encoding two byte array's: arrA and arrB:

     int len = getByteArrayLength(arrA) + getByteArrayLength(arrB);
     byte[] data = new byte[len];
     int start = 0;
     start += encodeByteArray(arrA, data, start);
     start += encodeByteArray(arrB, data, start);
 

Parameters:
x - The MPI to be encoded
data - Byte array into which the MPI has to be encoded
start - Starting position in data where to start encoding

decodeByte

public static byte decodeByte(byte[] data,
                              int start,
                              int len)
                       throws DataFormatException
Decode a signed byte.
Parameters:
data - The byte array which contains the byte to decode
start - The position of the byte
len - The maximum length this byte is allowed to occupy
Throws:
DataFormatException - If len

decodeUnsignedByte

public static short decodeUnsignedByte(byte[] data,
                                       int start,
                                       int len)
                                throws DataFormatException
Decode an unsigned byte.
Parameters:
data - The byte array which contains the byte to decode
start - The position of the byte
len - The maximum length this byte is allowed to occupy
Throws:
DataFormatException - If len

decodeShort

public static short decodeShort(byte[] data,
                                int start,
                                int len)
                         throws DataFormatException
Decode a signed short.

This method will read 2 bytes.

Parameters:
data - The byte array which contains the short to decode
start - The starting position of the short
len - The maximum length this short is allowed to occupy
Throws:
DataFormatException - If len

decodeUnsignedShort

public static int decodeUnsignedShort(byte[] data,
                                      int start,
                                      int len)
                               throws DataFormatException
Decode an unsigned short.

This method will read 2 bytes.

Parameters:
data - The byte array which contains the short to decode
start - The starting position of the short
len - The maximum length this short is allowed to occupy
Throws:
DataFormatException - If len

decodeInt

public static int decodeInt(byte[] data,
                            int start,
                            int len)
                     throws DataFormatException
Decode a signed int.

This method will read 4 bytes.

Parameters:
data - The byte array which contains the int to decode
start - The starting position of the int
len - The maximum length this int is allowed to occupy
Throws:
DataFormatException - If len

decodeUnsignedInt

public static long decodeUnsignedInt(byte[] data,
                                     int start,
                                     int len)
                              throws DataFormatException
Decode an unsigned int.

This method will read 4 bytes.

Parameters:
data - The byte array which contains the int to decode
start - The starting position of the int
len - The maximum length this int is allowed to occupy
Throws:
DataFormatException - If len

decodeLong

public static long decodeLong(byte[] data,
                              int start,
                              int len)
                       throws DataFormatException
Decode a signed long.

This method will read 8 bytes.

Parameters:
data - The byte array which contains the long to decode
start - The starting position of the long
len - The maximum length this long is allowed to occupy
Throws:
DataFormatException - If len

encodeByte

public static void encodeByte(byte x,
                              byte[] data,
                              int start)
Encode a byte

The caller should make sure that from the starting position start at least one byte is available in the byte array data.

Parameters:
x - The byte to encode
data - The byte array into where the byte should be encoded
start - The starting position inside data where the encoding should start.

encodeShort

public static void encodeShort(short x,
                               byte[] data,
                               int start)
Encode a short

The caller should make sure that from the starting position start at least two bytes are available in the byte array data.

Parameters:
x - The short to encode
data - The byte array into where the short should be encoded
start - The starting position inside data where the encoding should start.

encodeInt

public static void encodeInt(int x,
                             byte[] data,
                             int start)
Encode an int

The caller should make sure that from the starting position start at least four bytes are available in the byte array data.

Parameters:
x - The int to encode
data - The byte array into where the int should be encoded
start - The starting position inside data where the encoding should start.

encodeLong

public static void encodeLong(long x,
                              byte[] data,
                              int start)
Encode a long

The caller should make sure that from the starting position start at least eight bytes are available in the byte array data.

Parameters:
x - The long to encode
data - The byte array into where the long should be encoded
start - The starting position inside data where the encoding should start.