|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.lwes.util.NumberCodec
public final class NumberCodec
This is a class to efficiently encode built-in primitive types into byte arrays and decode them back. While this can be done with a combination of ByteArrayOutputStreams, DataOutputStreams, ByteArrayInputStreams, DataInputStreams, merely creating those objects is quite costly and it is difficult to make them persistent. As such, this contains code lifted from the guts of the Data*Stream classes. Also, this class defines functions to convert primitive types and byte arrays to and from hexadecimal strings. Hopefully, support for these operations will be added to the standard Java API and this class can be retired.
| Field Summary | |
|---|---|
static int |
BYTE_BITS
|
static int |
BYTE_BYTES
|
static short |
BYTE_MASK
|
static int |
INT_BITS
|
static int |
INT_BYTES
|
static long |
INT_MASK
|
static int |
LONG_BITS
|
static int |
LONG_BYTES
|
static int |
SHORT_BITS
|
static int |
SHORT_BYTES
|
static int |
SHORT_MASK
|
| Method Summary | |
|---|---|
static java.lang.String |
byteArrayToHexString(byte[] bytes)
Return a String encoding the bytes in a byte array in hex form. |
static java.lang.String |
byteArrayToHexString(byte[] bytes,
int offset,
int length)
Return a String encoding the bytes from a portion of a byte array in hex form. |
static byte |
byteFromHexString(java.lang.String s)
Output a byte in unsigned hexadecimal form, padding with zeroes. |
static void |
checkRange(int minLength,
byte[] buffer,
int offset,
int length)
Verifies that the buffer exists, that the writeable region fits into the buffer, and that the writeable length is long enough. |
static byte |
decodeByte(byte[] buffer,
int offset,
int length)
Decode a byte out of a byte-array buffer. |
static byte |
decodeByteUnchecked(byte[] buffer,
int offset)
Extract and decode a byte out of a byte-array buffer. |
static int |
decodeInt(byte[] pBytes)
Decode a int out of a byte-array buffer. |
static int |
decodeInt(byte[] buffer,
int offset,
int length)
Decode an int out of a byte-array buffer. |
static int |
decodeIntUnchecked(byte[] buffer,
int offset)
Extract and decode an int out of a byte-array buffer. |
static long |
decodeLong(byte[] pBytes)
Decode a long out of a byte-array buffer. |
static long |
decodeLong(byte[] buffer,
int offset,
int length)
Decode a long out of a byte-array buffer. |
static long |
decodeLongUnchecked(byte[] buffer,
int offset)
Extract and decode a long out of a byte-array buffer. |
static short |
decodeShort(byte[] buffer,
int offset,
int length)
Decode a short out of a byte-array buffer. |
static short |
decodeShortUnchecked(byte[] buffer,
int offset)
Extract and decode a short out of a byte-array buffer. |
static void |
encodeByte(byte b,
byte[] buffer,
int offset,
int length)
Encode a byte into a byte-array buffer. |
static void |
encodeByteUnchecked(byte b,
byte[] buffer,
int offset)
Encode a byte into a byte-array buffer. |
static byte[] |
encodeInt(int pInt)
Encode a int into a byte-array buffer. |
static void |
encodeInt(int i,
byte[] buffer,
int offset,
int length)
Encode an int into a byte-array buffer. |
static void |
encodeIntUnchecked(int i,
byte[] buffer,
int offset)
Encode an int into a byte-array buffer. |
static byte[] |
encodeLong(long pLong)
Encode a long into a byte-array buffer. |
static void |
encodeLong(long l,
byte[] buffer,
int offset,
int length)
Encode a long into a byte-array buffer. |
static void |
encodeLongUnchecked(long l,
byte[] buffer,
int offset)
Encode a long into a byte-array buffer. |
static void |
encodeShort(short s,
byte[] buffer,
int offset,
int length)
Encode a short into a byte-array buffer. |
static void |
encodeShortUnchecked(short s,
byte[] buffer,
int offset)
Encode a short into a byte-array buffer. |
static byte[] |
hexStringToByteArray(java.lang.String aString)
|
static int |
intFromHexString(java.lang.String s)
Output an int in unsigned hexadecimal form, padding with zeroes. |
static long |
longFromHexString(java.lang.String s)
Output a long in unsigned hexadecimal form, padding with zeroes. |
static short |
shortFromHexString(java.lang.String s)
Output a short in unsigned hexadecimal form, padding with zeroes. |
static java.lang.String |
toHexString(java.math.BigInteger bi)
Output a BigInteger in unsigned hexadecimal form, padding with zeroes. |
static java.lang.String |
toHexString(byte b)
Output a byte in unsigned hexadecimal form, padding with zeroes. |
static java.lang.String |
toHexString(int i)
Output an int in unsigned hexadecimal form, padding with zeroes. |
static java.lang.String |
toHexString(long l)
Output a long in unsigned hexadecimal form, padding with zeroes. |
static java.lang.String |
toHexString(short s)
Output a short in unsigned hexadecimal form, padding with zeroes. |
static void |
writeHexString(byte b,
byte[] bytes,
int offset)
Write a byte in unsigned hexadecimal form, padding with zeroes. |
static void |
writeHexString(byte b,
java.lang.StringBuffer buf)
Write a byte in unsigned hexadecimal form, padding with zeroes. |
static void |
writeHexString(int value,
byte[] bytes,
int offset)
Write a int in unsigned hexadecimal form, padding with zeroes. |
static void |
writeHexString(int i,
java.lang.StringBuffer buf)
Write a int in unsigned hexadecimal form, padding with zeroes. |
static void |
writeHexString(long value,
byte[] bytes,
int offset)
Write a long in unsigned hexadecimal form, padding with zeroes. |
static void |
writeHexString(long l,
java.lang.StringBuffer buf)
Write a long in unsigned hexadecimal form, padding with zeroes. |
static void |
writeHexString(short value,
byte[] bytes,
int offset)
Write a short in unsigned hexadecimal form, padding with zeroes. |
static void |
writeHexString(short s,
java.lang.StringBuffer buf)
Write a short in unsigned hexadecimal form, padding with zeroes. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final int BYTE_BYTES
public static final int SHORT_BYTES
public static final int INT_BYTES
public static final int LONG_BYTES
public static final int BYTE_BITS
public static final int SHORT_BITS
public static final int INT_BITS
public static final int LONG_BITS
public static final short BYTE_MASK
public static final int SHORT_MASK
public static final long INT_MASK
| Method Detail |
|---|
public static void encodeByteUnchecked(byte b,
byte[] buffer,
int offset)
b - the byte to be encodedbuffer - the byte array into which the encoding should be writtenoffset - the position in the array to start writing the encoding
public static void encodeByte(byte b,
byte[] buffer,
int offset,
int length)
throws java.lang.IllegalArgumentException
b - the byte to be encodedbuffer - the byte array into which the encoding should be writtenoffset - the position in the array to start writing the encodinglength - the maximum number of bytes that may be written
java.lang.IllegalArgumentException
public static void encodeShortUnchecked(short s,
byte[] buffer,
int offset)
s - the short to be encodedbuffer - the byte array into which the encoding should be writtenoffset - the position in the array to start writing the encoding
public static void encodeShort(short s,
byte[] buffer,
int offset,
int length)
throws java.lang.IllegalArgumentException
s - the short to be encodedbuffer - the byte array into which the encoding should be writtenoffset - the position in the array to start writing the encodinglength - the maximum number of bytes that may be written
java.lang.IllegalArgumentException
public static void encodeIntUnchecked(int i,
byte[] buffer,
int offset)
i - the int to be encodedbuffer - the byte array into which the encoding should be writtenoffset - the position in the array to start writing the encoding
public static void encodeInt(int i,
byte[] buffer,
int offset,
int length)
throws java.lang.IllegalArgumentException
i - the int to be encodedbuffer - the byte array into which the encoding should be writtenoffset - the position in the array to start writing the encodinglength - the maximum number of bytes that may be written
java.lang.IllegalArgumentException
public static void encodeLongUnchecked(long l,
byte[] buffer,
int offset)
l - the long to be encodedbuffer - the byte array into which the encoding should be writtenoffset - the position in the array to start writing the encoding
public static void encodeLong(long l,
byte[] buffer,
int offset,
int length)
throws java.lang.IllegalArgumentException
l - the long to be encodedbuffer - the byte array into which the encoding should be writtenoffset - the position in the array to start writing the encodinglength - the maximum number of bytes that may be written
java.lang.IllegalArgumentException
public static final byte decodeByteUnchecked(byte[] buffer,
int offset)
buffer - the byte array from which the encoded form should be readoffset - the position in the array to start reading the encoded form
public static final byte decodeByte(byte[] buffer,
int offset,
int length)
throws java.lang.IllegalArgumentException
buffer - the byte array from which the encoded form should be readoffset - the position in the array to start reading the encoded formlength - the maximum number of bytes that may be read
java.lang.IllegalArgumentException
public static short decodeShortUnchecked(byte[] buffer,
int offset)
buffer - the byte array from which the encoded form should be readoffset - the position in the array to start reading the encoded form
public static short decodeShort(byte[] buffer,
int offset,
int length)
throws java.lang.IllegalArgumentException
buffer - the byte array from which the encoded form should be readoffset - the position in the array to start reading the encoded formlength - the maximum number of bytes that may be read
java.lang.IllegalArgumentException
public static int decodeIntUnchecked(byte[] buffer,
int offset)
buffer - the byte array from which the encoded form should be readoffset - the position in the array to start reading the encoded form
public static int decodeInt(byte[] buffer,
int offset,
int length)
throws java.lang.IllegalArgumentException
buffer - the byte array from which the encoded form should be readoffset - the position in the array to start reading the encoded formlength - the maximum number of bytes that may be read
java.lang.IllegalArgumentException
public static long decodeLongUnchecked(byte[] buffer,
int offset)
buffer - the byte array from which the encoded form should be readoffset - the position in the array to start reading the encoded form
public static long decodeLong(byte[] buffer,
int offset,
int length)
throws java.lang.IllegalArgumentException
buffer - the byte array from which the encoded form should be readoffset - the position in the array to start reading the encoded formlength - the maximum number of bytes that may be read
java.lang.IllegalArgumentException
public static void checkRange(int minLength,
byte[] buffer,
int offset,
int length)
throws java.lang.IllegalArgumentException
minLength - the length that will be writtenbuffer - the destination arrayoffset - the first position that should be writtenlength - the number of bytes that may be written
java.lang.IllegalArgumentException - if the check failspublic static java.lang.String toHexString(byte b)
b - the byte
public static java.lang.String toHexString(short s)
s - the short
public static java.lang.String toHexString(int i)
i - the int
public static java.lang.String toHexString(long l)
l - the long
public static java.lang.String toHexString(java.math.BigInteger bi)
bi - the BigInteger
public static void writeHexString(byte b,
java.lang.StringBuffer buf)
buf - the StringBuffer into which to writeb - the byte
public static void writeHexString(short s,
java.lang.StringBuffer buf)
buf - the StringBuffer into which to writes - the short
public static void writeHexString(int i,
java.lang.StringBuffer buf)
buf - the StringBuffer into which to writei - the int
public static void writeHexString(long l,
java.lang.StringBuffer buf)
buf - the StringBuffer into which to writel - the long
public static void writeHexString(byte b,
byte[] bytes,
int offset)
b - the bytebytes - the byte array into which to writeoffset - the index in the byte array to start writing
public static void writeHexString(short value,
byte[] bytes,
int offset)
value - the value to writebytes - the byte array into which to writeoffset - the index in the byte array to start writing
public static void writeHexString(int value,
byte[] bytes,
int offset)
value - the value to writebytes - the byte array into which to writeoffset - the index in the byte array to start writing
public static void writeHexString(long value,
byte[] bytes,
int offset)
value - the value to writebytes - the byte array into which to writeoffset - the index in the byte array to start writing
public static java.lang.String byteArrayToHexString(byte[] bytes,
int offset,
int length)
bytes - the byte arrayoffset - the first byte to outputlength - the number of bytes to output
public static byte[] hexStringToByteArray(java.lang.String aString)
public static java.lang.String byteArrayToHexString(byte[] bytes)
byteArrayToHexString(bytes,0,bytes.length);
bytes - the byte array
public static byte byteFromHexString(java.lang.String s)
s - the String representing the byte.
public static short shortFromHexString(java.lang.String s)
s - the String representing the short
public static int intFromHexString(java.lang.String s)
s - the String representing the int.
public static long longFromHexString(java.lang.String s)
s - the String representing the long.
public static long decodeLong(byte[] pBytes)
throws java.lang.NumberFormatException
pBytes - the byte array from which the encoded form should be read
java.lang.NumberFormatExceptionpublic static byte[] encodeLong(long pLong)
pLong - the long to be encoded
public static int decodeInt(byte[] pBytes)
throws java.lang.NumberFormatException
pBytes - the byte array from which the encoded form should be read
java.lang.NumberFormatException
public static byte[] encodeInt(int pInt)
throws java.lang.NumberFormatException
pInt - the int to be encoded
java.lang.NumberFormatException
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||