Class Base64OutputStream

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

@Scriptable public final class Base64OutputStream extends 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();
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for Base64OutputStream.
    Constructor for Base64OutputStream.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close the stream, finish()ing encoding and flush()ing it first.
    void
    Finishes writing base64-encoded data to the output stream without closing the underlying stream.
    void
    Flush the stream.
    static int
    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.io.OutputStream

    nullOutputStream

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Base64OutputStream

      public Base64OutputStream(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.
    • Base64OutputStream

      public Base64OutputStream(XMLStreamWriter p_xmlStreamWriter)
      Constructor for Base64OutputStream. Creates a new base64 output stream to write data to the specified XMLStreamWriter with a default 512-byte buffer size.
      Parameters:
      p_xmlStreamWriter - The p_xmlStreamWriter to write to.
  • Method Details

    • 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 IOException
      Writes a single byte to the base64-encoded output stream.
      Specified by:
      write in class OutputStream
      Parameters:
      p_byte - The byte to be written.
      Throws:
      IOException - If an I/O error occurs.
    • write

      public void write(byte[] p_buf) throws IOException
      Writes an array of bytes to the base64-encoded output stream.
      Overrides:
      write in class OutputStream
      Parameters:
      p_buf - The data to be written.
      Throws:
      IOException - If an I/O error occurs.
    • write

      public void write(byte[] p_buf, int p_iOffs, int p_iLen) throws IOException
      Writes an array of bytes to the base64-encoded output stream.
      Overrides:
      write in class 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:
      IOException - If an I/O error occurs.
    • finish

      public void finish() throws IOException
      Finishes writing base64-encoded data to the output stream without closing the underlying stream.
      Throws:
      IOException - If an I/O error occurs.
    • flush

      public void flush() throws IOException
      Flush the stream. Note: at most two bytes may not be flushed unless finish() is applied.
      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
      Throws:
      IOException - If an I/O error occurs.
    • close

      public void close() throws IOException
      Close the stream, finish()ing encoding and flush()ing it first.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException - If an I/O error occurs.