com.jkristian.io
Class Base64DecodeOutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by java.io.FilterOutputStream
          extended by com.jkristian.io.Base64DecodeOutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

public class Base64DecodeOutputStream
extends java.io.FilterOutputStream

A filter that decodes output from base64 encoding to binary bytes. Unexpected bytes are silently ignored.

Base64 encoding is defined by RFC 2045 (MIME) section 6.8.


Field Summary
protected  boolean flushCausesReset
          whether flush() resets the decoder
protected static byte[] map
           
protected  byte n
          index of the next expected input sextet
protected static byte NUL
           
protected static byte PAD
           
protected  byte x
          the previous input sextet
 
Fields inherited from class java.io.FilterOutputStream
out
 
Constructor Summary
Base64DecodeOutputStream(java.io.OutputStream out)
           
 
Method Summary
 void flush()
           
static void main(java.lang.String[] args)
          A unit test.
 void setFlushCausesReset(boolean b)
          Subsequently, a call to flush() will reset this object's state if and only if the given parameter is true.
 void write(byte[] b, int off, int len)
           
 void write(int b)
           
 
Methods inherited from class java.io.FilterOutputStream
close, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

n

protected byte n
index of the next expected input sextet


x

protected byte x
the previous input sextet


flushCausesReset

protected boolean flushCausesReset
whether flush() resets the decoder


PAD

protected static final byte PAD
See Also:
Constant Field Values

NUL

protected static final byte NUL
See Also:
Constant Field Values

map

protected static final byte[] map
Constructor Detail

Base64DecodeOutputStream

public Base64DecodeOutputStream(java.io.OutputStream out)
Parameters:
out - should be buffered (e.g. a BufferedOutputStream) for best performance. This class will write to it one byte at a time.
Method Detail

write

public void write(int b)
           throws java.io.IOException
Overrides:
write in class java.io.FilterOutputStream
Throws:
java.io.IOException

write

public void write(byte[] b,
                  int off,
                  int len)
           throws java.io.IOException
Overrides:
write in class java.io.FilterOutputStream
Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.FilterOutputStream
Throws:
java.io.IOException

setFlushCausesReset

public void setFlushCausesReset(boolean b)
Subsequently, a call to flush() will reset this object's state if and only if the given parameter is true. The default setting (if this method is not called) is false.

For example, write("BA"); flush(); write("BA") will ordinarily produce the same sequence of decoded bytes as write("BABA"); that is {4, 0, 64}. But {4, 4} will be produced if setFlushCausesReset(true) is called before flush(). In the latter case, the second "BA" will be decoded as if it appeared at the beginning of a stream.

Parameters:
b - whether the decoder will be reset by a subsequent call to flush(). false (the default) is appropriate if flush() is called merely for performance optimization, and should not affect decoding. true is appropriate if a call to flush() indicates a significant boundary in the data stream, such that previous bytes should not be combined with subsequent bytes for decoding.

main

public static void main(java.lang.String[] args)
                 throws java.io.IOException
A unit test. Output each parameter, decode it from base64 and re-encode it. If the re-encoded string differs from the input parameter, output the decoded byte string to System.err in hexadecimal.

Throws:
java.io.IOException