Class BaseCOSParser

java.lang.Object
org.sejda.sambox.input.SourceReader
org.sejda.sambox.input.BaseCOSParser
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
ContentStreamCOSParser, COSParser

abstract class BaseCOSParser extends SourceReader
Base parser for COS objects providing methods to get parsed objects from the given SeekableSource
  • Field Details

  • Constructor Details

    • BaseCOSParser

      BaseCOSParser(org.sejda.io.SeekableSource source)
  • Method Details

    • nextParsedToken

      public abstract COSBase nextParsedToken() throws IOException
      Returns:
      The next parsed basic type object from the stream or null if the next token is not a COSBase. Basic types are defined in Chap 7.3 of PDF 32000-1:2008
      Throws:
      IOException - If there is an error during parsing.
    • nextDictionary

      public COSDictionary nextDictionary() throws IOException
      Returns:
      The next parsed dictionary object from the stream. Dictionary objects are defined in Chap 7.3.7 of PDF 32000-1:2008
      Throws:
      IOException - If there is an error during parsing.
    • consumeInvalidDictionaryKey

      private boolean consumeInvalidDictionaryKey() throws IOException
      Consumes an invalid dictionary key
      Returns:
      true if the dictionary has been recovered and parsing can go on
      Throws:
      IOException
    • nextArray

      public COSArray nextArray() throws IOException
      Returns:
      The next parsed array object from the stream. Array objects are defined in Chap 7.3.6 of PDF 32000-1:2008
      Throws:
      IOException - If there is an error during parsing.
    • nextBoolean

      public COSBoolean nextBoolean() throws IOException
      Returns:
      The next parsed boolean object from the stream. Boolean objects are defined in Chap 7.3.2 of PDF 32000-1:2008
      Throws:
      IOException - If there is an error during parsing.
    • nextNumber

      public COSNumber nextNumber() throws IOException
      Returns:
      The next parsed numeric object from the stream. Numeric objects are defined in Chap 7.3.3 of PDF 32000-1:2008
      Throws:
      IOException - If there is an error during parsing.
    • nextNull

      public COSNull nextNull() throws IOException
      Returns:
      The next parsed null object from the stream. Null object is defined in Chap 7.3.9 of PDF 32000-1:2008
      Throws:
      IOException - If there is an error during parsing.
    • nextName

      public COSName nextName() throws IOException
      Returns:
      The next parsed name object from the stream. Name objects are defined in Chap 7.3.5 of PDF 32000-1:2008
      Throws:
      IOException - If there is an error during parsing.
    • nextLiteralString

      public COSString nextLiteralString() throws IOException
      Returns:
      The next parsed literal string object from the stream. Literal string objects are defined in Chap 7.3.4.2 of PDF 32000-1:2008
      Throws:
      IOException - If there is an error during parsing.
    • nextHexadecimalString

      public COSString nextHexadecimalString() throws IOException
      Returns:
      The next parsed hexadecimal string object from the stream. Hexadecimal string objects is defined in Chap 7.3.4.3 of PDF 32000-1:2008
      Throws:
      IOException - If there is an error during parsing.
    • nextString

      public COSString nextString() throws IOException
      Returns:
      The next parsed string object from the stream. String objects is defined in Chap 7.3.4 of PDF 32000-1:2008
      Throws:
      IOException - If there is an error during parsing.
    • nextStream

      public COSStream nextStream(COSDictionary streamDictionary) throws IOException
      This will read a COSStream from the input stream using length attribute within dictionary. If length attribute is a indirect reference it is first resolved to get the stream length. This means we copy stream data without testing for 'endstream' or 'endobj' and thus it is no problem if these keywords occur within stream. We require 'endstream' to be found after stream data is read.
      Parameters:
      streamDictionary - dictionary that goes with this stream.
      Returns:
      parsed pdf stream.
      Throws:
      IOException - if an error occurred reading the stream, like problems with reading length attribute, stream does not end with 'endstream' after data read, stream too short etc.
    • streamLength

      private long streamLength(COSDictionary streamDictionary) throws IOException
      Retrieves the stream length. It gets it from the dictionary, if not present there it applies fallback strategy searching for endstream or endobj keywords.
      Parameters:
      streamDictionary -
      Returns:
      the length
      Throws:
      IOException
    • streamLengthFrom

      private long streamLengthFrom(COSDictionary streamDictionary) throws IOException
      Parameters:
      streamDictionary -
      Returns:
      the stream length if found in the dictionary. -1 if nothing is found or if the length is incorrect.
      Throws:
      IOException
    • doStreamLengthFrom

      private long doStreamLengthFrom(COSBase lengthBaseObj) throws IOException
      Throws:
      IOException
    • findStreamLength

      private long findStreamLength() throws IOException
      Reads from the current position until it finds the "endstream" meaning we're at the end of this stream object. Some pdf files, however, forget to write some endstream tags and just close off objects with an "endobj" tag so we have to handle this case as well.
      Returns:
      the length from the current position to the position where "endstream" or "endobj" was found
      Throws:
      IOException
    • doFindStreamLength

      private long doFindStreamLength(long start) throws IOException
      Throws:
      IOException