public class ByteUtils
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static char[] |
digits
A nibble->char mapping for printing out bytes.
|
static int |
SIZE_BOOL |
static int |
SIZE_DOUBLE |
static int |
SIZE_INT |
static int |
SIZE_LONG |
static int |
SIZE_SHORT |
Constructor and Description |
---|
ByteUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
bigIntegerToBytes(java.math.BigInteger n,
byte[] data,
int[] offset)
Write the bytes representing
n into the byte array
data , starting at index offset [0] , and
increment offset [0] by the number of bytes written; if
data == null , increment offset [0] by the
number of bytes that would have been written otherwise. |
static void |
boolToBytes(boolean b,
byte[] data,
int[] offset)
Write the bytes representing
b into the byte array
data , starting at index offset [0] , and
increment offset [0] by the number of bytes written; if
data == null , increment offset [0] by the
number of bytes that would have been written otherwise. |
static java.math.BigInteger |
bytesToBigInteger(byte[] data,
int[] offset)
Return the
BigInteger represented by the bytes in
data staring at offset offset[0] . |
static boolean |
bytesToBool(byte[] data,
int[] offset)
Return the
boolean represented by the bytes in
data staring at offset offset[0] . |
static double |
bytesToDouble(byte[] data,
int[] offset)
Return the
double represented by the bytes in
data staring at offset offset[0] . |
static int |
bytesToInt(byte[] data,
int[] offset)
Return the
int represented by the bytes in
data staring at offset offset[0] . |
static void |
bytesToInts(int[] dst,
int dst_offset,
byte[] src,
int src_offset,
int length)
Convert an array of
bytes s into an array of
ints . |
static long |
bytesToLong(byte[] data,
int[] offset)
Return the
long represented by the bytes in
data staring at offset offset[0] . |
static short |
bytesToShort(byte[] data,
int[] offset)
Return the
short represented by the bytes in
data staring at offset offset[0] . |
static java.lang.String |
bytesToString(byte[] data,
int[] offset)
Return the
String represented by the bytes in
data staring at offset offset[0] . |
static int |
byteToUnsignedInt(byte b)
Convert a
byte into an unsigned integer. |
static void |
doubleToBytes(double d,
byte[] data,
int[] offset)
Write the bytes representing
d into the byte array
data , starting at index offset [0] , and
increment offset [0] by the number of bytes written; if
data == null , increment offset [0] by the
number of bytes that would have been written otherwise. |
static boolean |
equals(byte[] b1,
byte[] b2)
Determines whether two arrays of
byte s contain the same
contents. |
static int |
getUnsigned(byte b)
DOCUMENT ME!
|
static void |
intsToBytes(byte[] dst,
int dst_offset,
int[] src,
int src_offset,
int length)
Convert an array of
int s into an array of
bytes . |
static void |
intToBytes(int i,
byte[] data,
int[] offset)
Write the bytes representing
i into the byte array
data , starting at index offset [0] , and
increment offset [0] by the number of bytes written; if
data == null , increment offset [0] by the
number of bytes that would have been written otherwise. |
static void |
longToBytes(long l,
byte[] data,
int[] offset)
Write the bytes representing
l into the byte array
data , starting at index offset [0] , and
increment offset [0] by the number of bytes written; if
data == null , increment offset [0] by the
number of bytes that would have been written otherwise. |
static void |
memclr(byte[] array,
int offset,
int length)
Fill the given array with zeros.
|
static boolean |
memcmp(byte[] a,
int a_offset,
byte[] b,
int b_offset,
int length)
Compare the contents of one array of
bytes to another. |
static int |
memcpy(byte[] dst,
int dst_offset,
byte[] src,
int src_offset,
int length)
Copy contents of one array of
bytes into another. |
static java.lang.String |
print_bytes_exact(byte[] data,
int offset,
int length)
DOCUMENT ME!
|
static java.lang.String |
print_bytes(byte[] data)
DOCUMENT ME!
|
static java.lang.String |
print_bytes(byte[] data,
int offset,
int length)
Produce a
String representation for the specified array of
byte s. |
static int |
round_up(int value,
int multiple)
Round a number up to a given multiple.
|
static void |
shortToBytes(short s,
byte[] data,
int[] offset)
Write the bytes representing
s into the byte array
data , starting at index offset [0] , and
increment offset [0] by the number of bytes written; if
data == null , increment offset [0] by the
number of bytes that would have been written otherwise. |
static void |
stringToBytes(java.lang.String s,
byte[] data,
int[] offset)
Write the bytes representing
s into the byte array
data , starting at index offset [0] , and
increment offset [0] by the number of bytes written; if
data == null , increment offset [0] by the
number of bytes that would have been written otherwise. |
static byte[] |
zero_pad(byte[] original,
int block_size)
Return a new array equal to original except zero-padded to an integral
mulitple of blocks.
|
public static final int SIZE_BOOL
public static final int SIZE_SHORT
public static final int SIZE_INT
public static final int SIZE_LONG
public static final int SIZE_DOUBLE
public static final char[] digits
public static final int bytesToInt(byte[] data, int[] offset)
int
represented by the bytes in
data
staring at offset offset[0]
.data
- the array from which to readoffset
- A single element array whose first element is the index in
data from which to begin reading on function entry, and which on
function exit has been incremented by the number of bytes read.int
decodedpublic static final void intToBytes(int i, byte[] data, int[] offset)
i
into the byte array
data
, starting at index offset [0]
, and
increment offset [0]
by the number of bytes written; if
data == null
, increment offset [0]
by the
number of bytes that would have been written otherwise.i
- the int
to encodedata
- The byte array to store into, or null
.offset
- A single element array whose first element is the index in
data to begin writing at on function entry, and which on
function exit has been incremented by the number of bytes
written.public static final short bytesToShort(byte[] data, int[] offset)
short
represented by the bytes in
data
staring at offset offset[0]
.data
- the array from which to readoffset
- A single element array whose first element is the index in
data from which to begin reading on function entry, and which on
function exit has been incremented by the number of bytes read.short
decodedpublic static final void shortToBytes(short s, byte[] data, int[] offset)
s
into the byte array
data
, starting at index offset [0]
, and
increment offset [0]
by the number of bytes written; if
data == null
, increment offset [0]
by the
number of bytes that would have been written otherwise.s
- the short
to encodedata
- The byte array to store into, or null
.offset
- A single element array whose first element is the index in
data to begin writing at on function entry, and which on
function exit has been incremented by the number of bytes
written.public static final long bytesToLong(byte[] data, int[] offset)
long
represented by the bytes in
data
staring at offset offset[0]
.data
- the array from which to readoffset
- A single element array whose first element is the index in
data from which to begin reading on function entry, and which
on function exit has been incremented by the number of bytes
read.long
decodedpublic static final void longToBytes(long l, byte[] data, int[] offset)
l
into the byte array
data
, starting at index offset [0]
, and
increment offset [0]
by the number of bytes written; if
data == null
, increment offset [0]
by the
number of bytes that would have been written otherwise.l
- the long
to encodedata
- The byte array to store into, or null
.offset
- A single element array whose first element is the index in
data to begin writing at on function entry, and which on
function exit has been incremented by the number of bytes
written.public static final double bytesToDouble(byte[] data, int[] offset)
double
represented by the bytes in
data
staring at offset offset[0]
.data
- the array from which to readoffset
- A single element array whose first element is the index in
data from which to begin reading on function entry, and which
on function exit has been incremented by the number of bytes
read.double
decodedpublic static final void doubleToBytes(double d, byte[] data, int[] offset)
d
into the byte array
data
, starting at index offset [0]
, and
increment offset [0]
by the number of bytes written; if
data == null
, increment offset [0]
by the
number of bytes that would have been written otherwise.d
- the double
to encodedata
- The byte array to store into, or null
.offset
- A single element array whose first element is the index in
data to begin writing at on function entry, and which on
function exit has been incremented by the number of bytes
written.public static final java.lang.String bytesToString(byte[] data, int[] offset)
String
represented by the bytes in
data
staring at offset offset[0]
. This method
relies on the user using the corresponding stringToBytes
method to encode the String
, so that it may properly
retrieve the String
length.data
- the array from which to readoffset
- A single element array whose first element is the index in
data from which to begin reading on function entry, and which on
function exit has been incremented by the number of bytes read.String
decodedpublic static final void stringToBytes(java.lang.String s, byte[] data, int[] offset)
s
into the byte array
data
, starting at index offset [0]
, and
increment offset [0]
by the number of bytes written; if
data == null
, increment offset [0]
by the
number of bytes that would have been written otherwise.s
- the String
to encodedata
- The byte array to store into, or null
.offset
- A single element array whose first element is the index in
data to begin writing at on function entry, and which on
function exit has been incremented by the number of bytes
written.public static final boolean bytesToBool(byte[] data, int[] offset)
boolean
represented by the bytes in
data
staring at offset offset[0]
.data
- the array from which to readoffset
- A single element array whose first element is the index in
data from which to begin reading on function entry, and which
on function exit has been incremented by the number of bytes
read.boolean
decodedpublic static final void boolToBytes(boolean b, byte[] data, int[] offset)
b
into the byte array
data
, starting at index offset [0]
, and
increment offset [0]
by the number of bytes written; if
data == null
, increment offset [0]
by the
number of bytes that would have been written otherwise.b
- the boolean
to encodedata
- The byte array to store into, or null
.offset
- A single element array whose first element is the index in
data to begin writing at on function entry, and which on
function exit has been incremented by the number of bytes
written.public static final java.math.BigInteger bytesToBigInteger(byte[] data, int[] offset)
BigInteger
represented by the bytes in
data
staring at offset offset[0]
.data
- the array from which to readoffset
- A single element array whose first element is the index in
data from which to begin reading on function entry, and which
on function exit has been incremented by the number of bytes
read.BigInteger
decodedpublic static final void bigIntegerToBytes(java.math.BigInteger n, byte[] data, int[] offset)
n
into the byte array
data
, starting at index offset [0]
, and
increment offset [0]
by the number of bytes written; if
data == null
, increment offset [0]
by the
number of bytes that would have been written otherwise.n
- the BigInteger
to encodedata
- The byte array to store into, or null
.offset
- A single element array whose first element is the index in
data to begin writing at on function entry, and which on
function exit has been incremented by the number of bytes
written.public static final void bytesToInts(int[] dst, int dst_offset, byte[] src, int src_offset, int length)
bytes
s into an array of
ints
.dst
- the array to writedst_offset
- the start offset in dst
, times 4. This
measures the offset as if dst
were an array of
byte
s (rather than int
s).src
- the array to readsrc_offset
- the start offset in src
length
- the number of byte
s to copy.public static final void intsToBytes(byte[] dst, int dst_offset, int[] src, int src_offset, int length)
int
s into an array of
bytes
.dst
- the array to writedst_offset
- the start offset in dst
src
- the array to readsrc_offset
- the start offset in src
, times 4. This
measures the offset as if src
were an array of
byte
s (rather than int
s).length
- the number of byte
s to copy.public static final int byteToUnsignedInt(byte b)
byte
into an unsigned integer.b
- the byte
to castint
whose lowest byte contains the bits
of b
.public static int memcpy(byte[] dst, int dst_offset, byte[] src, int src_offset, int length)
bytes
into another. If either
array is null
, simply return the length
parameter directly.dst
- the array to write, or null
dst_offset
- the start offset in dst
src
- the array to read, or null
src_offset
- the start offset in src
length
- the number of byte
s to copy.public static boolean memcmp(byte[] a, int a_offset, byte[] b, int b_offset, int length)
bytes
to another.a
- the first arraya_offset
- the start offset in a
b
- the second arrayb_offset
- the start offset in b
length
- the number of byte
s to compare.public static void memclr(byte[] array, int offset, int length)
array
- the array to clearoffset
- the start offsetlength
- the number of byte
s to clear.public static int round_up(int value, int multiple)
value
- the number to be roundedmultiple
- the number to which to be roundedint
greater than or equal to
value
which divides multiple
exactly.public static byte[] zero_pad(byte[] original, int block_size)
original
- the array of byte
s to be paddedblock_size
- the size of the blocksblock_size
exactly. The
array is either original
itself, or a copy whose
first original.length
bytes are equal to
original
.public static boolean equals(byte[] b1, byte[] b2)
byte
s contain the same
contents.b1
- The first arrayb2
- The second arraytrue
if both arrays are null
, both
empty, or both of the same length with equal contents.public static java.lang.String print_bytes(byte[] data, int offset, int length)
String
representation for the specified array of
byte
s. Print each byte
as two hexadecimal
digits.data
- The array to printoffset
- the start offset in data
length
- the number of byte
s to printpublic static java.lang.String print_bytes_exact(byte[] data, int offset, int length)
data
- DOCUMENT ME!offset
- DOCUMENT ME!length
- DOCUMENT ME!public static java.lang.String print_bytes(byte[] data)
data
- DOCUMENT ME!public static int getUnsigned(byte b)
b
- DOCUMENT ME!