P2PWNC
Class Base64

java.lang.Object
  extended byP2PWNC.Base64

public class Base64
extends java.lang.Object

This class provides Base64 encoding and decoding functionality, according to the RCF 2045 specifications.

Version:
0.0.1
Author:
Pantelis Frangoudis, Elias Efstathiou

Field Summary
protected static byte[] EncTable
          Encoding table.
protected  int nLineLength
          Length of the lines of the base64 encoded text.
 
Constructor Summary
Base64()
          Default constructor.
Base64(int nLineLength)
          Sets nLineLength to the specified parameter.
 
Method Summary
 int CalculateB64Size(int nDataLen)
          Calculates the size of the resulting base64 encoded string.
 int CalculateRawSize(java.lang.String b64)
          Calculates the size of the raw byte array that will result from a dacoding operation performed on the given string.
protected  void DecodeBlock(byte[] in, byte[] out)
          Decodes a 4-byte base64-encoded block to raw bytes.
(package private)  int DecodeData(java.lang.String b64, byte[] raw)
          Decodes the base64 input data.
 int DecodeFile(java.lang.String infile, java.lang.String outfile)
          Decodes a base64 encoded input file.
protected  void EncodeBlock(byte[] in, byte[] out, int len)
          Encodes a 3-byte block to a 4-byte base64-encoded block.
(package private)  java.lang.String EncodeData(byte[] raw, int datalen)
          Base64-encodes a raw byte array of datalen length.
 int EncodeFile(java.lang.String infile, java.lang.String outfile)
          Base64-encodes the input file
static void main(java.lang.String[] args)
           
protected  boolean ShouldBeIgnored(char c)
          Returns true if c is a character that should be ignored during the decoding process (that is, tab, space, cr, lf)
protected  B64Data TranslateB64(java.lang.String b64data, int inlen)
          Reads base64-encoded data, removes blank/carriage return/linefeed characters
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

nLineLength

protected int nLineLength
Length of the lines of the base64 encoded text. It defaults to 64 characters.


EncTable

protected static byte[] EncTable
Encoding table. It includes the 64 legal ascii characters.

Constructor Detail

Base64

public Base64()
Default constructor. It sets nLineLength to 64.


Base64

public Base64(int nLineLength)
Sets nLineLength to the specified parameter. If the value supplied is not a multiple of 4, then the default value of 64 is used.

Parameters:
nLineLength - int Length of the lines of the base64 encoded data.
Method Detail

ShouldBeIgnored

protected boolean ShouldBeIgnored(char c)
Returns true if c is a character that should be ignored during the decoding process (that is, tab, space, cr, lf)

Parameters:
c - char The character that is to be checked
Returns:
boolean Returns true if the character should be ignored. Otherwise, it returns false

TranslateB64

protected B64Data TranslateB64(java.lang.String b64data,
                               int inlen)
Reads base64-encoded data, removes blank/carriage return/linefeed characters

Parameters:
b64data - String
inlen - int Length of the input data
Returns:
B64Data

EncodeBlock

protected void EncodeBlock(byte[] in,
                           byte[] out,
                           int len)
Encodes a 3-byte block to a 4-byte base64-encoded block. This function is used internally by the EncodeData and EncodeFile functions.

Parameters:
in - Input data block. Is is a block of original data that will be converted to a block of 4 characters.
out - byte[] Output of the encoding operation on the input block. out is always a four character byte array. The four characters which are the result of the encoding operation.
len - int Length of the original block (in). Legal values are 1, 2, 3. If len is less than 3 we must pad with =

DecodeBlock

protected void DecodeBlock(byte[] in,
                           byte[] out)
Decodes a 4-byte base64-encoded block to raw bytes. This function is used internally by the EncodeData and EncodeFile functions.

Parameters:
in - byte[] 4-byte input array
out - byte[] Raw byte array (the result of the decoding operation of 4 bytes).

CalculateB64Size

public int CalculateB64Size(int nDataLen)
Calculates the size of the resulting base64 encoded string. (after the encoding the size is about 4/3 higher)

Parameters:
nDataLen - int Length of a raw byte array that will be base64-encoded
Returns:
Returns the calculated length

CalculateRawSize

public int CalculateRawSize(java.lang.String b64)
Calculates the size of the raw byte array that will result from a dacoding operation performed on the given string.

Parameters:
b64 - String A base64 encoded string.
Returns:
int Returnes the calculated length

EncodeData

java.lang.String EncodeData(byte[] raw,
                            int datalen)
Base64-encodes a raw byte array of datalen length. Data is output in lines of nLineLength characters.

Parameters:
raw - byte[] Input byte array that is to be encoded.
datalen - int Length of the raw data byte array.
Returns:
String The base64 encoding of the specified input byte array.

DecodeData

int DecodeData(java.lang.String b64,
               byte[] raw)
Decodes the base64 input data.

Parameters:
b64 - String Base64 encoding of raw data.
raw - byte[] This byte array stores the result of the decoding operation.
Returns:
int Length o9f the decoded data.

EncodeFile

public int EncodeFile(java.lang.String infile,
                      java.lang.String outfile)
Base64-encodes the input file

Parameters:
infile - String Path of the file that is to be encoded.
outfile - String Path of the file where the encoded data will be stored.
Returns:
int Length of the encoded data.

DecodeFile

public int DecodeFile(java.lang.String infile,
                      java.lang.String outfile)
Decodes a base64 encoded input file.

Parameters:
infile - String The path of the base64 encoded input file.
outfile - String The path of the decoded output file.
Returns:
int Length of the decoded file.

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
Throws:
java.io.IOException