Class Base64OutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public final class Base64OutputStream
    extends java.io.FilterOutputStream

    A Base64OutputStream encodes given bytes in Base64 characters and writes them in the given OutputStream.

    The Base 64 encoding

    • According to the RFC-2045, valid Base64 characters are: A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9,+,/.
    • Encoded data ARE splitted in lines of 76 characters, using the local line separator (System.getProperty("line.separator")).

    Buffer

    This stream is buffered. That means that encoded bytes sent to the inner output stream will be written when a given number of bytes are collected. By default the buffer size is: 8192.

    Warning ! Actually the buffer is not managed in Base64OutputStream but in its inner output stream. At the initialization the given output stream is used to create a BufferedOutputStream with (N/3)*4 as buffer size (where N is the given buffer size). Indeed, with the Base64 encoding, the number of encoded bytes is always greater than the number of the corresponding decoded bytes: 3 bytes will be encoded by 4 characters encoded on 6 bits (which allows an alphabet of 2^6=64 characters, hence base64). Consequently: a buffer of N bytes in a BufferedOutputStream corresponds to a buffer of (N/3)*4 bytes in its inner output stream. So, when you set a buffer size of 8192 bytes at the creation of a Base64OutputStream, it is really implemented by a buffer of 10924 bytes.

    Since:
    09/2011
    Author:
    Gregory Mantelet
    See Also:
    Base64
    • Field Summary

      • Fields inherited from class java.io.FilterOutputStream

        out
    • Constructor Summary

      Constructors 
      Constructor Description
      Base64OutputStream​(java.io.OutputStream stream)  
      Base64OutputStream​(java.io.OutputStream stream, int bufferSize)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Decodes and writes last given bytes and finally flushes and closes the inner output stream.
      void flush()
      ONLY FLUSH THE INNER OUTPUT STREAM !
      void write​(byte[] b)  
      void write​(byte[] b, int off, int len)  
      void write​(int b)  
      • Methods inherited from class java.io.OutputStream

        nullOutputStream
      • 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 stream)
        Parameters:
        stream -
      • Base64OutputStream

        public Base64OutputStream​(java.io.OutputStream stream,
                                  int bufferSize)
        Parameters:
        stream -
        bufferSize -
    • 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)
                   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
        ONLY FLUSH THE INNER OUTPUT STREAM !
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
        See Also:
        FilterOutputStream.flush()
      • close

        public void close()
                   throws java.io.IOException
        Decodes and writes last given bytes and finally flushes and closes the inner output stream.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.FilterOutputStream
        Throws:
        java.io.IOException
        See Also:
        FilterOutputStream.close()