com.mindbright.util
Class ASCIIArmour

java.lang.Object
  extended bycom.mindbright.util.ASCIIArmour

public final class ASCIIArmour
extends java.lang.Object

Encode/decode binary data to/from ASCII. Typical output looks like this:

 -----BEGIN RSA PRIVATE KEY-----
 Proc-Type: 4,ENCRYPTED
 DEK-Info: DES-EDE3-CBC,A68BCF5FE86D7652

 ZlH6zo6nf8mBxFPGkaUkfXtzgUDTKmUYAUO24eXkoV/fbg+IPOtbPdJa7PKlhQTs
 2Ycjb5Gk/ZVwGZaqa01roRSmfGDHL2ZSVrXHHMxDxCn1aU+rOFHcUA==
 -----END RSA PRIVATE KEY-----
 

See Also:
Base64

Field Summary
static int DEFAULT_LINE_LENGTH
          Default length of encoded lines
 
Constructor Summary
ASCIIArmour(java.lang.String headerLinePrePostFix)
          Creates an instance ready for decoding
ASCIIArmour(java.lang.String headerLine, java.lang.String tailLine)
          Creates an instance ready for encoding or decoding
ASCIIArmour(java.lang.String headerLine, java.lang.String tailLine, boolean blankHeaderSep, int lineLen)
          Creates an instance ready for encoding or decoding
 
Method Summary
 byte[] decode(byte[] data)
          Decode the given array.
 byte[] decode(byte[] data, int offset, int length)
          Decode the given array.
 byte[] decode(java.io.InputStream in)
          Decode data from the given InputStream.
 byte[] encode(byte[] data)
          Encode data and return the encoded ascii blob
 byte[] encode(byte[] data, int offset, int length)
          Encode data and return the encoded ascii blob
 void encode(java.io.OutputStream out, byte[] data)
          Encode data and print it to the given OutputStream
 void encode(java.io.OutputStream out, byte[] data, int off, int len)
          Encode data and print it to the given OutputStream
 java.lang.String getHeaderField(java.lang.String headerName)
          Get one header field
 java.util.Hashtable getHeaderFields()
          Get the header fields
 java.lang.String getHeaderLine()
          Get the header line
 java.lang.String printHeaders()
          Print the header fields to a String.
 void setBlankHeaderSep(boolean value)
          Control if a blank line should follow the headers
 void setCanonicalLineEnd(boolean value)
          Set if canoncial end of line markings should be used or not
 void setHeaderField(java.lang.String headerName, java.lang.String value)
          Set a header field
 void setHeaderLine(java.lang.String headerLine)
          Set the header line
 void setLineLength(int lineLen)
          Set length of encoded lines
 void setTailLine(java.lang.String tailLine)
          Set the tailing line
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_LINE_LENGTH

public static final int DEFAULT_LINE_LENGTH
Default length of encoded lines

See Also:
Constant Field Values
Constructor Detail

ASCIIArmour

public ASCIIArmour(java.lang.String headerLine,
                   java.lang.String tailLine,
                   boolean blankHeaderSep,
                   int lineLen)
Creates an instance ready for encoding or decoding

Parameters:
headerLine - the first line in the file
tailLine - the last line in the file
blankHeaderSep - true if a blank line should follow the header lines
lineLen - maximum length of generated lines

ASCIIArmour

public ASCIIArmour(java.lang.String headerLine,
                   java.lang.String tailLine)
Creates an instance ready for encoding or decoding

Parameters:
headerLine - the first line in the file
tailLine - the last line in the file

ASCIIArmour

public ASCIIArmour(java.lang.String headerLinePrePostFix)
Creates an instance ready for decoding

Parameters:
headerLinePrePostFix - start of header and tail line
Method Detail

setCanonicalLineEnd

public void setCanonicalLineEnd(boolean value)
Set if canoncial end of line markings should be used or not

Parameters:
value - true means CRLF which false means LF

setBlankHeaderSep

public void setBlankHeaderSep(boolean value)
Control if a blank line should follow the headers


setLineLength

public void setLineLength(int lineLen)
Set length of encoded lines


getHeaderLine

public java.lang.String getHeaderLine()
Get the header line


setHeaderLine

public void setHeaderLine(java.lang.String headerLine)
Set the header line


setTailLine

public void setTailLine(java.lang.String tailLine)
Set the tailing line


getHeaderFields

public java.util.Hashtable getHeaderFields()
Get the header fields

Returns:
a Hashtable where the leys are the names of the header fields

getHeaderField

public java.lang.String getHeaderField(java.lang.String headerName)
Get one header field


setHeaderField

public void setHeaderField(java.lang.String headerName,
                           java.lang.String value)
Set a header field


encode

public byte[] encode(byte[] data)
Encode data and return the encoded ascii blob

Parameters:
data - the array which will be encoded

encode

public byte[] encode(byte[] data,
                     int offset,
                     int length)
Encode data and return the encoded ascii blob

Parameters:
data - array in which the data can be found
offset - offset in array where data begins
length - how many bytes the data consists of

encode

public void encode(java.io.OutputStream out,
                   byte[] data)
            throws java.io.IOException
Encode data and print it to the given OutputStream

Parameters:
out - where to send the encoded blob
data - the array which will be encoded
Throws:
java.io.IOException

encode

public void encode(java.io.OutputStream out,
                   byte[] data,
                   int off,
                   int len)
            throws java.io.IOException
Encode data and print it to the given OutputStream

Parameters:
out - where to send the encoded blob
data - array in which the data can be found
off - offset in array where data begins
len - how many bytes the data consists of
Throws:
java.io.IOException

decode

public byte[] decode(byte[] data)
Decode the given array. It will also populate the list of header fields.

Parameters:
data - array containg the ascii blob to decode
Returns:
the decoded binary blob

decode

public byte[] decode(byte[] data,
                     int offset,
                     int length)
Decode the given array. It will also populate the list of header fields.

Parameters:
data - array containg the ascii blob to decode
offset - offset in array where data begins
length - how many bytes the data consists of
Returns:
the decoded binary blob

decode

public byte[] decode(java.io.InputStream in)
              throws java.io.IOException
Decode data from the given InputStream. It will also populate the list of header fields.

Parameters:
in - stream to read data from
Returns:
the decoded binary blob
Throws:
java.io.IOException

printHeaders

public java.lang.String printHeaders()
Print the header fields to a String.