|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--webfunds.token.algorithm.EncodeDecodeUtil
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 |
public static int getMPILength(byte[] data,
int start,
int len)
throws DataFormatException
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.
public static java.math.BigInteger decodeMPI(byte[] data,
int start,
int len)
throws DataFormatException
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.
data - Byte array which contains the MPI to decodestart - Starting position of the MPI to decodelen - Maximum number of bytes this method is allowed to read. If
this method wants more bytes, a DataFormatException will be
thrown.public static int getMPILength(java.math.BigInteger x)
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.
public static int encodeMPI(java.math.BigInteger x,
byte[] data,
int start)
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);
x - The MPI to be encodeddata - Byte array into which the MPI has to be encodedstart - Starting position in data where to start encoding
public static java.util.Date decodeDateTime(byte[] data,
int start,
int len)
throws DataFormatException
This method always decodes 4 bytes.
data - The byte array which contains the date to decodestart - The position of the datelen - The maximum length this date is allowed to occupy
public static void encodeDateTime(java.util.Date x,
byte[] data,
int start)
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.
x - The date to encodedata - The byte array into where the date should be encodedstart - The starting position inside data where the encoding should
start.
public static int getByteArrayLength(byte[] data,
int start,
int len)
throws DataFormatException
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.
public static byte[] decodeByteArray(byte[] data,
int start,
int len)
throws DataFormatException
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.
data - Byte array which contains the byte array to decodestart - Starting position of the byte array to decodelen - Maximum number of bytes this method is allowed to read. If
this method wants more bytes, a DataFormatException will be
thrown.public static int getByteArrayLength(byte[] x)
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.
public static int encodeByteArray(byte[] bytes,
byte[] data,
int start)
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);
x - The MPI to be encodeddata - Byte array into which the MPI has to be encodedstart - Starting position in data where to start encoding
public static byte decodeByte(byte[] data,
int start,
int len)
throws DataFormatException
data - The byte array which contains the byte to decodestart - The position of the bytelen - The maximum length this byte is allowed to occupy
public static short decodeUnsignedByte(byte[] data,
int start,
int len)
throws DataFormatException
data - The byte array which contains the byte to decodestart - The position of the bytelen - The maximum length this byte is allowed to occupy
public static short decodeShort(byte[] data,
int start,
int len)
throws DataFormatException
This method will read 2 bytes.
data - The byte array which contains the short to decodestart - The starting position of the shortlen - The maximum length this short is allowed to occupy
public static int decodeUnsignedShort(byte[] data,
int start,
int len)
throws DataFormatException
This method will read 2 bytes.
data - The byte array which contains the short to decodestart - The starting position of the shortlen - The maximum length this short is allowed to occupy
public static int decodeInt(byte[] data,
int start,
int len)
throws DataFormatException
This method will read 4 bytes.
data - The byte array which contains the int to decodestart - The starting position of the intlen - The maximum length this int is allowed to occupy
public static long decodeUnsignedInt(byte[] data,
int start,
int len)
throws DataFormatException
This method will read 4 bytes.
data - The byte array which contains the int to decodestart - The starting position of the intlen - The maximum length this int is allowed to occupy
public static long decodeLong(byte[] data,
int start,
int len)
throws DataFormatException
This method will read 8 bytes.
data - The byte array which contains the long to decodestart - The starting position of the longlen - The maximum length this long is allowed to occupy
public static void encodeByte(byte x,
byte[] data,
int start)
The caller should make sure that from the starting position start at least one byte is available in the byte array data.
x - The byte to encodedata - The byte array into where the byte should be encodedstart - The starting position inside data where the encoding should
start.
public static void encodeShort(short x,
byte[] data,
int start)
The caller should make sure that from the starting position start at least two bytes are available in the byte array data.
x - The short to encodedata - The byte array into where the short should be encodedstart - The starting position inside data where the encoding should
start.
public static void encodeInt(int x,
byte[] data,
int start)
The caller should make sure that from the starting position start at least four bytes are available in the byte array data.
x - The int to encodedata - The byte array into where the int should be encodedstart - The starting position inside data where the encoding should
start.
public static void encodeLong(long x,
byte[] data,
int start)
The caller should make sure that from the starting position start at least eight bytes are available in the byte array data.
x - The long to encodedata - The byte array into where the long should be encodedstart - The starting position inside data where the encoding should
start.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||