APIdia Launch Partner
Top Description Inners Fields Constructors Methods
io.github.spannm.jackcess.impl

public final Class ByteUtil

extends Object
Class Inheritance
Imports
java.io.Closeable, .DataInputStream, .FileOutputStream, .IOException, .InputStream, .OutputStream, .OutputStreamWriter, .PrintWriter, java.nio.ByteBuffer, .ByteOrder, java.nio.charset.Charset, java.util.Arrays

Byte manipulation and display utilities
Author
Tim McCune

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
ByteUtil.ByteStream

Utility byte stream similar to ByteArrayOutputStream but with extended accessibility to the bytes.

Field Summary

Modifier and TypeField and Description
private static final String[]
private static final int
private static final int

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static int

Returns:

the byte value converted to an unsigned int value
asUnsignedByte
(byte b)

Returns the byte value converted to an unsigned int value.

public static int

Returns:

the short value converted to an unsigned int value
asUnsignedShort
(short s)

Returns the short value converted to an unsigned int value.

public static void
clearRange(ByteBuffer buffer, int start, int end)

Sets all bits in the given byte range to 0.

public static void
clearRemaining(ByteBuffer buffer)

Sets all bits in the given remaining byte range to 0.

public static void
closeQuietly(Closeable c)

Closes the given Closeable if non-null swallowing any IOException.

public static byte[]
concat(byte[] b1, byte[] b2)

Concatenates and returns the given byte arrays.

public static void
copy(InputStream in, OutputStream out)

Copies the given InputStream to the given OutputStream.

public static byte[]
copyOf(byte[] arr, int newLength)

Returns a copy of the given array of the given length.

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.

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.

public static void
fillRange(ByteBuffer buffer, int start, int end)

Sets all bits in the given byte range to 1.

public static int

Returns:

the offset of the pattern if a match is found, -1 otherwise
findRange
(ByteBuffer buffer, int start, byte[] pattern)

Searches for a pattern of bytes in the given buffer starting at the given offset.

public static int

Returns:

the new buffer position
forward
(ByteBuffer buffer, int count)

Moves the position of the given buffer the given count from the current position.

public static int

Returns:

The int
get3ByteInt
(ByteBuffer
Buffer containing the bytes
buffer
)

Read a 3 byte int from a buffer

public static int

Returns:

The int
get3ByteInt
(ByteBuffer
Buffer containing the bytes
buffer
,
ByteOrder
the order of the bytes of the int
order
)

Read a 3 byte int from a buffer

public static int

Returns:

The int
get3ByteInt
(ByteBuffer
Buffer containing the bytes
buffer
,
int
Offset at which to start reading the int
offset
)

Read a 3 byte int from a buffer

public static int

Returns:

The int
get3ByteInt
(ByteBuffer
Buffer containing the bytes
buffer
,
int
Offset at which to start reading the int
offset
,
ByteOrder
the order of the bytes of the int
order
)

Read a 3 byte int from a buffer

public static byte[]

Returns:

a new buffer with the given number of bytes from the current position in the given buffer
getBytes
(ByteBuffer
Buffer containing the desired bytes
buffer
,
int
length of the desired bytes
len
)

Reads an array of bytes from the given buffer

public static byte[]

Returns:

a new buffer with the given number of bytes from the given position in the given buffer
getBytes
(ByteBuffer
Buffer containing the desired bytes
buffer
,
int
Offset at which to read the bytes
offset
,
int
length of the desired bytes
len
)

Reads an array of bytes from the given buffer at the given offset

public static int

Returns:

an int from the current position in the given buffer, read using the given ByteOrder
getInt
(ByteBuffer
Buffer containing the bytes
buffer
,
ByteOrder
the order of the bytes of the int
order
)

Returns an int from the current position in the given buffer, read using the given ByteOrder.

public static int

Returns:

an int from the given position in the given buffer, read using the given ByteOrder
getInt
(ByteBuffer
Buffer containing the bytes
buffer
,
int
Offset at which to start reading the int
offset
,
ByteOrder
the order of the bytes of the int
order
)

Returns an int from the given position in the given buffer, read using the given ByteOrder.

public static int

Returns:

The unsigned byte as an int
getUnsignedByte
(ByteBuffer
Buffer containing the bytes
buffer
)

Read an unsigned byte from a buffer

public static int

Returns:

The unsigned byte as an int
getUnsignedByte
(ByteBuffer
Buffer containing the bytes
buffer
,
int
Offset at which to read the byte
offset
)

Read an unsigned byte from a buffer

public static int

Returns:

The unsigned short as an int
getUnsignedShort
(ByteBuffer
Buffer containing the short
buffer
)

Read an unsigned short from a buffer

public static int

Returns:

The unsigned short as an int
getUnsignedShort
(ByteBuffer
Buffer containing the short
buffer
,
int
Offset at which to read the short
offset
)

Read an unsigned short from a buffer

public static int

Returns:

The unsigned int
getUnsignedVarInt
(ByteBuffer
Buffer containing the variable length int
buffer
,
int numBytes)

Read an unsigned variable length int from a buffer

public static int

Returns:

The unsigned int
getUnsignedVarInt
(ByteBuffer
Buffer containing the variable length int
buffer
,
int
Offset at which to read the value
offset
,
int numBytes)

Read an unsigned variable length int from a buffer

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).

public static boolean
matchesRange(ByteBuffer buffer, int start, byte[] pattern)

Matches a pattern of bytes against the given buffer starting at the given offset.

public static void
put3ByteInt(ByteBuffer
buffer into which to insert the int
buffer
,
int
Int to convert
val
)

Put an integer into the given buffer at the given offset as a 3-byte integer.

public static void
put3ByteInt(ByteBuffer
buffer into which to insert the int
buffer
,
int
Int to convert
val
,
ByteOrder
the order to insert the bytes of the int
order
)

Put an integer into the given buffer at the given offset as a 3-byte integer.

public static void
put3ByteInt(ByteBuffer
buffer into which to insert the int
buffer
,
int
Int to convert
val
,
int
offset at which to insert the int
offset
,
ByteOrder
the order to insert the bytes of the int
order
)

Put an integer into the given buffer at the given offset as a 3-byte integer.

public static void
putInt(ByteBuffer
buffer into which to insert the int
buffer
,
int
Int to insert
val
,
ByteOrder
the order to insert the bytes of the int
order
)

Writes an int at the current position in the given buffer, using the given ByteOrder

public static void
putInt(ByteBuffer
buffer into which to insert the int
buffer
,
int
Int to insert
val
,
int
offset at which to insert the int
offset
,
ByteOrder
the order to insert the bytes of the int
order
)

Writes an int at the given position in the given buffer, using the given ByteOrder

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.

public static void
skipFully(DataInputStream din, int len)

Skips the given number of bytes in the given stream

public static void
swap2Bytes(byte[]
buffer containing bytes to swap
bytes
,
int
offset of the first byte of the bytes to swap
offset
)

Swaps the 2 bytes (changes endianness) of the bytes at the given offset.

public static void
swap4Bytes(byte[]
buffer containing bytes to swap
bytes
,
int
offset of the first byte of the bytes to swap
offset
)

Swaps the 4 bytes (changes endianness) of the bytes at the given offset.

public static void
swap8Bytes(byte[]
buffer containing bytes to swap
bytes
,
int
offset of the first byte of the bytes to swap
offset
)

Swaps the 8 bytes (changes endianness) of the bytes at the given offset.

private static void
swapBytesAt(byte[] bytes, int p1, int p2)

Swaps the bytes at the given positions.

public static void
toHexFile(String fileName, ByteBuffer buffer, int offset, int size)

Writes a chunk of data to a file in pretty printed hexidecimal.

public static String

Returns:

The display String
toHexString
(ByteBuffer
Buffer to display, starting at offset 0
buffer
,
int
Number of bytes to read from the buffer
size
)

Convert a byte buffer to a hexadecimal string for display

public static String

Returns:

The display String
toHexString
(byte[]
byte array to display, starting at offset 0
array
)

Convert a byte array to a hexadecimal string for display

public static String

Returns:

The display String
toHexString
(ByteBuffer
Buffer to display, starting at offset 0
buffer
,
int
Offset at which to start reading the buffer
offset
,
int
Number of bytes to read from the buffer
size
)

Convert a byte buffer to a hexadecimal string for display

public static String

Returns:

The display String
toHexString
(ByteBuffer
Buffer to display, starting at offset 0
buffer
,
int
Offset at which to start reading the buffer
offset
,
int
Number of bytes to read from the buffer
size
,
boolean
flag indicating if formatting is required
formatted
)

Convert a byte buffer to a hexadecimal string for display

public 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.

public 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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

HEX_CHARSback to summary
private static final String[] HEX_CHARS
NUM_BYTES_PER_BLOCKback to summary
private static final int NUM_BYTES_PER_BLOCK
NUM_BYTES_PER_LINEback to summary
private static final int NUM_BYTES_PER_LINE

Constructor Detail

ByteUtilback to summary
private ByteUtil()

Method Detail

asUnsignedByteback to summary
public static int asUnsignedByte(byte b)

Returns the byte value converted to an unsigned int value.

Returns:int

the byte value converted to an unsigned int value

asUnsignedShortback to summary
public static int asUnsignedShort(short s)

Returns the short value converted to an unsigned int value.

Returns:int

the short value converted to an unsigned int value

clearRangeback to summary
public static void clearRange(ByteBuffer buffer, int start, int end)

Sets all bits in the given byte range to 0.

clearRemainingback to summary
public static void clearRemaining(ByteBuffer buffer)

Sets all bits in the given remaining byte range to 0.

closeQuietlyback to summary
public static void closeQuietly(Closeable c)

Closes the given Closeable if non-null swallowing any IOException.

Annotations
@SuppressWarnings:PMD.UnnecessaryReturn
concatback to summary
public static byte[] concat(byte[] b1, byte[] b2)

Concatenates and returns the given byte arrays.

copyback to summary
public static void copy(InputStream in, OutputStream out) throws IOException

Copies the given InputStream to the given OutputStream.

copyOfback to summary
public static byte[] copyOf(byte[] arr, int newLength)

Returns a copy of the given array of the given length.

copyOfback to summary
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.

copyOfback to summary
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.

fillRangeback to summary
public static void fillRange(ByteBuffer buffer, int start, int end)

Sets all bits in the given byte range to 1.

findRangeback to summary
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:int

the offset of the pattern if a match is found, -1 otherwise

forwardback to summary
public static int forward(ByteBuffer buffer, int count)

Moves the position of the given buffer the given count from the current position.

Returns:int

the new buffer position

get3ByteIntback to summary
public static int get3ByteInt(ByteBuffer buffer)

Read a 3 byte int from a buffer

Parameters
buffer:ByteBuffer

Buffer containing the bytes

Returns:int

The int

get3ByteIntback to summary
public static int get3ByteInt(ByteBuffer buffer, ByteOrder order)

Read a 3 byte int from a buffer

Parameters
buffer:ByteBuffer

Buffer containing the bytes

order:ByteOrder

the order of the bytes of the int

Returns:int

The int

get3ByteIntback to summary
public static int get3ByteInt(ByteBuffer buffer, int offset)

Read a 3 byte int from a buffer

Parameters
buffer:ByteBuffer

Buffer containing the bytes

offset:int

Offset at which to start reading the int

Returns:int

The int

get3ByteIntback to summary
public static int get3ByteInt(ByteBuffer buffer, int offset, ByteOrder order)

Read a 3 byte int from a buffer

Parameters
buffer:ByteBuffer

Buffer containing the bytes

offset:int

Offset at which to start reading the int

order:ByteOrder

the order of the bytes of the int

Returns:int

The int

getBytesback to summary
public static byte[] getBytes(ByteBuffer buffer, int len)

Reads an array of bytes from the given buffer

Parameters
buffer:ByteBuffer

Buffer containing the desired bytes

len:int

length of the desired bytes

Returns:byte[]

a new buffer with the given number of bytes from the current position in the given buffer

getBytesback to summary
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:ByteBuffer

Buffer containing the desired bytes

offset:int

Offset at which to read the bytes

len:int

length of the desired bytes

Returns:byte[]

a new buffer with the given number of bytes from the given position in the given buffer

getIntback to summary
public static int getInt(ByteBuffer buffer, ByteOrder order)

Returns an int from the current position in the given buffer, read using the given ByteOrder.

Parameters
buffer:ByteBuffer

Buffer containing the bytes

order:ByteOrder

the order of the bytes of the int

Returns:int

an int from the current position in the given buffer, read using the given ByteOrder

getIntback to summary
public static int getInt(ByteBuffer buffer, int offset, ByteOrder order)

Returns an int from the given position in the given buffer, read using the given ByteOrder.

Parameters
buffer:ByteBuffer

Buffer containing the bytes

offset:int

Offset at which to start reading the int

order:ByteOrder

the order of the bytes of the int

Returns:int

an int from the given position in the given buffer, read using the given ByteOrder

getUnsignedByteback to summary
public static int getUnsignedByte(ByteBuffer buffer)

Read an unsigned byte from a buffer

Parameters
buffer:ByteBuffer

Buffer containing the bytes

Returns:int

The unsigned byte as an int

getUnsignedByteback to summary
public static int getUnsignedByte(ByteBuffer buffer, int offset)

Read an unsigned byte from a buffer

Parameters
buffer:ByteBuffer

Buffer containing the bytes

offset:int

Offset at which to read the byte

Returns:int

The unsigned byte as an int

getUnsignedShortback to summary
public static int getUnsignedShort(ByteBuffer buffer)

Read an unsigned short from a buffer

Parameters
buffer:ByteBuffer

Buffer containing the short

Returns:int

The unsigned short as an int

getUnsignedShortback to summary
public static int getUnsignedShort(ByteBuffer buffer, int offset)

Read an unsigned short from a buffer

Parameters
buffer:ByteBuffer

Buffer containing the short

offset:int

Offset at which to read the short

Returns:int

The unsigned short as an int

getUnsignedVarIntback to summary
public static int getUnsignedVarInt(ByteBuffer buffer, int numBytes)

Read an unsigned variable length int from a buffer

Parameters
buffer:ByteBuffer

Buffer containing the variable length int

Returns:int

The unsigned int

getUnsignedVarIntback to summary
public static int getUnsignedVarInt(ByteBuffer buffer, int offset, int numBytes)

Read an unsigned variable length int from a buffer

Parameters
buffer:ByteBuffer

Buffer containing the variable length int

offset:int

Offset at which to read the value

Returns:int

The unsigned int

insertEmptyDataback to summary
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.

matchesRangeback to summary
public static boolean matchesRange(ByteBuffer buffer, int start, byte[] pattern)

Matches a pattern of bytes against the given buffer starting at the given offset.

put3ByteIntback to summary
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:ByteBuffer

buffer into which to insert the int

val:int

Int to convert

put3ByteIntback to summary
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:ByteBuffer

buffer into which to insert the int

val:int

Int to convert

order:ByteOrder

the order to insert the bytes of the int

put3ByteIntback to summary
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:ByteBuffer

buffer into which to insert the int

val:int

Int to convert

offset:int

offset at which to insert the int

order:ByteOrder

the order to insert the bytes of the int

putIntback to summary
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:ByteBuffer

buffer into which to insert the int

val:int

Int to insert

order:ByteOrder

the order to insert the bytes of the int

putIntback to summary
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:ByteBuffer

buffer into which to insert the int

val:int

Int to insert

offset:int

offset at which to insert the int

order:ByteOrder

the order to insert the bytes of the int

putRangeback to summary
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.

skipFullyback to summary
public static void skipFully(DataInputStream din, int len) throws IOException

Skips the given number of bytes in the given stream

swap2Bytesback to summary
public static void swap2Bytes(byte[] bytes, int offset)

Swaps the 2 bytes (changes endianness) of the bytes at the given offset.

Parameters
bytes:byte[]

buffer containing bytes to swap

offset:int

offset of the first byte of the bytes to swap

swap4Bytesback to summary
public static void swap4Bytes(byte[] bytes, int offset)

Swaps the 4 bytes (changes endianness) of the bytes at the given offset.

Parameters
bytes:byte[]

buffer containing bytes to swap

offset:int

offset of the first byte of the bytes to swap

swap8Bytesback to summary
public static void swap8Bytes(byte[] bytes, int offset)

Swaps the 8 bytes (changes endianness) of the bytes at the given offset.

Parameters
bytes:byte[]

buffer containing bytes to swap

offset:int

offset of the first byte of the bytes to swap

swapBytesAtback to summary
private static void swapBytesAt(byte[] bytes, int p1, int p2)

Swaps the bytes at the given positions.

toHexFileback to summary
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.

Annotations
@SuppressWarnings:PMD.LinguisticNaming
toHexStringback to summary
public static String toHexString(ByteBuffer buffer, int size)

Convert a byte buffer to a hexadecimal string for display

Parameters
buffer:ByteBuffer

Buffer to display, starting at offset 0

size:int

Number of bytes to read from the buffer

Returns:String

The display String

toHexStringback to summary
public static String toHexString(byte[] array)

Convert a byte array to a hexadecimal string for display

Parameters
array:byte[]

byte array to display, starting at offset 0

Returns:String

The display String

toHexStringback to summary
public static String toHexString(ByteBuffer buffer, int offset, int size)

Convert a byte buffer to a hexadecimal string for display

Parameters
buffer:ByteBuffer

Buffer to display, starting at offset 0

offset:int

Offset at which to start reading the buffer

size:int

Number of bytes to read from the buffer

Returns:String

The display String

toHexStringback to summary
public static String toHexString(ByteBuffer buffer, int offset, int size, boolean formatted)

Convert a byte buffer to a hexadecimal string for display

Parameters
buffer:ByteBuffer

Buffer to display, starting at offset 0

offset:int

Offset at which to start reading the buffer

size:int

Number of bytes to read from the buffer

formatted:boolean

flag indicating if formatting is required

Returns:String

The display String

toHexStringback to summary
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.

writeHexStringback to summary
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.

io.github.spannm.jackcess.impl back to summary

public Class ByteUtil.ByteStream

extends OutputStream
Class Inheritance
Known Direct Subclasses
io.github.spannm.jackcess.impl.General97IndexCodes.NibbleStream, io.github.spannm.jackcess.impl.GeneralLegacyIndexCodes.ExtraCodesStream

Utility byte stream similar to ByteArrayOutputStream but with extended accessibility to the bytes.

Field Summary

Modifier and TypeField and Description
private byte[]
private int
private int

Constructor Summary

AccessConstructor and Description
public
public
ByteStream(int capacity)

Method Summary

Modifier and TypeMethod and Description
protected void
ensureCapacity(int newLength)

protected void
ensureNewCapacity(int numBytes)

public byte
get(int offset)

public byte[]
public int
public void
public void
set(int offset, byte b)

public void
setBits(int offset, byte b)

public void
skip(int n)

public byte[]
public void
trimTrailing(byte minTrimCode, byte maxTrimCode)

public void
write(int b)

Implements abstract java.io.OutputStream.write.

Writes the specified byte to this output stream.

public void
write(byte[] b)

Overrides java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream.

public void
write(byte[] b, int offset, int len)

Overrides java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream.

public void
writeFill(int len, byte b)

public void
Inherited from java.io.OutputStream:
closeflushnullOutputStream

Field Detail

bytesback to summary
private byte[] bytes
lastLengthback to summary
private int lastLength
lengthback to summary
private int length

Constructor Detail

ByteStreamback to summary
public ByteStream()
ByteStreamback to summary
public ByteStream(int capacity)

Method Detail

ensureCapacityback to summary
protected void ensureCapacity(int newLength)
ensureNewCapacityback to summary
protected void ensureNewCapacity(int numBytes)
getback to summary
public byte get(int offset)
getBytesback to summary
public byte[] getBytes()
getLengthback to summary
public int getLength()
resetback to summary
public void reset()
setback to summary
public void set(int offset, byte b)
setBitsback to summary
public void setBits(int offset, byte b)
skipback to summary
public void skip(int n)
toByteArrayback to summary
public byte[] toByteArray()
trimTrailingback to summary
public void trimTrailing(byte minTrimCode, byte maxTrimCode)
writeback to summary
public void write(int b)

Implements abstract java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Annotations
@Override
writeback to summary
public void write(byte[] b)

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes b.length bytes from the specified byte array to this output stream. The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length).

Annotations
@Override
writeback to summary
public void write(byte[] b, int offset, int len)

Overrides java.io.OutputStream.write.

Doc from java.io.OutputStream.write.

Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

Annotations
@Override
writeFillback to summary
public void writeFill(int len, byte b)
writeToback to summary
public void writeTo(ByteUtil.ByteStream out)