Base64Encoder provides two interfaces for performing base64 encoding.
A high-level interface encodes from an input String to an output String. This interface comprises a single member function, 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:
Maximum Line Length The value of this option determines the maximum length of a line in the encoder's output. If you don't want any line breaks at all, you may set the maximum line length to a very large value (for example, 0x7FFFFFFF). The allowed values must be a multiple of 4, and the library enforces this restriction by rounding down any value that is not a multiple of 4 (for example, it rounds 73, 74, or 75 down to 72). The default value is 72.
Output CR LF If you set this option to true, then the encoder uses CR LF as the end-of-line characters in the encoded output. If you set this option to false, then the encoder uses LF alone. The default value is true if TextUtil::EOL_CHARS is TextUtil::CRLF.
Suppress Final Newline If you set this option to true, then the encoder does not put a final newline (CR LF, or LF) at the end of the encoded output, unless the last line of output is a full line. If you set this option to false, then the encoder always adds a newline to the end of the encoded output, even if the last line is a partial line. If you don't want any end-of-line characters in the output, you may set this option to true and set the Maximum Line Length to a very large value. The default value is false (meaning that a final newline is always added).
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 member functions: start(), encodeSegment(), and finish(). The procedure is described here:
inBuf, set inBuf.bytes to a char 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.chars to a char array, set outBuf.pos to zero, and set outBuf.endPos to the length of the array referenced by outBuf.chars.
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.
You may use the same encoder object for multiple encode operations.
Public Member Functions | |
| Base64Encoder () | |
| Default constructor. | |
| ~Base64Encoder () | |
| Destructor. | |
| void | setMaxLineLen (int len) |
| Sets the maximum line length of the encoded output. | |
| int | maxLineLen () |
| Gets the maximum line length of the encoded output. | |
| void | setOutputCrLf (bool b) |
| Sets the CRLF end-of-line characters option. | |
| bool | outputCrLf () |
| Gets the CRLF end-of-line characters option. | |
| void | setSuppressFinalNewline (bool b) |
| Sets the option to suppress a final newline in the output. | |
| bool | suppressFinalNewline () |
| Gets the option to suppress a final newline in the output. | |
| void | start () |
| Starts a multiple-buffer encode operation. | |
| void | encodeSegment (ByteBuffer *inBuf, CharBuffer *outBuf) |
| Encodes data from the input buffer to the output buffer. | |
| void | finish (CharBuffer *outBuf) |
| Finishes a multiple-buffer encode operation. | |
| String | encode (const String &decoded) |
| Performs single-step buffer-to-buffer base64 encoding. | |
|
|
Default constructor. The constructor sets default values for all the options. |
|
|
Destructor. |
|
|
Performs single-step buffer-to-buffer base64 encoding. To perform base64 encoding using this function, create a String containing the data you want to encode and pass it as the function's argument. The returned String contains the encoded output. This member function makes it very simple to perform base64 encoding. 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 encoding of large data using limited memory. This member function uses the low-level interface internally. Any options set for the encoder object have the same effect using either this function or the low-level interface.
|
|
||||||||||||
|
Encodes 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 encoding for the Base64Encoder class. It takes an input buffer and an output buffer as parameters, and encodes data from the input buffer until the input buffer is empty or the output buffer is full. In other words, one of the following conditions is guaranteed to be satisfied when the function returns:
You may call the function multiple times to encode multiple buffers of input data. However, before you call the function, both of the following conditions should be true:
For more information on using the low-level interface, see the overview section for Base64Encoder.
|
|
|
Finishes a multiple-buffer encode operation. When you use the low-level interface, the encoder buffers some data internally. Therefore, after you have passed all input data to the encoder, you must call this member function to flush the internal buffer. The following condition must be satisfied when you call the function:
The above condition must also be satisified after the function returns in order to guarantee that all output data has been written to the output buffer. You may need to call finish() more than once before the above condition is satisfied when the function returns. For more information on using the low-level interface, see the overview section for Base64Encoder.
|
|
|
Gets the maximum line length of the encoded output.
|
|
|
Gets the CRLF end-of-line characters option.
|
|
|
Sets the maximum line length of the encoded output.
For MIME-compliant Internet mail, lines should be no longer than 76 characters. However, for applications other than mail, there may be no limit on line length. You may use this option to cause the encoder to output lines of any length. You may even force output without any line breaks by setting the maximum line length to a very large value (for example, This function forces the maximum line length to be a multiple of 4. If len is not a multiple of 4, then the function rounds it down to the nearest multiple of 4. The default value is 72.
|
|
|
Sets the CRLF end-of-line characters option. If this option is true, then the encoder uses CR LF as the end-of-line characters in the encoded output. If this option is false, then the encoder uses LF alone. Normally, you do not need to set this option, because the encoder performs correctly by default. When your program starts, and before you create any threads, set TextUtil::EOL_CHARS to either TextUtil::LF or TextUtil::CRLF. (The default is TextUtil::LF.) Then, the base64 encoder sets the value of this option based on the value of TextUtil::EOL_CHARS.
|
|
|
Sets the option to suppress a final newline in the output. If this option is true, then the encoder does not put a final newline (CR LF, or LF) at the end of the encoded output, unless the last line of output is a full line. If this option is false, then the encoder always adds a newline to the end of the encoded output, even if the last line is a partial line. If you don't want any end-of-line characters in the output, you may set this option to true and set the Maximum Line Length to a very large value. The default value is false (meaning that the encoder always adds a final newline).
|
|
|
Starts a multiple-buffer encode operation. If you use the low-level interface for multiple-buffer encoding, you must call start() to begin the encode operation. You may use a Base64Encoder instance for many encode operations, but you must call start() to begin each operation. For more information on using the low-level interface, see the overview section for Base64Encoder. You do not need to call this method if you use the encode() member function for encoding. |
|
|
Gets the option to suppress a final newline in the output.
|
Copyright © 2001-2007 Hunny Software, Inc. All rights reserved.