org.apache.james.mime4j.decoder
Class Base64OutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by org.apache.james.mime4j.decoder.Base64OutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class Base64OutputStream
extends java.io.FilterOutputStream

This class is based on Base64 and Base64OutputStream code from Commons-Codec 1.4 Provides Base64 encoding and decoding as defined by RFC 2045.

This class implements section 6.8. Base64 Content-Transfer-Encoding from RFC 2045 Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet Message Bodies by Freed and Borenstein.

Since:
1.0-dev
Version:
$Id$
Author:
Apache Software Foundation
See Also:
RFC 2045

Field Summary
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
Base64OutputStream(java.io.OutputStream os)
          Default constructor: lineLength is 76, and the lineSeparator is CRLF when encoding, and all forms can be decoded.
Base64OutputStream(java.io.OutputStream os, int lineLength)
           Consumer can use this constructor to choose a different lineLength when encoding (lineSeparator is still CRLF).
Base64OutputStream(java.io.OutputStream os, int lineLength, byte[] lineSeparator)
           Consumer can use this constructor to choose a different lineLength and lineSeparator when encoding.
 
Method Summary
 void close()
          Terminates the BASE64 coded content and flushes the internal buffers.
 void flush()
          Flushes this output stream and forces any buffered output bytes to be written out to the stream.
static boolean isBase64(byte octet)
          Returns whether or not the octet is in the base 64 alphabet.
 void write(byte[] b, int offset, int len)
          Writes len bytes from the specified b array starting at offset to this output stream.
 void write(int i)
          Writes the specified byte to this output stream.
 
Methods inherited from class java.io.FilterOutputStream
write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Base64OutputStream

public Base64OutputStream(java.io.OutputStream os)
Default constructor: lineLength is 76, and the lineSeparator is CRLF when encoding, and all forms can be decoded.


Base64OutputStream

public Base64OutputStream(java.io.OutputStream os,
                          int lineLength)

Consumer can use this constructor to choose a different lineLength when encoding (lineSeparator is still CRLF). All forms of data can be decoded.

Note: lineLengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.

Parameters:
lineLength - each line of encoded data will be at most this long (rounded up to nearest multiple of 4). If lineLength <= 0, then the output will not be divided into lines (chunks). Ignored when decoding.

Base64OutputStream

public Base64OutputStream(java.io.OutputStream os,
                          int lineLength,
                          byte[] lineSeparator)

Consumer can use this constructor to choose a different lineLength and lineSeparator when encoding. All forms of data can be decoded.

Note: lineLengths that aren't multiples of 4 will still essentially end up being multiples of 4 in the encoded data.

Parameters:
lineLength - Each line of encoded data will be at most this long (rounded up to nearest multiple of 4). Ignored when decoding. If <= 0, then output will not be divided into lines (chunks).
lineSeparator - Each line of encoded data will end with this sequence of bytes. If lineLength <= 0, then the lineSeparator is not used.
Throws:
java.lang.IllegalArgumentException - The provided lineSeparator included some base64 characters. That's not going to work!
Method Detail

isBase64

public static boolean isBase64(byte octet)
Returns whether or not the octet is in the base 64 alphabet.

Parameters:
octet - The value to test
Returns:
true if the value is defined in the the base 64 alphabet, false otherwise.

write

public void write(int i)
           throws java.io.IOException
Writes the specified byte to this output stream.

Overrides:
write in class java.io.FilterOutputStream
Throws:
java.io.IOException

write

public void write(byte[] b,
                  int offset,
                  int len)
           throws java.io.IOException
Writes len bytes from the specified b array starting at offset to this output stream.

Overrides:
write in class java.io.FilterOutputStream
Parameters:
b - source byte array
offset - where to start reading the bytes
len - maximum number of bytes to write
Throws:
java.io.IOException - if an I/O error occurs.
java.lang.NullPointerException - if the byte array parameter is null
java.lang.IndexOutOfBoundsException - if offset, len or buffer size are invalid

flush

public void flush()
           throws java.io.IOException
Flushes this output stream and forces any buffered output bytes to be written out to the stream.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.FilterOutputStream
Throws:
java.io.IOException - if an I/O error occurs.

close

public void close()
           throws java.io.IOException
Terminates the BASE64 coded content and flushes the internal buffers. This method does NOT close the underlying output stream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.FilterOutputStream
Throws:
java.io.IOException


Copyright © 2004-2008 The Apache Software Foundation. All Rights Reserved.