Class Ascii85OutputStream

java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
com.orsonpdf.filter.Ascii85OutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class Ascii85OutputStream extends 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 Details

    • 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 Details

    • Ascii85OutputStream

      public Ascii85OutputStream(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(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(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 Details

    • startEncoding

      private void startEncoding() throws IOException
      Throws:
      IOException
    • write

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

      public void writeUnencoded(int b) throws 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:
      IOException - If the underlying output stream has an I/O error.
    • writeUnencoded

      public void writeUnencoded(byte[] b) throws 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:
      IOException - If the underlying output stream has an I/O error.
    • writeUnencoded

      public void writeUnencoded(byte[] b, int off, int len) throws 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:
      IOException - If the underlying output stream has an I/O error.
    • encode

      private void encode(int tuple, int count) throws 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:
      IOException - If an I/O error occurs.
    • flush

      public void flush() throws 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 Flushable
      Overrides:
      flush in class FilterOutputStream
      Throws:
      IOException