Class COSStream

All Implemented Interfaces:
Closeable, AutoCloseable, COSObjectable, Encryptable

public class COSStream extends COSDictionary implements Closeable, Encryptable
This class represents a stream object in a PDF document.
Author:
Ben Litchfield
  • Constructor Details

    • COSStream

      public COSStream()
    • COSStream

      public COSStream(COSDictionary dictionary)
      Parameters:
      dictionary - The dictionary that is associated with this stream.
    • COSStream

      public COSStream(COSDictionary dictionary, org.sejda.io.SeekableSource seekableSource, long startingPosition, long length)
      Creates a stream with the given dictionary and where filtered data is a view of the given SeekableSource.
      Parameters:
      dictionary - The dictionary that is associated with this stream.
      seekableSource - the source where filtered data is read from
      startingPosition - starting position of the stream data in the SeekableSource
      length - the length of the stream data
  • Method Details

    • getFilteredStream

      public final InputStream getFilteredStream() throws IOException
      Returns:
      the (encoded) stream with all of the filters applied.
      Throws:
      IOException - when encoding/decoding causes an exception
    • doGetFilteredStream

      protected InputStream doGetFilteredStream() throws IOException
      Throws:
      IOException
    • getFilteredSource

      public org.sejda.io.SeekableSource getFilteredSource() throws IOException
      Returns:
      the (encoded) SeekableSource with all of the filters applied.
      Throws:
      IOException - when encoding/decoding causes an exception
    • getFilteredLength

      public long getFilteredLength() throws IOException
      Returns:
      the length of the encoded stream as long
      Throws:
      IOException
    • getUnfilteredStream

      public InputStream getUnfilteredStream() throws IOException
      Returns:
      the (decoded) stream with all of the filters applied.
      Throws:
      IOException - when encoding/decoding causes an exception
    • getUnfilteredSource

      public org.sejda.io.SeekableSource getUnfilteredSource() throws IOException
      Returns:
      the (decoded) SeekableSource with all of the filters applied.
      Throws:
      IOException - when encoding/decoding causes an exception
    • getUnfilteredByteBuffer

      public ByteBuffer getUnfilteredByteBuffer() throws IOException
      Returns:
      the (decoded) stream in the form of a read only ByteBuffer with all of the filters applied.
      Throws:
      IOException - when encoding/decoding causes an exception
    • getUnfilteredLength

      public long getUnfilteredLength() throws IOException
      Returns:
      the length of the decoded stream as long
      Throws:
      IOException
    • getDecodeResult

      public DecodeResult getDecodeResult() throws IOException
      Returns:
      the repaired stream parameters dictionary
      Throws:
      IOException - when encoding/decoding causes an exception
    • accept

      public void accept(COSVisitor visitor) throws IOException
      Description copied from class: COSBase
      Visitor pattern for the COS model objects
      Overrides:
      accept in class COSDictionary
      Throws:
      IOException
    • 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
    • hasFilter

      public boolean hasFilter(COSName filter)
      Parameters:
      filter -
      Returns:
      true if the stream has the given filter in the filters list
    • setEncryptor

      public void setEncryptor(Function<InputStream,InputStream> encryptor)
      Sets the function to be used to encrypt this stream.
      Parameters:
      encryptor -
    • createFilteredStream

      public OutputStream createFilteredStream()
      Creates a new stream for which filtered byte should be written to. You probably don't want this but want to use the createUnfilteredStream, which is used to write raw bytes to.
      Returns:
      A stream that can be written to.
    • createFilteredStream

      public OutputStream createFilteredStream(COSBase filters)
      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:
      IOException - If the output stream could not be created.
    • setFilters

      public void setFilters(COSBase filters) throws IOException
      set the filters to be applied to the stream.
      Parameters:
      filters - The filters to set on this stream.
      Throws:
      IOException - If there is an error clearing the old filters.
    • addCompression

      public boolean addCompression()
      Adds Flate decode filter to the current filters list if possible
    • encryptable

      public boolean encryptable()
      Specified by:
      encryptable in interface Encryptable
      Returns:
      true if the can be encrypted
    • encryptable

      public void encryptable(boolean encryptable)
      Description copied from interface: Encryptable
      Sets if the object can be encrypted
      Specified by:
      encryptable in interface Encryptable
    • createUnfilteredStream

      public OutputStream createUnfilteredStream()
      This will create an output stream that can be written to.
      Returns:
      An output stream which raw data bytes should be written to.
      Throws:
      IOException - If there is an error creating the stream.
    • isEmpty

      public boolean isEmpty() throws IOException
      Throws:
      IOException
    • asTextString

      public String asTextString()
      Returns:
      the contents of the stream as a text string. Text string as defined in Chap 7.9 of PDF 32000-1:2008.
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • unDecode

      public void unDecode()
      offload decoded/unfiltered data leaving the COSStrem in its filtered state and reducing memory footprint
    • indirectLength

      public boolean indirectLength()
      Returns:
      true if the writer should write this stream length as indirect
    • indirectLength

      public void indirectLength(boolean indirectLength)
      Parameters:
      indirectLength - if the writer should write this stream length as indirect