Main Page | Namespace List | Class Hierarchy | Class List | Namespace Members | Class Members

Base64Decoder Class Reference

List of all members.

Detailed Description

Base64 encoding encodes binary data into printable ASCII characters. The encoding is required because binary data cannot pass reliably through the Internet mail system. The encoding method is really quite simple: a group of three 8-bit bytes (3 x 8 bits = 24 bits) is encoded into a group of four printable characters. Only 64 different printable characters are used, so six bits uniquely identify each character. The four printable characters sufficiently encode the original three bytes (4 x 6 bits = 24 bits). The characters that occur in the encoding include the upper- and lower-case letters, the digits, and the characters "+" and "/". RFC 2045 describes the details of base64 encoding.

Base64Decoder provides two interfaces for performing base64 decoding.

A high-level interface decodes from an input String to an output String. This interface comprises a single member function, decode().

A low-level interface allows decoding by passing multiple buffers to the decoder. The correct procedure for using this interface is described below.

Using the Low-Level Interface

The low-level interface allows you to decode data one buffer at a time; thus you may decode data of unlimited size using a limited amount of memory. For example, if you want to decode data from an input file to an output file, you may read from the input file one buffer at a time, pass each buffer to the decoder, and write to the output file one buffer at a time.

The low-level interface comprises two member functions: start() and decodeSegment(). The procedure is described here:

  1. Call start() to initialize the decoder.

  2. Initialize an input buffer and an output buffer. These buffers are instances of CharBuffer and ByteBuffer, respectively. To initialize an input buffer named inBuf, set inBuf.chars to a char array that contains the data to be decoded, set inBuf.pos to the offset of the beginning of the data in inBuf.chars, and set inBuf.endPos to the offset of the first byte past the end of the data in inBuf.chars. To initialize an output buffer named outBuf, set outBuf.bytes to a char array, set outBuf.pos to zero, and set outBuf.endPos to the length of the array referenced by outBuf.bytes.

  3. Call decodeSegment() with the input buffer and output buffer as arguments.

  4. Check to see if the output buffer is full, if the input buffer is empty, or if a decoding error has occurred. If outBuf.pos == outBuf.endPos, then the output buffer is full, and you must make room in the output buffer before you call decodeSegment() again. If inBuf.pos == inBuf.endPos, then the input buffer is empty, and you must supply the input buffer with more data before you call decodeSegment() again. If decodeSegment() returns -1, then the decoder has detected a decoding error.

  5. Repeat steps 3 and 4 until the last input buffer is empty or until the decoder detects a decoding error.

You may use the same decoder object for multiple decode operations.

Dealing With Errors

The decoder correctly decodes all data that is correctly encoded. However, if the data is not correctly encoded, the decoder detects these errors. All decoding errors are treated as fatal errors -- the decoder does not try to recover. The decoder notifies your application of decoding errors by returning -1 from the decodeSegment() member function.


Public Member Functions

 Base64Decoder ()
 Default constructor.
 ~Base64Decoder ()
 Destructor.
void start ()
 Starts a multiple-buffer decode operation.
int decodeSegment (CharBuffer *inBuf, ByteBuffer *outBuf)
 Decodes data from the input buffer to the output buffer.
String decode (const String &encoded)
 Performs single-step buffer-to-buffer base64 decoding.


Constructor & Destructor Documentation

Base64Decoder  ) 
 

Default constructor.

~Base64Decoder  ) 
 

Destructor.


Member Function Documentation

String decode const String encoded  ) 
 

Performs single-step buffer-to-buffer base64 decoding.

To perform base64 decoding using this function, create a String containing the data you want to decode and pass it as the function's argument. The returned String contains the decoded output.

If a decoding error occurs, the function returns an empty string.

This member function makes it very simple to perform base64 decoding. The disadvantage of this function is that it requires all the data to be kept in memory for processing. You may use the low-level interface, described in the overview section, to perform base64 decoding of large data using limited memory.

This member function uses the low-level interface internally.

Parameters:
encoded string containing the encoded data
Returns:
string containing the decoded data

int decodeSegment CharBuffer inBuf,
ByteBuffer outBuf
 

Decodes data from the input buffer to the output buffer.

This member function is an essential part of the low-level interface and performs most of the work of decoding for the Base64Decoder class. It takes an input buffer and an output buffer as parameters, and decodes data from the input buffer until the input buffer is empty, the output buffer is full, or a decoding error has been detected. In other words, one of the following conditions is guaranteed to be satisfied when the function returns:

  • inBuf.pos == inBuf.endPos (input buffer empty)
  • outBuf.pos == outBuf.endPos (output buffer full)
  • return value is -1 (decoding error detected)

You may call the function multiple times to decode multiple buffers of input data. However, before you call the function, both of the following conditions should be true:

  • inBuf.pos < inBuf.endPos (input buffer data available)
  • outBuf.pos < outBuf.endPos (output buffer space available)

For more information on using the low-level interface, see the overview section for Base64Decoder.

Parameters:
inBuf input buffer
outBuf output buffer
Returns:
-1 if the decoder detects a decoding error; 0 otherwise

void start  ) 
 

Starts a multiple-buffer decode operation.

If you use the low-level interface for multiple-buffer decoding, you must call start() to begin the decode operation. You may use a Base64Decoder instance for many decode operations, but you must call start() to begin each operation.

For more information on using the low-level interface, see the overview section for Base64Decoder.

You do not need to call this method if you use the decode() member function for decoding.

Copyright © 2001-2007 Hunny Software, Inc. All rights reserved.