Class BaseParser

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String DEF
      This is a string constant that will be used for comparisons.
      protected COSDocument document
      This is the document that will be parsed.
      static byte[] ENDOBJ
      This is a byte array that will be used for comparisons.
      static byte[] ENDSTREAM
      This is a byte array that will be used for comparisons.
      protected boolean forceParsing
      Flag to skip malformed or otherwise unparseable input where possible.
      protected PushBackInputStream pdfSource
      This is the stream that will be read from.
      static java.lang.String PROP_PUSHBACK_SIZE
      system property allowing to define size of push back buffer.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        BaseParser()
      Default constructor.
      protected BaseParser​(byte[] input)
      Constructor.
        BaseParser​(java.io.InputStream input)
      Constructor.
        BaseParser​(java.io.InputStream input, boolean forceParsingValue)
      Constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void clearResources()
      Release all used resources.
      protected boolean isClosing()
      This will tell if the next character is a closing brace( close of PDF array ).
      protected boolean isClosing​(int c)
      This will tell if the next character is a closing brace( close of PDF array ).
      protected boolean isEndOfName​(char ch)
      Determine if a character terminates a PDF name.
      protected boolean isEOL()
      This will tell if the next byte to be read is an end of line byte.
      protected boolean isEOL​(int c)
      This will tell if the next byte to be read is an end of line byte.
      protected boolean isWhitespace()
      This will tell if the next byte is whitespace or not.
      protected boolean isWhitespace​(int c)
      This will tell if the next byte is whitespace or not.
      protected COSBoolean parseBoolean()
      This will parse a boolean object from the stream.
      protected COSArray parseCOSArray()
      This will parse a PDF array object.
      protected COSDictionary parseCOSDictionary()
      This will parse a PDF dictionary.
      protected COSName parseCOSName()
      This will parse a PDF name from the stream.
      protected COSStream parseCOSStream​(COSDictionary dic, RandomAccess file)
      This will read a COSStream from the input stream.
      protected COSString parseCOSString()
      This will parse a PDF string.
      protected COSString parseCOSString​(boolean isDictionary)
      Deprecated.
      Not needed anymore.
      protected COSBase parseDirObject()
      This will parse a directory object from the stream.
      protected java.lang.String readExpectedString​(java.lang.String theString)
      This will read bytes until the end of line marker occurs.
      protected int readGenerationNumber()
      This will read a integer from the Stream and throw an IllegalArgumentException if the integer value has more than the maximum object revision (i.e.
      protected int readInt()
      This will read an integer from the stream.
      protected java.lang.String readLine()
      This will read bytes until the first end of line marker occurs.
      protected long readLong()
      This will read an long from the stream.
      protected long readObjectNumber()
      This will read a long from the Stream and throw an IllegalArgumentException if the long value has more than 10 digits (i.e.
      protected java.lang.String readString()
      This will read the next string from the stream.
      protected java.lang.String readString​(int length)
      This will read the next string from the stream up to a certain length.
      protected java.lang.StringBuilder readStringNumber()
      This method is used to read a token by the readInt() method and the readLong() method.
      protected void readUntilEndStream​(java.io.OutputStream out)
      This method will read through the current stream object until we find the keyword "endstream" meaning we're at the end of this object.
      void setDocument​(COSDocument doc)
      Set the document for this stream.
      protected void skipSpaces()
      This will skip all spaces and comments that are present.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PROP_PUSHBACK_SIZE

        public static final java.lang.String PROP_PUSHBACK_SIZE
        system property allowing to define size of push back buffer.
        See Also:
        Constant Field Values
      • ENDSTREAM

        public static final byte[] ENDSTREAM
        This is a byte array that will be used for comparisons.
      • ENDOBJ

        public static final byte[] ENDOBJ
        This is a byte array that will be used for comparisons.
      • DEF

        public static final java.lang.String DEF
        This is a string constant that will be used for comparisons.
        See Also:
        Constant Field Values
      • pdfSource

        protected PushBackInputStream pdfSource
        This is the stream that will be read from.
      • document

        protected COSDocument document
        This is the document that will be parsed.
      • forceParsing

        protected final boolean forceParsing
        Flag to skip malformed or otherwise unparseable input where possible.
    • Constructor Detail

      • BaseParser

        public BaseParser()
        Default constructor.
      • BaseParser

        public BaseParser​(java.io.InputStream input,
                          boolean forceParsingValue)
                   throws java.io.IOException
        Constructor.
        Parameters:
        input - The input stream to read the data from.
        forceParsingValue - flag to skip malformed or otherwise unparseable input where possible
        Throws:
        java.io.IOException - If there is an error reading the input stream.
        Since:
        Apache PDFBox 1.3.0
      • BaseParser

        public BaseParser​(java.io.InputStream input)
                   throws java.io.IOException
        Constructor.
        Parameters:
        input - The input stream to read the data from.
        Throws:
        java.io.IOException - If there is an error reading the input stream.
      • BaseParser

        protected BaseParser​(byte[] input)
                      throws java.io.IOException
        Constructor.
        Parameters:
        input - The array to read the data from.
        Throws:
        java.io.IOException - If there is an error reading the byte data.
    • Method Detail

      • setDocument

        public void setDocument​(COSDocument doc)
        Set the document for this stream.
        Parameters:
        doc - The current document.
      • parseCOSDictionary

        protected COSDictionary parseCOSDictionary()
                                            throws java.io.IOException
        This will parse a PDF dictionary.
        Returns:
        The parsed dictionary.
        Throws:
        java.io.IOException - IF there is an error reading the stream.
      • parseCOSStream

        protected COSStream parseCOSStream​(COSDictionary dic,
                                           RandomAccess file)
                                    throws java.io.IOException
        This will read a COSStream from the input stream.
        Parameters:
        file - The file to write the stream to when reading.
        dic - The dictionary that goes with this stream.
        Returns:
        The parsed pdf stream.
        Throws:
        java.io.IOException - If there is an error reading the stream.
      • readUntilEndStream

        protected void readUntilEndStream​(java.io.OutputStream out)
                                   throws java.io.IOException
        This method will read through the current stream object until we find the keyword "endstream" meaning we're at the end of this 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. This method is optimized using buffered IO and reduced number of byte compare operations.
        Parameters:
        out - stream we write out to.
        Throws:
        java.io.IOException
      • parseCOSString

        @Deprecated
        protected COSString parseCOSString​(boolean isDictionary)
                                    throws java.io.IOException
        Deprecated.
        Not needed anymore. Use parseCOSString() instead. PDFBOX-1437
        This will parse a PDF string.
        Parameters:
        isDictionary - indicates if the stream is a dictionary or not
        Returns:
        The parsed PDF string.
        Throws:
        java.io.IOException - If there is an error reading from the stream.
      • parseCOSString

        protected COSString parseCOSString()
                                    throws java.io.IOException
        This will parse a PDF string.
        Returns:
        The parsed PDF string.
        Throws:
        java.io.IOException - If there is an error reading from the stream.
      • parseCOSArray

        protected COSArray parseCOSArray()
                                  throws java.io.IOException
        This will parse a PDF array object.
        Returns:
        The parsed PDF array.
        Throws:
        java.io.IOException - If there is an error parsing the stream.
      • isEndOfName

        protected boolean isEndOfName​(char ch)
        Determine if a character terminates a PDF name.
        Parameters:
        ch - The character
        Returns:
        true if the character terminates a PDF name, otherwise false.
      • parseCOSName

        protected COSName parseCOSName()
                                throws java.io.IOException
        This will parse a PDF name from the stream.
        Returns:
        The parsed PDF name.
        Throws:
        java.io.IOException - If there is an error reading from the stream.
      • parseBoolean

        protected COSBoolean parseBoolean()
                                   throws java.io.IOException
        This will parse a boolean object from the stream.
        Returns:
        The parsed boolean object.
        Throws:
        java.io.IOException - If an IO error occurs during parsing.
      • parseDirObject

        protected COSBase parseDirObject()
                                  throws java.io.IOException
        This will parse a directory object from the stream.
        Returns:
        The parsed object.
        Throws:
        java.io.IOException - If there is an error during parsing.
      • readString

        protected java.lang.String readString()
                                       throws java.io.IOException
        This will read the next string from the stream.
        Returns:
        The string that was read from the stream.
        Throws:
        java.io.IOException - If there is an error reading from the stream.
      • readExpectedString

        protected java.lang.String readExpectedString​(java.lang.String theString)
                                               throws java.io.IOException
        This will read bytes until the end of line marker occurs.
        Parameters:
        theString - The next expected string in the stream.
        Returns:
        The characters between the current position and the end of the line.
        Throws:
        java.io.IOException - If there is an error reading from the stream or theString does not match what was read.
      • readString

        protected java.lang.String readString​(int length)
                                       throws java.io.IOException
        This will read the next string from the stream up to a certain length.
        Parameters:
        length - The length to stop reading at.
        Returns:
        The string that was read from the stream of length 0 to length.
        Throws:
        java.io.IOException - If there is an error reading from the stream.
      • isClosing

        protected boolean isClosing()
                             throws java.io.IOException
        This will tell if the next character is a closing brace( close of PDF array ).
        Returns:
        true if the next byte is ']', false otherwise.
        Throws:
        java.io.IOException - If an IO error occurs.
      • isClosing

        protected boolean isClosing​(int c)
        This will tell if the next character is a closing brace( close of PDF array ).
        Parameters:
        c - The character to check against end of line
        Returns:
        true if the next byte is ']', false otherwise.
      • readLine

        protected java.lang.String readLine()
                                     throws java.io.IOException
        This will read bytes until the first end of line marker occurs. Note: if you later unread the results of this function, you'll need to add a newline character to the end of the string.
        Returns:
        The characters between the current position and the end of the line.
        Throws:
        java.io.IOException - If there is an error reading from the stream.
      • isEOL

        protected boolean isEOL()
                         throws java.io.IOException
        This will tell if the next byte to be read is an end of line byte.
        Returns:
        true if the next byte is 0x0A or 0x0D.
        Throws:
        java.io.IOException - If there is an error reading from the stream.
      • isEOL

        protected boolean isEOL​(int c)
        This will tell if the next byte to be read is an end of line byte.
        Parameters:
        c - The character to check against end of line
        Returns:
        true if the next byte is 0x0A or 0x0D.
      • isWhitespace

        protected boolean isWhitespace()
                                throws java.io.IOException
        This will tell if the next byte is whitespace or not.
        Returns:
        true if the next byte in the stream is a whitespace character.
        Throws:
        java.io.IOException - If there is an error reading from the stream.
      • isWhitespace

        protected boolean isWhitespace​(int c)
        This will tell if the next byte is whitespace or not. These values are specified in table 1 (page 12) of ISO 32000-1:2008.
        Parameters:
        c - The character to check against whitespace
        Returns:
        true if the next byte in the stream is a whitespace character.
      • skipSpaces

        protected void skipSpaces()
                           throws java.io.IOException
        This will skip all spaces and comments that are present.
        Throws:
        java.io.IOException - If there is an error reading from the stream.
      • readObjectNumber

        protected long readObjectNumber()
                                 throws java.io.IOException
        This will read a long from the Stream and throw an IllegalArgumentException if the long value has more than 10 digits (i.e. : bigger than OBJECT_NUMBER_THRESHOLD)
        Returns:
        the object number being read.
        Throws:
        java.io.IOException - if an I/O error occurs
      • readGenerationNumber

        protected int readGenerationNumber()
                                    throws java.io.IOException
        This will read a integer from the Stream and throw an IllegalArgumentException if the integer value has more than the maximum object revision (i.e. : bigger than GENERATION_NUMBER_THRESHOLD)
        Returns:
        the generation number being read.
        Throws:
        java.io.IOException - if an I/O error occurs
      • readInt

        protected int readInt()
                       throws java.io.IOException
        This will read an integer from the stream.
        Returns:
        The integer that was read from the stream.
        Throws:
        java.io.IOException - If there is an error reading from the stream.
      • readLong

        protected long readLong()
                         throws java.io.IOException
        This will read an long from the stream.
        Returns:
        The long that was read from the stream.
        Throws:
        java.io.IOException - If there is an error reading from the stream.
      • readStringNumber

        protected final java.lang.StringBuilder readStringNumber()
                                                          throws java.io.IOException
        This method is used to read a token by the readInt() method and the readLong() method.
        Returns:
        the token to parse as integer or long by the calling method.
        Throws:
        java.io.IOException - throws by the pdfSource methods.
      • clearResources

        public void clearResources()
        Release all used resources.