Hunny Software Library Reference

Base64Decoder Class

Class that performs base64 decoding.

For a list of all members of this type, see Base64Decoder Members.

System.Object
   Base64Decoder

[Visual Basic]
Public Class Base64Decoder
[C#]
public class Base64Decoder

Remarks

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 ByteString to an output ByteString. This interface comprises a single method, 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 methods: 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 ByteBuffer. To initialize an input buffer named inBuf, set inBuf.Bytes to a byte array that contains the data to be decoded, set inBuf.Pos to the offset of the beginning of the data in inBuf.Bytes, and set inBuf.EndPos to the offset of the first byte past the end of the data in inBuf.Bytes. To initialize an output buffer named outBuf, set outBuf.Bytes to a byte 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 or if the input buffer is empty. 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.
  5. Repeat steps 3 and 4 until the last input buffer is empty.

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 throwing a DecodeException. Your program code should catch this exception.

Requirements

Namespace: Hunny.Mime

Assembly: Hunny.Mime (in Hunny.Mime.dll)

See Also

Base64Decoder Members | Hunny.Mime Namespace | Base64EncoderW | Base64 in RFC 2045