public final class ByteUtil extends Object
Modifier and Type | Class and Description |
---|---|
static class |
ByteUtil.ByteStream
Utility byte stream similar to ByteArrayOutputStream but with extended
accessibility to the bytes.
|
Modifier and Type | Method and Description |
---|---|
static int |
asUnsignedByte(byte b) |
static int |
asUnsignedShort(short s) |
static void |
clearRange(ByteBuffer buffer,
int start,
int end)
Sets all bits in the given byte range to 0.
|
static void |
clearRemaining(ByteBuffer buffer)
Sets all bits in the given remaining byte range to 0.
|
static void |
closeQuietly(Closeable c)
Closes the given Closeable if non-null, swallows any IOExceptions.
|
static byte[] |
concat(byte[] b1,
byte[] b2)
Concatenates and returns the given byte arrays.
|
static void |
copy(InputStream in,
OutputStream out)
Copies the given InputStream to the given OutputStream.
|
static byte[] |
copyOf(byte[] arr,
int newLength)
Returns a copy of the given array of the given length.
|
static byte[] |
copyOf(byte[] arr,
int offset,
int newLength)
Returns a copy of the given array of the given length starting at the
given position.
|
static byte[] |
copyOf(byte[] arr,
int offset,
int newLength,
int dstOffset)
Returns a copy of the given array of the given length starting at the
given position.
|
static void |
fillRange(ByteBuffer buffer,
int start,
int end)
Sets all bits in the given byte range to 1.
|
static int |
findRange(ByteBuffer buffer,
int start,
byte[] pattern)
Searches for a pattern of bytes in the given buffer starting at the
given offset.
|
static int |
forward(ByteBuffer buffer,
int count)
Moves the position of the given buffer the given count from the current
position.
|
static int |
get3ByteInt(ByteBuffer buffer)
Read a 3 byte int from a buffer
|
static int |
get3ByteInt(ByteBuffer buffer,
ByteOrder order)
Read a 3 byte int from a buffer
|
static int |
get3ByteInt(ByteBuffer buffer,
int offset)
Read a 3 byte int from a buffer
|
static int |
get3ByteInt(ByteBuffer buffer,
int offset,
ByteOrder order)
Read a 3 byte int from a buffer
|
static byte[] |
getBytes(ByteBuffer buffer,
int len)
Reads an array of bytes from the given buffer
|
static byte[] |
getBytes(ByteBuffer buffer,
int offset,
int len)
Reads an array of bytes from the given buffer at the given offset
|
static int |
getInt(ByteBuffer buffer,
ByteOrder order) |
static int |
getInt(ByteBuffer buffer,
int offset,
ByteOrder order) |
static int |
getUnsignedByte(ByteBuffer buffer)
Read an unsigned byte from a buffer
|
static int |
getUnsignedByte(ByteBuffer buffer,
int offset)
Read an unsigned byte from a buffer
|
static int |
getUnsignedShort(ByteBuffer buffer)
Read an unsigned short from a buffer
|
static int |
getUnsignedShort(ByteBuffer buffer,
int offset)
Read an unsigned short from a buffer
|
static int |
getUnsignedVarInt(ByteBuffer buffer,
int numBytes)
Read an unsigned variable length int from a buffer
|
static int |
getUnsignedVarInt(ByteBuffer buffer,
int offset,
int numBytes)
Read an unsigned variable length int from a buffer
|
static void |
insertEmptyData(ByteBuffer buffer,
int len)
Inserts empty data of the given length at the current position of the
given buffer (moving existing data forward the given length).
|
static boolean |
matchesRange(ByteBuffer buffer,
int start,
byte[] pattern)
Matches a pattern of bytes against the given buffer starting at the given
offset.
|
static void |
put3ByteInt(ByteBuffer buffer,
int val)
Put an integer into the given buffer at the given offset as a 3-byte
integer.
|
static void |
put3ByteInt(ByteBuffer buffer,
int val,
ByteOrder order)
Put an integer into the given buffer at the given offset as a 3-byte
integer.
|
static void |
put3ByteInt(ByteBuffer buffer,
int val,
int offset,
ByteOrder order)
Put an integer into the given buffer at the given offset as a 3-byte
integer.
|
static void |
putInt(ByteBuffer buffer,
int val,
ByteOrder order)
Writes an int at the current position in the given buffer, using the
given ByteOrder
|
static void |
putInt(ByteBuffer buffer,
int val,
int offset,
ByteOrder order)
Writes an int at the given position in the given buffer, using the
given ByteOrder
|
static void |
putRange(ByteBuffer buffer,
int start,
int end,
byte b)
Sets all bytes in the given byte range to the given byte value.
|
static void |
skipFully(DataInputStream din,
int len)
Skips the given number of bytes in the given stream
|
static void |
swap2Bytes(byte[] bytes,
int offset)
Swaps the 2 bytes (changes endianness) of the bytes at the given offset.
|
static void |
swap4Bytes(byte[] bytes,
int offset)
Swaps the 4 bytes (changes endianness) of the bytes at the given offset.
|
static void |
swap8Bytes(byte[] bytes,
int offset)
Swaps the 8 bytes (changes endianness) of the bytes at the given offset.
|
static void |
toHexFile(String fileName,
ByteBuffer buffer,
int offset,
int size)
Writes a chunk of data to a file in pretty printed hexidecimal.
|
static String |
toHexString(byte[] array)
Convert a byte array to a hexadecimal string for display
|
static String |
toHexString(ByteBuffer buffer,
int size)
Convert a byte buffer to a hexadecimal string for display
|
static String |
toHexString(ByteBuffer buffer,
int offset,
int size)
Convert a byte buffer to a hexadecimal string for display
|
static String |
toHexString(ByteBuffer buffer,
int offset,
int size,
boolean formatted)
Convert a byte buffer to a hexadecimal string for display
|
static String |
toHexString(DatabaseImpl db,
int pageNumber,
int size)
Convert the given number of bytes from the given database page to a
hexidecimal string for display.
|
static void |
writeHexString(ByteBuffer buffer,
String hexStr)
Writes a sequence of hexidecimal values into the given buffer, where
every two characters represent one byte value.
|
public static void put3ByteInt(ByteBuffer buffer, int val)
buffer
- buffer into which to insert the intval
- Int to convertpublic static void put3ByteInt(ByteBuffer buffer, int val, ByteOrder order)
buffer
- buffer into which to insert the intval
- Int to convertorder
- the order to insert the bytes of the intpublic static void put3ByteInt(ByteBuffer buffer, int val, int offset, ByteOrder order)
buffer
- buffer into which to insert the intval
- Int to convertoffset
- offset at which to insert the intorder
- the order to insert the bytes of the intpublic static int get3ByteInt(ByteBuffer buffer)
buffer
- Buffer containing the bytespublic static int get3ByteInt(ByteBuffer buffer, ByteOrder order)
buffer
- Buffer containing the bytesorder
- the order of the bytes of the intpublic static int get3ByteInt(ByteBuffer buffer, int offset)
buffer
- Buffer containing the bytesoffset
- Offset at which to start reading the intpublic static int get3ByteInt(ByteBuffer buffer, int offset, ByteOrder order)
buffer
- Buffer containing the bytesoffset
- Offset at which to start reading the intorder
- the order of the bytes of the intpublic static int getUnsignedByte(ByteBuffer buffer)
buffer
- Buffer containing the bytespublic static int getUnsignedByte(ByteBuffer buffer, int offset)
buffer
- Buffer containing the bytesoffset
- Offset at which to read the bytepublic static int getUnsignedShort(ByteBuffer buffer)
buffer
- Buffer containing the shortpublic static int getUnsignedShort(ByteBuffer buffer, int offset)
buffer
- Buffer containing the shortoffset
- Offset at which to read the shortpublic static int getInt(ByteBuffer buffer, ByteOrder order)
buffer
- Buffer containing the bytesorder
- the order of the bytes of the intpublic static int getInt(ByteBuffer buffer, int offset, ByteOrder order)
buffer
- Buffer containing the bytesoffset
- Offset at which to start reading the intorder
- the order of the bytes of the intpublic static void putInt(ByteBuffer buffer, int val, ByteOrder order)
buffer
- buffer into which to insert the intval
- Int to insertorder
- the order to insert the bytes of the intpublic static void putInt(ByteBuffer buffer, int val, int offset, ByteOrder order)
buffer
- buffer into which to insert the intval
- Int to insertoffset
- offset at which to insert the intorder
- the order to insert the bytes of the intpublic static int getUnsignedVarInt(ByteBuffer buffer, int numBytes)
buffer
- Buffer containing the variable length intpublic static int getUnsignedVarInt(ByteBuffer buffer, int offset, int numBytes)
buffer
- Buffer containing the variable length intoffset
- Offset at which to read the valuepublic static byte[] getBytes(ByteBuffer buffer, int len)
buffer
- Buffer containing the desired byteslen
- length of the desired bytespublic static byte[] getBytes(ByteBuffer buffer, int offset, int len)
buffer
- Buffer containing the desired bytesoffset
- Offset at which to read the byteslen
- length of the desired bytespublic static byte[] concat(byte[] b1, byte[] b2)
public static void clearRemaining(ByteBuffer buffer)
public static void clearRange(ByteBuffer buffer, int start, int end)
public static void fillRange(ByteBuffer buffer, int start, int end)
public static void putRange(ByteBuffer buffer, int start, int end, byte b)
public static boolean matchesRange(ByteBuffer buffer, int start, byte[] pattern)
public static int findRange(ByteBuffer buffer, int start, byte[] pattern)
public static void insertEmptyData(ByteBuffer buffer, int len)
public static String toHexString(ByteBuffer buffer, int size)
buffer
- Buffer to display, starting at offset 0size
- Number of bytes to read from the bufferpublic static String toHexString(byte[] array)
array
- byte array to display, starting at offset 0public static String toHexString(ByteBuffer buffer, int offset, int size)
buffer
- Buffer to display, starting at offset 0offset
- Offset at which to start reading the buffersize
- Number of bytes to read from the bufferpublic static String toHexString(ByteBuffer buffer, int offset, int size, boolean formatted)
buffer
- Buffer to display, starting at offset 0offset
- Offset at which to start reading the buffersize
- Number of bytes to read from the bufferformatted
- flag indicating if formatting is requiredpublic static String toHexString(DatabaseImpl db, int pageNumber, int size) throws IOException
IOException
public static void writeHexString(ByteBuffer buffer, String hexStr) throws IOException
IOException
public static void toHexFile(String fileName, ByteBuffer buffer, int offset, int size) throws IOException
IOException
public static int asUnsignedByte(byte b)
public static int asUnsignedShort(short s)
public static void swap8Bytes(byte[] bytes, int offset)
bytes
- buffer containing bytes to swapoffset
- offset of the first byte of the bytes to swappublic static void swap4Bytes(byte[] bytes, int offset)
bytes
- buffer containing bytes to swapoffset
- offset of the first byte of the bytes to swappublic static void swap2Bytes(byte[] bytes, int offset)
bytes
- buffer containing bytes to swapoffset
- offset of the first byte of the bytes to swappublic static int forward(ByteBuffer buffer, int count)
public static byte[] copyOf(byte[] arr, int newLength)
public static byte[] copyOf(byte[] arr, int offset, int newLength)
public static byte[] copyOf(byte[] arr, int offset, int newLength, int dstOffset)
public static void copy(InputStream in, OutputStream out) throws IOException
IOException
public static void closeQuietly(Closeable c)
public static void skipFully(DataInputStream din, int len) throws IOException
IOException
Copyright © 2005–2024 OpenHMS. All rights reserved.