de.uplanet.util.base64
Class Base64OutputStream

java.lang.Object
  extended by java.io.OutputStream
      extended by de.uplanet.util.base64.Base64OutputStream
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable

@Scriptable
public final class Base64OutputStream
extends java.io.OutputStream

Base64 encoding as described in RFC 2045 section 6.8.

0 A17 R34 i51 z
1 B18 S35 j52 0
2 C19 T36 k53 1
3 D20 U37 l54 2
4 E21 V38 m55 3
5 F22 W39 n56 4
6 G23 X40 o57 5
7 H24 Y41 p58 6
8 I25 Z42 q59 7
9 J26 a43 r60 8
10 K27 b44 s61 9
11 L28 c45 t62 +
12 M29 d46 u63 /
13 N30 e47 v
14 O31 f48 w(pad) =
15 P32 g49 x
16 Q33 h50 y


This stream-based encoding will outperform Base64Encoder when writing data sizes beyond about 400 bytes to a StringWriter provided that the output buffer is properly preallocated using the outputSizeFromInputSize(int) method. In these cases use the following pattern:

    byte[] l_data;

    // ...
    
    StringWriter       l_sw  = new StringWriter(Base64OutputStream.outputSizeFromInputSize(l_data.length));
    Base64OutputStream l_out = new Base64OutputStream(l_sw);

    l_out.write(l_data);
    l_out.close();

    String l_strEncoded = l_sw.toString();

Version:
$Revision: 55004 $
See Also:
Base64InputStream

Constructor Summary
Base64OutputStream(java.io.Writer p_out)
          Constructor for Base64OutputStream.
 
Method Summary
 void close()
          Close the stream, finish()ing encoding and flush()ing it first.
 void finish()
          Finishes writing base64-encoded data to the output stream without closing the underlying stream.
 void flush()
          Flush the stream.
static int outputSizeFromInputSize(int p_iSize)
          Calculates the size of the output for the given input size.
 void write(byte[] p_buf)
          Writes an array of bytes to the base64-encoded output stream.
 void write(byte[] p_buf, int p_iOffs, int p_iLen)
          Writes an array of bytes to the base64-encoded output stream.
 void write(int p_byte)
          Writes a single byte to the base64-encoded output stream.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Base64OutputStream

public Base64OutputStream(java.io.Writer p_out)
Constructor for Base64OutputStream. Creates a new base64 output stream to write data to the specified underlying output stream with a default 512-byte buffer size.

Parameters:
p_out - The underlying stream.
Method Detail

outputSizeFromInputSize

public static int outputSizeFromInputSize(int p_iSize)
Calculates the size of the output for the given input size. Note: values obtained for chunks of data are not additive.

Parameters:
p_iSize - The input size in bytes.
Returns:
The output size in characters.

write

public void write(int p_byte)
           throws java.io.IOException
Writes a single byte to the base64-encoded output stream.

Specified by:
write in class java.io.OutputStream
Parameters:
p_byte - The byte to be written.
Throws:
java.io.IOException - If an I/O error occurs.
See Also:
OutputStream.write(int)

write

public void write(byte[] p_buf)
           throws java.io.IOException
Writes an array of bytes to the base64-encoded output stream.

Overrides:
write in class java.io.OutputStream
Parameters:
p_buf - The data to be written.
Throws:
java.io.IOException - If an I/O error occurs.
See Also:
OutputStream.write(byte[])

write

public void write(byte[] p_buf,
                  int p_iOffs,
                  int p_iLen)
           throws java.io.IOException
Writes an array of bytes to the base64-encoded output stream.

Overrides:
write in class java.io.OutputStream
Parameters:
p_buf - The data to be written.
p_iOffs - The start offset of the data.
p_iLen - The length of the data.
Throws:
java.io.IOException - If an I/O error occurs.
See Also:
OutputStream.write(byte[], int, int)

finish

public void finish()
            throws java.io.IOException
Finishes writing base64-encoded data to the output stream without closing the underlying stream.

Throws:
java.io.IOException - If an I/O error occurs.

flush

public void flush()
           throws java.io.IOException
Flush the stream. Note: at most two bytes may not be flushed unless finish() is applied.

Specified by:
flush in interface java.io.Flushable
Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - if an I/O error occurs.
See Also:
OutputStream.flush()

close

public void close()
           throws java.io.IOException
Close the stream, finish()ing encoding and flush()ing it first.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.OutputStream
Throws:
java.io.IOException - if an I/O error occurs.
See Also:
OutputStream.close()


Last updated: 2011-08-11 10-04
Copyright © 2000-2011 United Planet. All Rights Reserved.