Class ByteUtil
- java.lang.Object
-
- com.healthmarketscience.jackcess.impl.ByteUtil
-
public final class ByteUtil extends Object
Byte manipulation and display utilities- Author:
- Tim McCune
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classByteUtil.ByteStreamUtility byte stream similar to ByteArrayOutputStream but with extended accessibility to the bytes.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intasUnsignedByte(byte b)static intasUnsignedShort(short s)static voidclearRange(ByteBuffer buffer, int start, int end)Sets all bits in the given byte range to 0.static voidclearRemaining(ByteBuffer buffer)Sets all bits in the given remaining byte range to 0.static voidcloseQuietly(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 voidcopy(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 voidfillRange(ByteBuffer buffer, int start, int end)Sets all bits in the given byte range to 1.static intfindRange(ByteBuffer buffer, int start, byte[] pattern)Searches for a pattern of bytes in the given buffer starting at the given offset.static intforward(ByteBuffer buffer, int count)Moves the position of the given buffer the given count from the current position.static intget3ByteInt(ByteBuffer buffer)Read a 3 byte int from a bufferstatic intget3ByteInt(ByteBuffer buffer, int offset)Read a 3 byte int from a bufferstatic intget3ByteInt(ByteBuffer buffer, int offset, ByteOrder order)Read a 3 byte int from a bufferstatic intget3ByteInt(ByteBuffer buffer, ByteOrder order)Read a 3 byte int from a bufferstatic byte[]getBytes(ByteBuffer buffer, int len)Reads an array of bytes from the given bufferstatic byte[]getBytes(ByteBuffer buffer, int offset, int len)Reads an array of bytes from the given buffer at the given offsetstatic intgetInt(ByteBuffer buffer, int offset, ByteOrder order)static intgetInt(ByteBuffer buffer, ByteOrder order)static intgetUnsignedByte(ByteBuffer buffer)Read an unsigned byte from a bufferstatic intgetUnsignedByte(ByteBuffer buffer, int offset)Read an unsigned byte from a bufferstatic intgetUnsignedShort(ByteBuffer buffer)Read an unsigned short from a bufferstatic intgetUnsignedShort(ByteBuffer buffer, int offset)Read an unsigned short from a bufferstatic intgetUnsignedVarInt(ByteBuffer buffer, int numBytes)Read an unsigned variable length int from a bufferstatic intgetUnsignedVarInt(ByteBuffer buffer, int offset, int numBytes)Read an unsigned variable length int from a bufferstatic voidinsertEmptyData(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 booleanmatchesRange(ByteBuffer buffer, int start, byte[] pattern)Matches a pattern of bytes against the given buffer starting at the given offset.static voidput3ByteInt(ByteBuffer buffer, int val)Put an integer into the given buffer at the given offset as a 3-byte integer.static voidput3ByteInt(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 voidput3ByteInt(ByteBuffer buffer, int val, ByteOrder order)Put an integer into the given buffer at the given offset as a 3-byte integer.static voidputInt(ByteBuffer buffer, int val, int offset, ByteOrder order)Writes an int at the given position in the given buffer, using the given ByteOrderstatic voidputInt(ByteBuffer buffer, int val, ByteOrder order)Writes an int at the current position in the given buffer, using the given ByteOrderstatic voidputRange(ByteBuffer buffer, int start, int end, byte b)Sets all bytes in the given byte range to the given byte value.static voidskipFully(DataInputStream din, int len)Skips the given number of bytes in the given streamstatic voidswap2Bytes(byte[] bytes, int offset)Swaps the 2 bytes (changes endianness) of the bytes at the given offset.static voidswap4Bytes(byte[] bytes, int offset)Swaps the 4 bytes (changes endianness) of the bytes at the given offset.static voidswap8Bytes(byte[] bytes, int offset)Swaps the 8 bytes (changes endianness) of the bytes at the given offset.static voidtoHexFile(String fileName, ByteBuffer buffer, int offset, int size)Writes a chunk of data to a file in pretty printed hexidecimal.static StringtoHexString(byte[] array)Convert a byte array to a hexadecimal string for displaystatic StringtoHexString(DatabaseImpl db, int pageNumber, int size)Convert the given number of bytes from the given database page to a hexidecimal string for display.static StringtoHexString(ByteBuffer buffer, int size)Convert a byte buffer to a hexadecimal string for displaystatic StringtoHexString(ByteBuffer buffer, int offset, int size)Convert a byte buffer to a hexadecimal string for displaystatic StringtoHexString(ByteBuffer buffer, int offset, int size, boolean formatted)Convert a byte buffer to a hexadecimal string for displaystatic voidwriteHexString(ByteBuffer buffer, String hexStr)Writes a sequence of hexidecimal values into the given buffer, where every two characters represent one byte value.
-
-
-
Method Detail
-
put3ByteInt
public static void put3ByteInt(ByteBuffer buffer, int val)
Put an integer into the given buffer at the given offset as a 3-byte integer.- Parameters:
buffer- buffer into which to insert the intval- Int to convert
-
put3ByteInt
public 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.- Parameters:
buffer- buffer into which to insert the intval- Int to convertorder- the order to insert the bytes of the int
-
put3ByteInt
public 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.- Parameters:
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 int
-
get3ByteInt
public static int get3ByteInt(ByteBuffer buffer)
Read a 3 byte int from a buffer- Parameters:
buffer- Buffer containing the bytes- Returns:
- The int
-
get3ByteInt
public static int get3ByteInt(ByteBuffer buffer, ByteOrder order)
Read a 3 byte int from a buffer- Parameters:
buffer- Buffer containing the bytesorder- the order of the bytes of the int- Returns:
- The int
-
get3ByteInt
public static int get3ByteInt(ByteBuffer buffer, int offset)
Read a 3 byte int from a buffer- Parameters:
buffer- Buffer containing the bytesoffset- Offset at which to start reading the int- Returns:
- The int
-
get3ByteInt
public static int get3ByteInt(ByteBuffer buffer, int offset, ByteOrder order)
Read a 3 byte int from a buffer- Parameters:
buffer- Buffer containing the bytesoffset- Offset at which to start reading the intorder- the order of the bytes of the int- Returns:
- The int
-
getUnsignedByte
public static int getUnsignedByte(ByteBuffer buffer)
Read an unsigned byte from a buffer- Parameters:
buffer- Buffer containing the bytes- Returns:
- The unsigned byte as an int
-
getUnsignedByte
public static int getUnsignedByte(ByteBuffer buffer, int offset)
Read an unsigned byte from a buffer- Parameters:
buffer- Buffer containing the bytesoffset- Offset at which to read the byte- Returns:
- The unsigned byte as an int
-
getUnsignedShort
public static int getUnsignedShort(ByteBuffer buffer)
Read an unsigned short from a buffer- Parameters:
buffer- Buffer containing the short- Returns:
- The unsigned short as an int
-
getUnsignedShort
public static int getUnsignedShort(ByteBuffer buffer, int offset)
Read an unsigned short from a buffer- Parameters:
buffer- Buffer containing the shortoffset- Offset at which to read the short- Returns:
- The unsigned short as an int
-
getInt
public static int getInt(ByteBuffer buffer, ByteOrder order)
- Parameters:
buffer- Buffer containing the bytesorder- the order of the bytes of the int- Returns:
- an int from the current position in the given buffer, read using the given ByteOrder
-
getInt
public static int getInt(ByteBuffer buffer, int offset, ByteOrder order)
- Parameters:
buffer- Buffer containing the bytesoffset- Offset at which to start reading the intorder- the order of the bytes of the int- Returns:
- an int from the given position in the given buffer, read using the given ByteOrder
-
putInt
public static void putInt(ByteBuffer buffer, int val, ByteOrder order)
Writes an int at the current position in the given buffer, using the given ByteOrder- Parameters:
buffer- buffer into which to insert the intval- Int to insertorder- the order to insert the bytes of the int
-
putInt
public 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- Parameters:
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 int
-
getUnsignedVarInt
public static int getUnsignedVarInt(ByteBuffer buffer, int numBytes)
Read an unsigned variable length int from a buffer- Parameters:
buffer- Buffer containing the variable length int- Returns:
- The unsigned int
-
getUnsignedVarInt
public static int getUnsignedVarInt(ByteBuffer buffer, int offset, int numBytes)
Read an unsigned variable length int from a buffer- Parameters:
buffer- Buffer containing the variable length intoffset- Offset at which to read the value- Returns:
- The unsigned int
-
getBytes
public static byte[] getBytes(ByteBuffer buffer, int len)
Reads an array of bytes from the given buffer- Parameters:
buffer- Buffer containing the desired byteslen- length of the desired bytes- Returns:
- a new buffer with the given number of bytes from the current position in the given buffer
-
getBytes
public static byte[] getBytes(ByteBuffer buffer, int offset, int len)
Reads an array of bytes from the given buffer at the given offset- Parameters:
buffer- Buffer containing the desired bytesoffset- Offset at which to read the byteslen- length of the desired bytes- Returns:
- a new buffer with the given number of bytes from the given position in the given buffer
-
concat
public static byte[] concat(byte[] b1, byte[] b2)Concatenates and returns the given byte arrays.
-
clearRemaining
public static void clearRemaining(ByteBuffer buffer)
Sets all bits in the given remaining byte range to 0.
-
clearRange
public static void clearRange(ByteBuffer buffer, int start, int end)
Sets all bits in the given byte range to 0.
-
fillRange
public static void fillRange(ByteBuffer buffer, int start, int end)
Sets all bits in the given byte range to 1.
-
putRange
public static void putRange(ByteBuffer buffer, int start, int end, byte b)
Sets all bytes in the given byte range to the given byte value.
-
matchesRange
public static boolean matchesRange(ByteBuffer buffer, int start, byte[] pattern)
Matches a pattern of bytes against the given buffer starting at the given offset.
-
findRange
public static int findRange(ByteBuffer buffer, int start, byte[] pattern)
Searches for a pattern of bytes in the given buffer starting at the given offset.- Returns:
- the offset of the pattern if a match is found, -1 otherwise
-
insertEmptyData
public 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). The limit of the buffer is adjusted by the given length. The buffer is expecting to have the required capacity available.
-
toHexString
public static String toHexString(ByteBuffer buffer, int size)
Convert a byte buffer to a hexadecimal string for display- Parameters:
buffer- Buffer to display, starting at offset 0size- Number of bytes to read from the buffer- Returns:
- The display String
-
toHexString
public static String toHexString(byte[] array)
Convert a byte array to a hexadecimal string for display- Parameters:
array- byte array to display, starting at offset 0- Returns:
- The display String
-
toHexString
public static String toHexString(ByteBuffer buffer, int offset, int size)
Convert a byte buffer to a hexadecimal string for display- Parameters:
buffer- Buffer to display, starting at offset 0offset- Offset at which to start reading the buffersize- Number of bytes to read from the buffer- Returns:
- The display String
-
toHexString
public static String toHexString(ByteBuffer buffer, int offset, int size, boolean formatted)
Convert a byte buffer to a hexadecimal string for display- Parameters:
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 required- Returns:
- The display String
-
toHexString
public static String toHexString(DatabaseImpl db, int pageNumber, int size) throws IOException
Convert the given number of bytes from the given database page to a hexidecimal string for display.- Throws:
IOException
-
writeHexString
public static void writeHexString(ByteBuffer buffer, String hexStr) throws IOException
Writes a sequence of hexidecimal values into the given buffer, where every two characters represent one byte value.- Throws:
IOException
-
toHexFile
public static void toHexFile(String fileName, ByteBuffer buffer, int offset, int size) throws IOException
Writes a chunk of data to a file in pretty printed hexidecimal.- Throws:
IOException
-
asUnsignedByte
public static int asUnsignedByte(byte b)
- Returns:
- the byte value converted to an unsigned int value
-
asUnsignedShort
public static int asUnsignedShort(short s)
- Returns:
- the short value converted to an unsigned int value
-
swap8Bytes
public static void swap8Bytes(byte[] bytes, int offset)Swaps the 8 bytes (changes endianness) of the bytes at the given offset.- Parameters:
bytes- buffer containing bytes to swapoffset- offset of the first byte of the bytes to swap
-
swap4Bytes
public static void swap4Bytes(byte[] bytes, int offset)Swaps the 4 bytes (changes endianness) of the bytes at the given offset.- Parameters:
bytes- buffer containing bytes to swapoffset- offset of the first byte of the bytes to swap
-
swap2Bytes
public static void swap2Bytes(byte[] bytes, int offset)Swaps the 2 bytes (changes endianness) of the bytes at the given offset.- Parameters:
bytes- buffer containing bytes to swapoffset- offset of the first byte of the bytes to swap
-
forward
public static int forward(ByteBuffer buffer, int count)
Moves the position of the given buffer the given count from the current position.- Returns:
- the new buffer position
-
copyOf
public static byte[] copyOf(byte[] arr, int newLength)Returns a copy of the given array of the given length.
-
copyOf
public 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.
-
copyOf
public 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.
-
copy
public static void copy(InputStream in, OutputStream out) throws IOException
Copies the given InputStream to the given OutputStream.- Throws:
IOException
-
closeQuietly
public static void closeQuietly(Closeable c)
Closes the given Closeable if non-null, swallows any IOExceptions.
-
skipFully
public static void skipFully(DataInputStream din, int len) throws IOException
Skips the given number of bytes in the given stream- Throws:
IOException
-
-