Elexis API documentation
Version 2.1.6 as of December 11 2011

com.healthmarketscience.jackcess
Class ByteUtil

java.lang.Object
  extended by com.healthmarketscience.jackcess.ByteUtil

public final class ByteUtil
extends java.lang.Object

Byte manipulation and display utilities

Author:
Tim McCune

Nested Class Summary
static class ByteUtil.ByteStream
          Utility byte stream similar to ByteArrayOutputStream but with extended accessibility to the bytes.
 
Method Summary
static int asUnsignedByte(byte b)
           
static int asUnsignedShort(short s)
           
static void clearRange(java.nio.ByteBuffer buffer, int start, int end)
          Sets all bits in the given byte range to 0.
static void clearRemaining(java.nio.ByteBuffer buffer)
          Sets all bits in the given remaining byte range to 0.
static void fillRange(java.nio.ByteBuffer buffer, int start, int end)
          Sets all bits in the given byte range to 1.
static int findRange(java.nio.ByteBuffer buffer, int start, byte[] pattern)
          Searches for a pattern of bytes in the given buffer starting at the given offset.
static int forward(java.nio.ByteBuffer buffer, int count)
          Moves the position of the given buffer the given count from the current position.
static int get3ByteInt(java.nio.ByteBuffer buffer)
          Read a 3 byte int from a buffer
static int get3ByteInt(java.nio.ByteBuffer buffer, java.nio.ByteOrder order)
          Read a 3 byte int from a buffer
static int get3ByteInt(java.nio.ByteBuffer buffer, int offset)
          Read a 3 byte int from a buffer
static int get3ByteInt(java.nio.ByteBuffer buffer, int offset, java.nio.ByteOrder order)
          Read a 3 byte int from a buffer
static int getInt(java.nio.ByteBuffer buffer, java.nio.ByteOrder order)
           
static int getInt(java.nio.ByteBuffer buffer, int offset, java.nio.ByteOrder order)
           
static int getUnsignedByte(java.nio.ByteBuffer buffer)
          Read an unsigned byte from a buffer
static int getUnsignedByte(java.nio.ByteBuffer buffer, int offset)
          Read an unsigned byte from a buffer
static int getUnsignedShort(java.nio.ByteBuffer buffer)
          Read an unsigned short from a buffer
static int getUnsignedShort(java.nio.ByteBuffer buffer, int offset)
          Read an unsigned short from a buffer
static int getUnsignedVarInt(java.nio.ByteBuffer buffer, int numBytes)
          Read an unsigned variable length int from a buffer
static int getUnsignedVarInt(java.nio.ByteBuffer buffer, int offset, int numBytes)
          Read an unsigned variable length int from a buffer
static boolean matchesRange(java.nio.ByteBuffer buffer, int start, byte[] pattern)
          Matches a pattern of bytes against the given buffer starting at the given offset.
static void put3ByteInt(java.nio.ByteBuffer buffer, int val)
          Put an integer into the given buffer at the given offset as a 3-byte integer.
static void put3ByteInt(java.nio.ByteBuffer buffer, int val, java.nio.ByteOrder order)
          Put an integer into the given buffer at the given offset as a 3-byte integer.
static void put3ByteInt(java.nio.ByteBuffer buffer, int val, int offset, java.nio.ByteOrder order)
          Put an integer into the given buffer at the given offset as a 3-byte integer.
static void putInt(java.nio.ByteBuffer buffer, int val, java.nio.ByteOrder order)
          Writes an int at the current position in the given buffer, using the given ByteOrder
static void putInt(java.nio.ByteBuffer buffer, int val, int offset, java.nio.ByteOrder order)
          Writes an int at the given position in the given buffer, using the given ByteOrder
static void putRange(java.nio.ByteBuffer buffer, int start, int end, byte b)
          Sets all bytes in the given byte range to the given byte value.
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 toHexFile(java.lang.String fileName, java.nio.ByteBuffer buffer, int offset, int size)
          Writes a chunk of data to a file in pretty printed hexidecimal.
static java.lang.String toHexString(byte[] array)
          Convert a byte array to a hexadecimal string for display
static java.lang.String toHexString(java.nio.ByteBuffer buffer, int size)
          Convert a byte buffer to a hexadecimal string for display
static java.lang.String toHexString(java.nio.ByteBuffer buffer, int offset, int size)
          Convert a byte buffer to a hexadecimal string for display
static java.lang.String toHexString(java.nio.ByteBuffer buffer, int offset, int size, boolean formatted)
          Convert a byte buffer to a hexadecimal string for display
static void writeHexString(java.nio.ByteBuffer buffer, java.lang.String hexStr)
          Writes a sequence of hexidecimal values into the given buffer, where every two characters represent one byte value.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

put3ByteInt

public static void put3ByteInt(java.nio.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 int
val - Int to convert

put3ByteInt

public static void put3ByteInt(java.nio.ByteBuffer buffer,
                               int val,
                               java.nio.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 int
val - Int to convert
order - the order to insert the bytes of the int

put3ByteInt

public static void put3ByteInt(java.nio.ByteBuffer buffer,
                               int val,
                               int offset,
                               java.nio.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 int
val - Int to convert
offset - offset at which to insert the int
order - the order to insert the bytes of the int

get3ByteInt

public static int get3ByteInt(java.nio.ByteBuffer buffer)
Read a 3 byte int from a buffer

Parameters:
buffer - Buffer containing the bytes
Returns:
The int

get3ByteInt

public static int get3ByteInt(java.nio.ByteBuffer buffer,
                              java.nio.ByteOrder order)
Read a 3 byte int from a buffer

Parameters:
buffer - Buffer containing the bytes
order - the order of the bytes of the int
Returns:
The int

get3ByteInt

public static int get3ByteInt(java.nio.ByteBuffer buffer,
                              int offset)
Read a 3 byte int from a buffer

Parameters:
buffer - Buffer containing the bytes
offset - Offset at which to start reading the int
Returns:
The int

get3ByteInt

public static int get3ByteInt(java.nio.ByteBuffer buffer,
                              int offset,
                              java.nio.ByteOrder order)
Read a 3 byte int from a buffer

Parameters:
buffer - Buffer containing the bytes
offset - Offset at which to start reading the int
order - the order of the bytes of the int
Returns:
The int

getUnsignedByte

public static int getUnsignedByte(java.nio.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(java.nio.ByteBuffer buffer,
                                  int offset)
Read an unsigned byte from a buffer

Parameters:
buffer - Buffer containing the bytes
offset - Offset at which to read the byte
Returns:
The unsigned byte as an int

getUnsignedShort

public static int getUnsignedShort(java.nio.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(java.nio.ByteBuffer buffer,
                                   int offset)
Read an unsigned short from a buffer

Parameters:
buffer - Buffer containing the short
offset - Offset at which to read the short
Returns:
The unsigned short as an int

getInt

public static int getInt(java.nio.ByteBuffer buffer,
                         java.nio.ByteOrder order)
Parameters:
buffer - Buffer containing the bytes
order - 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(java.nio.ByteBuffer buffer,
                         int offset,
                         java.nio.ByteOrder order)
Parameters:
buffer - Buffer containing the bytes
offset - Offset at which to start reading the int
order - 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(java.nio.ByteBuffer buffer,
                          int val,
                          java.nio.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 int
val - Int to insert
order - the order to insert the bytes of the int

putInt

public static void putInt(java.nio.ByteBuffer buffer,
                          int val,
                          int offset,
                          java.nio.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 int
val - Int to insert
offset - offset at which to insert the int
order - the order to insert the bytes of the int

getUnsignedVarInt

public static int getUnsignedVarInt(java.nio.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(java.nio.ByteBuffer buffer,
                                    int offset,
                                    int numBytes)
Read an unsigned variable length int from a buffer

Parameters:
buffer - Buffer containing the variable length int
offset - Offset at which to read the value
Returns:
The unsigned int

clearRemaining

public static void clearRemaining(java.nio.ByteBuffer buffer)
Sets all bits in the given remaining byte range to 0.


clearRange

public static void clearRange(java.nio.ByteBuffer buffer,
                              int start,
                              int end)
Sets all bits in the given byte range to 0.


fillRange

public static void fillRange(java.nio.ByteBuffer buffer,
                             int start,
                             int end)
Sets all bits in the given byte range to 1.


putRange

public static void putRange(java.nio.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(java.nio.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(java.nio.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

toHexString

public static java.lang.String toHexString(java.nio.ByteBuffer buffer,
                                           int size)
Convert a byte buffer to a hexadecimal string for display

Parameters:
buffer - Buffer to display, starting at offset 0
size - Number of bytes to read from the buffer
Returns:
The display String

toHexString

public static java.lang.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 java.lang.String toHexString(java.nio.ByteBuffer buffer,
                                           int offset,
                                           int size)
Convert a byte buffer to a hexadecimal string for display

Parameters:
buffer - Buffer to display, starting at offset 0
offset - Offset at which to start reading the buffer
size - Number of bytes to read from the buffer
Returns:
The display String

toHexString

public static java.lang.String toHexString(java.nio.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 0
offset - Offset at which to start reading the buffer
size - Number of bytes to read from the buffer
formatted - flag indicating if formatting is required
Returns:
The display String

writeHexString

public static void writeHexString(java.nio.ByteBuffer buffer,
                                  java.lang.String hexStr)
                           throws java.io.IOException
Writes a sequence of hexidecimal values into the given buffer, where every two characters represent one byte value.

Throws:
java.io.IOException

toHexFile

public static void toHexFile(java.lang.String fileName,
                             java.nio.ByteBuffer buffer,
                             int offset,
                             int size)
                      throws java.io.IOException
Writes a chunk of data to a file in pretty printed hexidecimal.

Throws:
java.io.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

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 swap
offset - 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 swap
offset - offset of the first byte of the bytes to swap

forward

public static int forward(java.nio.ByteBuffer buffer,
                          int count)
Moves the position of the given buffer the given count from the current position.

Returns:
the new buffer position

Elexis API documentation
Version 2.1.6 as of December 11 2011

Copyright 2005-2011 by Gerry Weirich, Elexis