Class Base64OutputStream

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

    public class Base64OutputStream
    extends java.io.OutputStream
    The Base64OutputStream is used to write base64 text in the form of a string through a conventional output stream. This is provided for convenience so that it is possible to encode and decode binary data as base64 for implementations that would normally use a binary format.
    See Also:
    Base64Encoder
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte[] buffer  
      private int count  
      private char[] encoded  
      private byte[] temp  
    • Constructor Summary

      Constructors 
      Constructor Description
      Base64OutputStream()
      Constructor for the Base64OutputStream object.
      Base64OutputStream​(int capacity)
      Constructor for the Base64OutputStream object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      This is used to close the stream and encode the buffered bytes to base64.
      private void expand​(int capacity)
      This will expand the size of the internal buffer.
      java.lang.String toString()
      This returns the base64 text encoded from the bytes written to the stream.
      void write​(byte[] array, int offset, int length)
      This method is used to write data as base64 to an internal buffer.
      void write​(int octet)
      This method is used to write data as base64 to an internal buffer.
      • Methods inherited from class java.io.OutputStream

        flush, nullOutputStream, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • encoded

        private char[] encoded
      • buffer

        private byte[] buffer
      • temp

        private byte[] temp
      • count

        private int count
    • Constructor Detail

      • Base64OutputStream

        public Base64OutputStream()
        Constructor for the Base64OutputStream object. A stream created with this constructor uses an initial capacity of one kilobyte, the capacity is increased as bytes are written.
      • Base64OutputStream

        public Base64OutputStream​(int capacity)
        Constructor for the Base64OutputStream object. A stream created with this constructor can have an initial capacity specified. Typically it is a good rule of thumb to use a capacity that is just over an additional third of the source binary data.
        Parameters:
        capacity - this is the initial capacity of the buffer
    • Method Detail

      • write

        public void write​(int octet)
                   throws java.io.IOException
        This method is used to write data as base64 to an internal buffer. The toString method can be used to acquire the text encoded from the written binary data.
        Specified by:
        write in class java.io.OutputStream
        Parameters:
        octet - the octet to encode in to the internal buffer
        Throws:
        java.io.IOException
      • write

        public void write​(byte[] array,
                          int offset,
                          int length)
                   throws java.io.IOException
        This method is used to write data as base64 to an internal buffer. The toString method can be used to acquire the text encoded from the written binary data.
        Overrides:
        write in class java.io.OutputStream
        Parameters:
        array - the octets to encode to the internal buffer
        offset - this is the offset in the array to encode from
        length - this is the number of bytes to be encoded
        Throws:
        java.io.IOException
      • expand

        private void expand​(int capacity)
                     throws java.io.IOException
        This will expand the size of the internal buffer. To allow for a variable length number of bytes to be written the internal buffer can grow as demand exceeds space available.
        Parameters:
        capacity - this is the minimum capacity required
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.IOException
        This is used to close the stream and encode the buffered bytes to base64. Once this method is invoked no further data can be encoded with the stream. The toString method can be used to acquire the base64 encoded text.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        This returns the base64 text encoded from the bytes written to the stream. This is the primary means for acquiring the base64 encoded text once the stream has been closed.
        Overrides:
        toString in class java.lang.Object
        Returns:
        this returns the base64 text encoded