Hunny Software Library Reference

Base64Encoder Class

Class that performs base64 encoding.

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

System.Object
   Base64Encoder

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

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.

Base64Encoder provides two interfaces for performing base64 encoding.

A high-level interface encodes from an input ByteString to an output ByteString. This interface comprises a single method, Encode().

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

Base64Encoder allows you to change certain options, which affect the behavior of the encoder:

Using the Low-Level Interface

The low-level interface allows you to encode binary data one buffer at a time; thus you may encode data of unlimited size using a limited amount of memory. For example, if you want to encode 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 encoder, and write to the output file one buffer at a time.

The low-level interface comprises three methods: Start(), EncodeSegment(), and Finish(). The procedure is described here:

  1. Call Start() to initialize the encoder.
  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 encoded, 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 EncodeSegment() 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 EncodeSegment() 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 EncodeSegment() again.
  5. Repeat steps 3 and 4 until the last input buffer is empty.
  6. Call Finish() to flush any internally buffered data to the output buffer. If the output buffer is full after Finish() returns, you must make room in the output buffer and call Finish() again. If Finish() returns and the output buffer is not full, then the encoding is finished.

You may use the same encoder object for multiple encode operations.

Requirements

Namespace: Hunny.Mime

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

See Also

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