Class HexDumper

java.lang.Object
  |
  +--HexDumper

public class HexDumper
extends java.lang.Object

The HexDumper class provides a somewhat abstract way of transforming raw binary data into its ASCII hex value equivalent. It is capable of dumping an od(1)-like format to a specified PrintStream defaulting to System.out. It also contains methods for converting a byte[] array into its hex representation.

The formatBytes () static functions are provided for the convenience of the programmer who wishes to dump just a few bytes at a time.

The most recent source code for this class is available at its web reference page.


Field Summary
static char[] hexcodes
          An array of the hex characters in their logical positions for conveniently transforming a four-bit value into its hex code.
 
Constructor Summary
HexDumper()
          Creates a new HexDumper object which is prepared to dump to System.out.
HexDumper(java.io.PrintStream stream)
          Initializes a HexDumper object to print to an arbitrary PrintStream.
 
Method Summary
 void dumpBytes(byte[] data)
          Dumps a byte[] array to the associated PrintStream in a format similar to od(1).
 void dumpBytes(byte[] data, int offset, int len)
          Dumps a specified portion of a byte[] array to the associated PrintStream in a format similar to od(1).
static char[] formatBytes(byte[] data)
          Formats a given byte[] array as a sequence of ASCII hex pairs representing the bytes of the array.
static char[] formatBytes(byte[] data, int offset, int len)
          Formats a portion of a given byte[] array as a sequence of ASCII hex pairs representing the bytes of the array.
 int getCurrentOffset()
          Returns this HexDumper object's concept of the current offset.
 void setCurrentOffset(int offset)
          Allows the user to specify the offset to be printed for the immediate next byte dumped by this HexDumper object.
 void usePersistentOffset(boolean val)
          Sets a flag determining whether this HexDumper object should keep persistent offsets in between calls to dumpBytes () or not.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

hexcodes

public static final char[] hexcodes
An array of the hex characters in their logical positions for conveniently transforming a four-bit value into its hex code.
Constructor Detail

HexDumper

public HexDumper()
Creates a new HexDumper object which is prepared to dump to System.out.

HexDumper

public HexDumper(java.io.PrintStream stream)
Initializes a HexDumper object to print to an arbitrary PrintStream.
Parameters:
stream - PrintStream object to receive output from this HexDumper
Method Detail

formatBytes

public static char[] formatBytes(byte[] data)
Formats a given byte[] array as a sequence of ASCII hex pairs representing the bytes of the array.
Parameters:
data - Binary "string" to turn into a hex sequence
Returns:
A newly allocated char[] array containing the formatted data

formatBytes

public static char[] formatBytes(byte[] data,
                                 int offset,
                                 int len)
Formats a portion of a given byte[] array as a sequence of ASCII hex pairs representing the bytes of the array.
Parameters:
data - Binary "string" to turn into a hex sequence
offset - Offset of the first byte to format
len - Length in bytes of the portion of the array that should be hex-ified
Returns:
A newly allocated char[] array containing the formatted data

dumpBytes

public void dumpBytes(byte[] data)
Dumps a byte[] array to the associated PrintStream in a format similar to od(1).
Parameters:
data - Array of raw bytes to be converted and output

dumpBytes

public void dumpBytes(byte[] data,
                      int offset,
                      int len)
Dumps a specified portion of a byte[] array to the associated PrintStream in a format similar to od(1).
Parameters:
data - Array of raw bytes to be converted and output
offset - Offset of the first byte of data that should be output
len - The number of bytes of data that should be output in this translation

usePersistentOffset

public void usePersistentOffset(boolean val)
Sets a flag determining whether this HexDumper object should keep persistent offsets in between calls to dumpBytes () or not. Note that this affects only the printed offset, and not the offset into any data passed in.
Parameters:
val - New truth value determining if persistent offsets are to be used

setCurrentOffset

public void setCurrentOffset(int offset)
Allows the user to specify the offset to be printed for the immediate next byte dumped by this HexDumper object. If persistent offsets are not in use, this function effectively does nothing.
Parameters:
offset - Integer value to be used as the next offset; note that while this is a signed value, it will be output as raw hex. Thus, a negative value may indeed make sense here in order to produce very large offsets.

getCurrentOffset

public int getCurrentOffset()
Returns this HexDumper object's concept of the current offset. This is effectively the next address to be printed when more data is dumped by this object.
Returns:
An integer representing the current virtual data offset of this HexDumper object.