fc.io
Class HexOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by fc.io.HexOutputStream
All Implemented Interfaces:
Closeable, Flushable

public final class HexOutputStream
extends FilterOutputStream

Writes supplied bytes in hex/ascii form. Useful for hex dumps and debugging. Each write(..) method call is independent of previous invokations and prints data seperately from previous lines. This stream can also optionally print in other bases instead of hex.

In addition to it's streamy goodness, this class also provided some misc. static utility functions such as toHex(int)


Constructor Summary
HexOutputStream(OutputStream out)
          Constructs a new HexOutputStream with a default width of 16 hex numbers (and corresponding ascii values) per line.
HexOutputStream(OutputStream out, int width)
          Constructs a new HexOutputStream with the specified column width.
 
Method Summary
 void close()
          Be careful with this call, if the underlying stream is say, System.out, then calling this method will close System.out
 void flush()
           
static void main(String[] args)
          Unit Test:
 void setAutoFlush(boolean val)
          Sets this stream to flush it's contents after every write statement.
 void setBase(int base)
          Shows output in the specified base (instead of the default hex).
 void showLineNumbers(boolean val)
          true/false enables or disables showing line numbers at the beginning of each line, if there is more than 1 line in the output.
static String toHex(byte[] buf)
          Utility method to convert a byte buffer to a hexadecimal string.
 void write(byte[] buf)
           
 void write(byte[] buf, int off, int len)
           
 void write(int b)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HexOutputStream

public HexOutputStream(OutputStream out)
Constructs a new HexOutputStream with a default width of 16 hex numbers (and corresponding ascii values) per line.

Parameters:
out - the underlying outputstream to send the data to

HexOutputStream

public HexOutputStream(OutputStream out,
                       int width)
Constructs a new HexOutputStream with the specified column width.

Parameters:
out - the underlying outputstream to send the data to
width - the number of hex numbers to print per line
Method Detail

close

public void close()
Be careful with this call, if the underlying stream is say, System.out, then calling this method will close System.out

Specified by:
close in interface Closeable
Overrides:
close in class FilterOutputStream

flush

public void flush()
Specified by:
flush in interface Flushable
Overrides:
flush in class FilterOutputStream

write

public void write(int b)
Overrides:
write in class FilterOutputStream

write

public void write(byte[] buf)
Overrides:
write in class FilterOutputStream

write

public void write(byte[] buf,
                  int off,
                  int len)
Overrides:
write in class FilterOutputStream

setAutoFlush

public void setAutoFlush(boolean val)
Sets this stream to flush it's contents after every write statement. By default, this is true.


showLineNumbers

public void showLineNumbers(boolean val)
true/false enables or disables showing line numbers at the beginning of each line, if there is more than 1 line in the output. The line number prefix is a running counter of the number of bytes displayed so far. This counter is reset after at the beginning of each write method call.


setBase

public void setBase(int base)
Shows output in the specified base (instead of the default hex). To redundantly set to hex, specify 16. To show decimal, specify 10.

Parameters:
base - the base to show each byte in. Must be between 2 and 36 (inclusive)
Throws:
IllegalArgumentException - if the specified base is not between [2, 36]

toHex

public static final String toHex(byte[] buf)
Utility method to convert a byte buffer to a hexadecimal string. useful for md5 and other hashes.


main

public static void main(String[] args)
                 throws Exception
Unit Test:
Usage: java HexOutputStream 
options:
        -file filename 
        or
        -data string

Throws:
Exception