com.hunnysoft.jmime
Class Base64EncoderW

java.lang.Object
  extended bycom.hunnysoft.jmime.Base64EncoderW

public class Base64EncoderW
extends java.lang.Object

Class that performs base64 encoding, with output to a char array.

This class is the same as Base64Encoder, except that the output is written to a character array, instead of a byte array. See the overview section of Base64Encoder for general information that applies to both Base64Encoder and Base64EncoderW.

See Also:
Base64Encoder

Constructor Summary
Base64EncoderW()
          Default constructor.
 
Method Summary
 java.lang.String encode(ByteString decoded)
          Performs single-step buffer-to-buffer base64 encoding.
 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.
 int maxLineLen()
          Gets the maximum line length of the encoded output.
 boolean outputCrLf()
          Gets the CRLF end-of-line characters option.
 void setMaxLineLen(int len)
          Sets the maximum line length of the encoded output.
 void setOutputCrLf(boolean b)
          Sets the CRLF end-of-line characters option.
 void setSuppressFinalNewline(boolean b)
          Sets the option to suppress a final newline in the output.
 void start()
          Starts a multiple-buffer encode operation.
 boolean suppressFinalNewline()
          Gets the option to suppress a final newline in the output.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Base64EncoderW

public Base64EncoderW()
Default constructor.

The constructor sets default values for all the options.

Method Detail

setMaxLineLen

public void setMaxLineLen(int len)
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, Integer.MAX_VALUE) and by setting the Suppress Final Newline option to true.

This method forces the maximum line length to be a multiple of 4. If len is not a multiple of 4, then the method rounds it down to the nearest multiple of 4.

The default value is 72.

Parameters:
len - maximum line length of the encoded output

maxLineLen

public int maxLineLen()
Gets the maximum line length of the encoded output.

Returns:
maximum line length of the encoded output
See Also:
setMaxLineLen(int)

setOutputCrLf

public void setOutputCrLf(boolean b)
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 to either TextUtil.LF_EOL or TextUtil.CRLF_EOL. (The default is TextUtil.LF_EOL.) Then, the base64 encoder sets the value of this option based on the value of TextUtil.EOL.

Parameters:
b - true value causes the encoder to output CR LF for the end-of-line characters; false causes it to output LF.

outputCrLf

public boolean outputCrLf()
Gets the CRLF end-of-line characters option.

Returns:
boolean value of this option
See Also:
setOutputCrLf(boolean)

setSuppressFinalNewline

public void setSuppressFinalNewline(boolean b)
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).

Parameters:
b - if true, the encoder suppresses a final newline; if false, the encoder always adds a final newline

suppressFinalNewline

public boolean suppressFinalNewline()
Gets the option to suppress a final newline in the output.

Returns:
boolean value of this option
See Also:
setSuppressFinalNewline(boolean)

start

public void start()
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 Base64EncoderW 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(ByteString) method for encoding.


encodeSegment

public void encodeSegment(ByteBuffer inBuf,
                          CharBuffer outBuf)
Encodes data from the input buffer to the output buffer.

This method is an essential part of the low-level interface and performs most of the work of encoding for the Base64EncoderW 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 method returns:

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

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

Parameters:
inBuf - input buffer
outBuf - output buffer

finish

public void finish(CharBuffer outBuf)
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 method to flush the internal buffer.

The following condition must be satisfied when you call the method:

The above condition must also be satisified after the method 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 method returns.

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

Parameters:
outBuf - output buffer

encode

public java.lang.String encode(ByteString decoded)
Performs single-step buffer-to-buffer base64 encoding.

To perform base64 encoding using this method, create a ByteString containing the data you want to encode and pass it as the method's argument. The returned String contains the encoded output.

This method makes it very simple to perform base64 encoding. The disadvantage of this method 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 method uses the low-level interface internally. Any options set for the encoder object have the same effect using either this method or the low-level interface.

Parameters:
decoded - byte string containing the data to be encoded
Returns:
string containing the encoded data