Class COSStream

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

    public class COSStream
    extends COSDictionary
    implements java.io.Closeable
    This class represents a stream object in a PDF document.
    • Field Detail

      • closeStreamCache

        private boolean closeStreamCache
      • isWriting

        private boolean isWriting
      • LOG

        private static final org.apache.commons.logging.Log LOG
    • Constructor Detail

      • COSStream

        public COSStream()
        Creates a new stream with an empty dictionary.

        Try to avoid using this constructor because it creates a new scratch file in memory. Instead, use document.getDocument().createCOSStream() which will use the existing scratch file (in memory or in temp file) of the document.

      • COSStream

        public COSStream​(RandomAccessStreamCache streamCache)
        Creates a new stream with an empty dictionary. Data is stored in the given scratch file.
        Parameters:
        streamCache - Stream cache for writing stream data.
      • COSStream

        public COSStream​(RandomAccessStreamCache streamCache,
                         RandomAccessReadView randomAccessReadView)
                  throws java.io.IOException
        Creates a new stream with an empty dictionary. Data is read from the given random accessview. Written data is stored in the given scratch file.
        Parameters:
        streamCache - Stream cache for writing stream data.
        randomAccessReadView - source for the data to be read
        Throws:
        java.io.IOException - if the length of the random access view isn't available
    • Method Detail

      • checkClosed

        private void checkClosed()
                          throws java.io.IOException
        Throws if the random access backing store has been closed. Helpful for catching cases where a user tries to use a COSStream which has outlived its COSDocument.
        Throws:
        java.io.IOException
      • getStreamCache

        private RandomAccessStreamCache getStreamCache()
                                                throws java.io.IOException
        Throws:
        java.io.IOException
      • createRawInputStream

        public java.io.InputStream createRawInputStream()
                                                 throws java.io.IOException
        Returns a new InputStream which reads the encoded PDF stream data. Experts only!
        Returns:
        InputStream containing raw, encoded PDF stream data.
        Throws:
        java.io.IOException - If the stream could not be read.
      • createInputStream

        public COSInputStream createInputStream()
                                         throws java.io.IOException
        Returns a new InputStream which reads the decoded stream data.
        Returns:
        InputStream containing decoded stream data.
        Throws:
        java.io.IOException - If the stream could not be read.
      • createInputStream

        public COSInputStream createInputStream​(DecodeOptions options)
                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • createView

        public RandomAccessRead createView()
                                    throws java.io.IOException
        Returns a new RandomAccessRead which reads the decoded stream data.
        Returns:
        RandomAccessRead containing decoded stream data.
        Throws:
        java.io.IOException - If the stream could not be read.
      • createOutputStream

        public java.io.OutputStream createOutputStream()
                                                throws java.io.IOException
        Returns a new OutputStream for writing stream data, using the current filters.
        Returns:
        OutputStream for un-encoded stream data.
        Throws:
        java.io.IOException - If the output stream could not be created.
      • createOutputStream

        public java.io.OutputStream createOutputStream​(COSBase filters)
                                                throws java.io.IOException
        Returns a new OutputStream for writing stream data, using and the given filters.
        Parameters:
        filters - COSArray or COSName of filters to be used.
        Returns:
        OutputStream for un-encoded stream data.
        Throws:
        java.io.IOException - If the output stream could not be created.
      • createRawOutputStream

        public java.io.OutputStream createRawOutputStream()
                                                   throws java.io.IOException
        Returns a new OutputStream for writing encoded PDF data. Experts only!
        Returns:
        OutputStream for raw PDF stream data.
        Throws:
        java.io.IOException - If the output stream could not be created.
      • getFilterList

        private java.util.List<Filter> getFilterList()
                                              throws java.io.IOException
        Returns the list of filters.
        Throws:
        java.io.IOException
      • getLength

        public long getLength()
        Returns the length of the encoded stream.
        Returns:
        length in bytes
      • getFilters

        public COSBase getFilters()
        This will return the filters to apply to the byte stream. The method will return
        • null if no filters are to be applied
        • a COSName if one filter is to be applied
        • a COSArray containing COSNames if multiple filters are to be applied
        Returns:
        the COSBase object representing the filters
      • toTextString

        public java.lang.String toTextString()
        Returns the contents of the stream as a PDF "text string".
        Returns:
        the PDF string representation of the stream content
      • accept

        public void accept​(ICOSVisitor visitor)
                    throws java.io.IOException
        Description copied from class: COSDictionary
        visitor pattern double dispatch method.
        Overrides:
        accept in class COSDictionary
        Parameters:
        visitor - The object to notify when visiting this object.
        Throws:
        java.io.IOException - If there is an error visiting this object.
      • close

        public void close()
                   throws java.io.IOException
        Called by PDFBox when the PDDocument is closed, this closes the stream and removes the data. You will usually not need this.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException - if something went wrong when closing the stream
      • hasData

        public boolean hasData()
        Indicates whether the stream contains any data or not.
        Returns:
        true if the stream contains any data