public class BufferUtils
extends Object
BufferUtils: utilities for converting between binary data and Java primitive types. Faster than Java runtime equivalents
| Modifier and Type | Field | Description |
|---|---|---|
static String[] |
alphas |
|
static String[] |
hexadecimal |
| Constructor | Description |
|---|---|
BufferUtils() |
| Modifier and Type | Method | Description |
|---|---|---|
static int |
byteArrayToInt(byte[] b) |
Convert byte array to int
|
static int |
byteArrayToInt(byte[] b,
int offset) |
Conver byte array to int with offset
|
static int |
byteArrayToInt(byte[] b,
int offset,
int count) |
Convert byte array to int with offset.
|
static int |
byteArrayToInt(byte[] b,
int offset,
int count,
boolean isReverse) |
Convert byte array to int with offset.
|
static long |
byteArrayToLong(byte[] b) |
Convert byte array to long
|
static long |
byteArrayToLong(byte[] b,
int offset) |
Conver byte array to long with offset
|
static long |
byteArrayToLong(byte[] b,
int offset,
int count) |
Convert byte array to long with offset.
|
static long |
byteArrayToLong(byte[] b,
int offset,
int count,
boolean isReverse) |
Convert byte array to long with offset.
|
static int |
byteArrayToShort(byte[] b) |
Convert byte array to int
|
static int |
byteArrayToShort(byte[] b,
int offset) |
Conver byte array to int with offset
|
static int |
byteArrayToShort(byte[] b,
int offset,
int count) |
Convert byte array to int with offset.
|
static int |
byteArrayToShort(byte[] b,
int offset,
int count,
boolean isReverse) |
Convert byte array to int with offset.
|
static String |
byteArrayToString(byte[] b) |
Convert a byte array to a String (UTF-8 encoding assumed)
|
static String |
byteArrayToString(byte[] b,
int offset,
int count) |
Convert a byte array to a String (UTF-8 encoding assumed)
|
static String |
byteArrayToString(byte[] b,
int offset,
int count,
String charsetName) |
Convert a byte array to a String
|
static byte[] |
decodeHexString(String hexStr) |
Decode a string as a byte array
|
static int |
doCRC32(int crc,
byte[] buffer,
int offset,
int len) |
Calculate an IEEE CRC32 value for MPEG transport stream from a starting crc value
|
static String |
encodeHexString(byte[] bytes) |
Encode a byte array as a string
|
static String |
encodeHexString(byte[] bytes,
int offset,
int len) |
Encode a byte array as a string
|
static int |
getUnsignedShort(java.nio.ByteBuffer buffer) |
|
static int |
indexOf(byte[] source,
byte[] pattern) |
Finds the first occurrence of a byte pattern in a byte buffer.
|
static int |
indexOfDifferent(byte[] buffer1,
byte[] buffer2) |
Compare two byte buffers, and return the index of the first byte that is different.
|
static byte[] |
intToByteArray(int value) |
Convert a int value to a byte array in network order
|
static void |
intToByteArray(int value,
byte[] buffer,
int offset,
int size) |
Convert a int value to a byte array in network order
|
static void |
intToByteArray(int value,
byte[] buffer,
int offset,
int size,
boolean isReverse) |
Convert a int value to a byte array in network order
|
static byte[] |
intToByteArray(int value,
int size) |
Convert a int value to a byte array in network order
|
static byte[] |
longToByteArray(long value) |
Convert a long value to a byte array in network order
|
static void |
longToByteArray(long value,
byte[] buffer,
int offset,
int size) |
Convert a long value to a byte array in network order
|
static void |
longToByteArray(long value,
byte[] buffer,
int offset,
int size,
boolean isReverse) |
Convert a long value to a byte array in network order
|
static byte[] |
longToByteArray(long value,
int size) |
Convert a long value to a byte array in network order
|
static boolean |
startsWith(byte[] source,
byte[] pattern) |
public static final String[] alphas
public static final String[] hexadecimal
public static int byteArrayToInt(byte[] b)
b - byte array (4 bytes)public static int byteArrayToInt(byte[] b,
int offset)
b - byte array (4 bytes)offset - offsetpublic static int byteArrayToInt(byte[] b,
int offset,
int count)
b - byte arrayoffset - offsetcount - number of bytespublic static int byteArrayToInt(byte[] b,
int offset,
int count,
boolean isReverse)
b - byte arrayoffset - offsetcount - number of bytesisReverse - is data in reverse orderpublic static long byteArrayToLong(byte[] b)
b - byte array (8 bytes)public static long byteArrayToLong(byte[] b,
int offset)
b - byte array (8 bytes)offset - offsetpublic static long byteArrayToLong(byte[] b,
int offset,
int count)
b - byte arrayoffset - offsetcount - number of bytespublic static long byteArrayToLong(byte[] b,
int offset,
int count,
boolean isReverse)
b - byte arrayoffset - offsetcount - number of bytesisReverse - is data in reverse orderpublic static int byteArrayToShort(byte[] b)
b - byte array (2 bytes)public static int byteArrayToShort(byte[] b,
int offset)
b - byte array (2 bytes)offset - offsetpublic static int byteArrayToShort(byte[] b,
int offset,
int count)
b - byte arrayoffset - offsetcount - number of bytespublic static int byteArrayToShort(byte[] b,
int offset,
int count,
boolean isReverse)
b - byte arrayoffset - offsetcount - number of bytesisReverse - is data in reverse orderpublic static String byteArrayToString(byte[] b)
b - byte arraypublic static String byteArrayToString(byte[] b,
int offset,
int count)
b - byte arrayoffset - offsetcount - lenpublic static String byteArrayToString(byte[] b,
int offset,
int count,
String charsetName)
b - byte arrayoffset - offsetcount - lencharsetName - character set namepublic static byte[] decodeHexString(String hexStr)
hexStr - stringpublic static int doCRC32(int crc,
byte[] buffer,
int offset,
int len)
crc - starting crc valuebuffer - bufferoffset - offsetlen - lenpublic static String encodeHexString(byte[] bytes)
bytes - byte arraypublic static String encodeHexString(byte[] bytes,
int offset,
int len)
bytes - byte arrayoffset - offsetlen - lengthpublic static int getUnsignedShort(java.nio.ByteBuffer buffer)
public static int indexOf(byte[] source,
byte[] pattern)
Note: If we want to improve the performance, we could implement the Knuth-Morris-Pratt algorithm. But I don't need the speed nor do I have time today!
source - The source byte bufferpattern - the pattern we're looking for.public static int indexOfDifferent(byte[] buffer1,
byte[] buffer2)
buffer1 - byte array1buffer2 - byte array2public static byte[] intToByteArray(int value)
value - valuepublic static void intToByteArray(int value,
byte[] buffer,
int offset,
int size)
value - valuebuffer - destination byte arrayoffset - starting offset in byte arraysize - number of bytes to writepublic static void intToByteArray(int value,
byte[] buffer,
int offset,
int size,
boolean isReverse)
value - valuebuffer - destination byte arrayoffset - starting offset in byte arraysize - number of bytes to writeisReverse - is data in reverse orderpublic static byte[] intToByteArray(int value,
int size)
value - valuesize - size of resultant byte arraypublic static byte[] longToByteArray(long value)
value - valuepublic static void longToByteArray(long value,
byte[] buffer,
int offset,
int size)
value - valuebuffer - destination byte arrayoffset - starting offset in byte arraysize - number of bytes to writepublic static void longToByteArray(long value,
byte[] buffer,
int offset,
int size,
boolean isReverse)
value - valuebuffer - destination byte arrayoffset - starting offset in byte arraysize - number of bytes to writeisReverse - is data in reverse orderpublic static byte[] longToByteArray(long value,
int size)
value - valuesize - size of resultant byte arraypublic static boolean startsWith(byte[] source,
byte[] pattern)