Class Ascii85OutputStream

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

    public class Ascii85OutputStream
    extends java.io.FilterOutputStream

    An ascii85 encoder, implemented as an OutputStream.

    Call flush() or close() to properly close the ascii85 block. The block must be closed for the encoded data to be valid. Do not call flush() before you intend to end the ascii85 block. Multiple ascii85 blocks may be encoded by calling flush() and then writing more bytes to the stream.

    Note that if you use the constructor with the useSpaceCompression option, the encoded text will be shorter when there are many consecutive space characters in the encoded data, but it will not be compatible with Adobe's ascii85 implementation. It makes sense to use this option if interoperability with other ascii85 implementations is not a requirement.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int count  
      private boolean encoding  
      private int pos  
      private int tuple  
      private boolean useSpaceCompression  
      private int width  
      • Fields inherited from class java.io.FilterOutputStream

        out
    • Constructor Summary

      Constructors 
      Constructor Description
      Ascii85OutputStream​(java.io.OutputStream out)
      Creates an output stream to encode ascii85 data, using a default line with of 72 characters and not using the space character compression option.
      Ascii85OutputStream​(java.io.OutputStream out, boolean useSpaceCompression)
      Creates an output stream to encode ascii85 data, using a default line width of 72 characters.
      Ascii85OutputStream​(java.io.OutputStream out, int width, boolean useSpaceCompression)
      Creates an output stream to encode ascii85 data.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void encode​(int tuple, int count)
      Encodes tuple and writes it to the output stream.
      void flush()
      Adds the closing block and flushes the underlying output stream.
      private void startEncoding()  
      void write​(int b)
      Writes a single byte to the stream.
      void writeUnencoded​(byte[] b)
      Writes bytes to the underlying output stream, unencoded.
      void writeUnencoded​(byte[] b, int off, int len)
      Writes bytes to the underlying output stream, unencoded.
      void writeUnencoded​(int b)
      Writes a single byte to the underlying output stream, unencoded.
      • Methods inherited from class java.io.FilterOutputStream

        close, write, write
      • 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
    • Field Detail

      • width

        private int width
      • pos

        private int pos
      • tuple

        private int tuple
      • count

        private int count
      • encoding

        private boolean encoding
      • useSpaceCompression

        private boolean useSpaceCompression
    • Constructor Detail

      • Ascii85OutputStream

        public Ascii85OutputStream​(java.io.OutputStream out)
        Creates an output stream to encode ascii85 data, using a default line with of 72 characters and not using the space character compression option. Call flush() to add the padding and end the ascii85 block.
        Parameters:
        out - the output stream.
      • Ascii85OutputStream

        public Ascii85OutputStream​(java.io.OutputStream out,
                                   boolean useSpaceCompression)
        Creates an output stream to encode ascii85 data, using a default line width of 72 characters. Call flush() to end the ascii85 block.
        Parameters:
        out - the output stream.
        useSpaceCompression - Whether to use space character compression in the output.
      • Ascii85OutputStream

        public Ascii85OutputStream​(java.io.OutputStream out,
                                   int width,
                                   boolean useSpaceCompression)
        Creates an output stream to encode ascii85 data. Call flush() to end the ascii85 block.
        Parameters:
        out - the output stream.
        width - The maximum line width of the encoded output text. Whitespace characters are ignored when decoding.
        useSpaceCompression - Whether to use space character compression in the output.
    • Method Detail

      • startEncoding

        private void startEncoding()
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • write

        public void write​(int b)
                   throws java.io.IOException
        Writes a single byte to the stream. See OutputStream.write(int b) for details.
        Overrides:
        write in class java.io.FilterOutputStream
        Parameters:
        b - The byte to encode.
        Throws:
        java.io.IOException - If an I/O error occurs in the underlying output stream.
      • writeUnencoded

        public void writeUnencoded​(int b)
                            throws java.io.IOException
        Writes a single byte to the underlying output stream, unencoded. If done improperly, this may corrupt the ascii85 data stream. Writing a byte using this method may cause the line length to increase since the line length counter will not be updated by this method.
        Parameters:
        b - The byte to write.
        Throws:
        java.io.IOException - If the underlying output stream has an I/O error.
      • writeUnencoded

        public void writeUnencoded​(byte[] b)
                            throws java.io.IOException
        Writes bytes to the underlying output stream, unencoded. If done improperly, this may corrupt the ascii85 data stream. Writing bytes using this method may cause the line length to increase since the line length counter will not be updated by this method.
        Parameters:
        b - The bytes to write.
        Throws:
        java.io.IOException - If the underlying output stream has an I/O error.
      • writeUnencoded

        public void writeUnencoded​(byte[] b,
                                   int off,
                                   int len)
                            throws java.io.IOException
        Writes bytes to the underlying output stream, unencoded. If done improperly, this may corrupt the ascii85 data stream. Writing bytes using this method may cause the line length to increase since the line length counter will not be updated by this method.
        Parameters:
        b - The bytes to write.
        off - The offset of b to start reading from.
        len - The amount of bytes to read from b.
        Throws:
        java.io.IOException - If the underlying output stream has an I/O error.
      • encode

        private void encode​(int tuple,
                            int count)
                     throws java.io.IOException
        Encodes tuple and writes it to the output stream. The number of bytes in the tuple, and thus the value of count is normally 4, however less bytes may also be encoded, particularly if the input stream has ended before the current tuple is full.
        Parameters:
        tuple - The tuple to encode.
        count - The number of bytes stuffed into the tuple.
        Throws:
        java.io.IOException - If an I/O error occurs.
      • flush

        public void flush()
                   throws java.io.IOException
        Adds the closing block and flushes the underlying output stream. This method should only be called if it is intended that the ascii85 block should be closed.
        Specified by:
        flush in interface java.io.Flushable
        Overrides:
        flush in class java.io.FilterOutputStream
        Throws:
        java.io.IOException