Top Description Fields Constructors Methods
org.apache.pdfbox.pdfparser

public abstract Class BaseParser

extends Object
Class Inheritance
Known Direct Subclasses
org.apache.pdfbox.pdfparser.COSParser, org.apache.pdfbox.pdfparser.PDFObjectStreamParser, org.apache.pdfbox.pdfparser.PDFStreamParser, org.apache.pdfbox.pdfparser.PDFXrefStreamParser
Imports
java.io.ByteArrayOutputStream, .IOException, .UnsupportedEncodingException, java.nio.ByteBuffer, java.nio.charset.CharacterCodingException, .Charset, .CharsetDecoder, .CodingErrorAction, .StandardCharsets, java.util.HashMap, .Map, org.apache.commons.logging.Log, .LogFactory, org.apache.pdfbox.cos.COSArray, .COSBase, .COSBoolean, .COSDictionary, .COSDocument, .COSInteger, .COSName, .COSNull, .COSNumber, .COSObject, .COSObjectKey, .COSString, org.apache.pdfbox.io.RandomAccessRead

This class is used to contain parsing logic that will be used by all parsers.
Author
Ben Litchfield

Field Summary

Modifier and TypeField and Description
protected static final int
A

private static final Charset
protected static final byte
ASCII_CR

ASCII code for carriage return.

protected static final byte
ASCII_LF

ASCII code for line feed.

private static final byte
private static final byte
private static final byte
protected static final int
B

protected static final int
D

public static final String
DEF

This is a string constant that will be used for comparisons.

protected COSDocument
document

This is the document that will be parsed.

protected static final int
E

protected static final String
ENDOBJ_STRING

This is a string constant that will be used for comparisons.

protected static final String
ENDSTREAM_STRING

This is a string constant that will be used for comparisons.

private static final char[]
FALSE

This is a string constant that will be used for comparisons.

private static final long
protected static final int
J

private final Map<Long, COSObjectKey>
private static final Log
LOG

Log instance.

protected static final int
M

pack-priv static final int
protected static final int
N

private static final char[]
NULL

This is a string constant that will be used for comparisons.

protected static final int
O

private static final long
protected static final int
R

protected static final int
S

protected final RandomAccessRead
source

This is the stream that will be read from.

protected static final String
STREAM_STRING

This is a string constant that will be used for comparisons.

protected static final int
T

private static final char[]
TRUE

This is a string constant that will be used for comparisons.

private final CharsetDecoder

Constructor Summary

AccessConstructor and Description
pack-priv
BaseParser(RandomAccessRead pdfSource)

Default constructor.

Method Summary

Modifier and TypeMethod and Description
private int

Returns:

the corrected value of the brace counter
checkForEndOfString
(final int
the number of braces currently open.
bracesParameter
)

This is really a bug in the Document creators code, but it caused a crash in PDFBox, the first bug was in this format: /Title ( (5) /Creator which was patched in 1 place.

private String

Returns:

the decoded String
decodeBuffer
(ByteArrayOutputStream
the ByteArrayOutputStream containing the bytes to decode
buffer
)

Tries to decode the buffer cotent to an UTF-8 String.

private COSBase
protected COSObjectKey

Returns:

the COS object key
getObjectKey
(long
the given object number
num
,
int
the given generation number
gen
)

Returns the object key for the given combination of object and generation number.

protected boolean

Returns:

true if the next byte is ']', false otherwise.
isClosing
()

This will tell if the next character is a closing brace( close of PDF array ).

protected boolean

Returns:

true if the next byte is ']', false otherwise.
isClosing
(int
The character to check against end of line
c
)

This will tell if the next character is a closing brace( close of PDF array ).

private boolean
isCR(int c)

protected boolean

Returns:

true if the next byte in the stream is a digit.
isDigit
()

This will tell if the next byte is a digit or not.

protected static boolean

Returns:

true if the next byte in the stream is a digit.
isDigit
(int
The character to be checked
c
)

This will tell if the given value is a digit or not.

protected boolean

Returns:

true if the character terminates a PDF name, otherwise false.
isEndOfName
(int
The character
ch
)

Determine if a character terminates a PDF name.

protected boolean

Returns:

true if the end of the data is reached.
isEOF
()

This will tell if the end of the data is reached.

protected boolean

Returns:

true if the next byte is 0x0A or 0x0D.
isEOL
()

This will tell if the next byte to be read is an end of line byte.

protected boolean

Returns:

true if the next byte is 0x0A or 0x0D.
isEOL
(int
The character to check against end of line
c
)

This will tell if the next byte to be read is an end of line byte.

private static boolean
isHexDigit(char ch)

private boolean
isLF(int c)

protected boolean

Returns:

true if the next byte in the stream is a space character.
isSpace
()

This will tell if the next byte is a space or not.

protected boolean

Returns:

true if the next byte in the stream is a space character.
isSpace
(int
The character to check against space
c
)

This will tell if the given value is a space or not.

protected boolean

Returns:

true if the next byte in the stream is a whitespace character.
isWhitespace
()

This will tell if the next byte is whitespace or not.

protected static boolean

Returns:

true if the character is a whitespace character.
isWhitespace
(int
The character to check against whitespace
c
)

This will tell if a character is whitespace or not.

protected COSArray

Returns:

The parsed PDF array.
parseCOSArray
()

This will parse a PDF array object.

protected COSDictionary

Returns:

The parsed dictionary, never null.
parseCOSDictionary
(boolean
indicates whether the dictionary to be read is a direct object
isDirect
)

This will parse a PDF dictionary.

private boolean
private COSBase

Returns:

The parsed Dictionary object.
parseCOSDictionaryValue
()

This will parse a PDF dictionary value.

private COSString

Returns:

The parsed PDF string.
parseCOSHexString
()

This will parse a PDF HEX string with fail fast semantic meaning that we stop if a not allowed character is found.

protected COSName

Returns:

The parsed PDF name.
parseCOSName
()

This will parse a PDF name from the stream.

private COSNumber
protected COSString

Returns:

The parsed PDF string.
parseCOSString
()

This will parse a PDF string.

protected COSBase

Returns:

The parsed object.
parseDirObject
()

This will parse a directory object from the stream.

protected void
readExpectedChar(char
the char value that is expected.
ec
)

Read one char and throw an exception if it is not the expected value.

protected final void
readExpectedString(final char[]
pattern to be skipped
expectedString
,
boolean
if set to true spaces before and after the string will be skipped
skipSpaces
)

Reads given pattern from source.

protected int

Returns:

the generation number being read.
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. : bigger than GENERATION_NUMBER_THRESHOLD)

protected int

Returns:

The integer that was read from the stream.
readInt
()

This will read an integer from the stream.

protected String

Returns:

The characters between the current position and the end of the line.
readLine
()

This will read bytes until the first end of line marker occurs.

protected long

Returns:

The long that was read from the stream.
readLong
()

This will read an long from the stream.

protected long

Returns:

the object number being read.
readObjectNumber
()

This will read a long from the Stream and throw an IOException if the long value is negative or has more than 10 digits (i.e. : bigger than OBJECT_NUMBER_THRESHOLD)

protected String

Returns:

The string that was read from the stream, never null.
readString
()

This will read the next string from the stream.

protected String

Returns:

The string that was read from the stream of length 0 to length.
readString
(int
The length to stop reading at.
length
)

This will read the next string from the stream up to a certain length.

protected final StringBuilder

Returns:

the token to parse as integer or long by the calling method.
readStringNumber
()

This method is used to read a token by the readInt() and the readLong() method.

private boolean

Returns:

true if the end of the object or the file has been found, false if not, i.e. that the caller can continue to parse the dictionary at the current position.
readUntilEndOfCOSDictionary
()

Keep reading until the end of the dictionary object or the file has been hit, or until a '/' has been found.

protected void
skipSpaces()

This will skip all spaces and comments that are present.

protected void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

Aback to summary
protected static final int A
ALTERNATIVE_CHARSETback to summary
private static final Charset ALTERNATIVE_CHARSET
ASCII_CRback to summary
protected static final byte ASCII_CR

ASCII code for carriage return.

ASCII_LFback to summary
protected static final byte ASCII_LF

ASCII code for line feed.

ASCII_NINEback to summary
private static final byte ASCII_NINE
ASCII_SPACEback to summary
private static final byte ASCII_SPACE
ASCII_ZEROback to summary
private static final byte ASCII_ZERO
Bback to summary
protected static final int B
Dback to summary
protected static final int D
DEFback to summary
public static final String DEF

This is a string constant that will be used for comparisons.

documentback to summary
protected COSDocument document

This is the document that will be parsed.

Eback to summary
protected static final int E
ENDOBJ_STRINGback to summary
protected static final String ENDOBJ_STRING

This is a string constant that will be used for comparisons.

ENDSTREAM_STRINGback to summary
protected static final String ENDSTREAM_STRING

This is a string constant that will be used for comparisons.

FALSEback to summary
private static final char[] FALSE

This is a string constant that will be used for comparisons.

GENERATION_NUMBER_THRESHOLDback to summary
private static final long GENERATION_NUMBER_THRESHOLD
Jback to summary
protected static final int J
keyCacheback to summary
private final Map<Long, COSObjectKey> keyCache
LOGback to summary
private static final Log LOG

Log instance.

Mback to summary
protected static final int M
MAX_LENGTH_LONGback to summary
pack-priv static final int MAX_LENGTH_LONG
Nback to summary
protected static final int N
NULLback to summary
private static final char[] NULL

This is a string constant that will be used for comparisons.

Oback to summary
protected static final int O
OBJECT_NUMBER_THRESHOLDback to summary
private static final long OBJECT_NUMBER_THRESHOLD
Rback to summary
protected static final int R
Sback to summary
protected static final int S
sourceback to summary
protected final RandomAccessRead source

This is the stream that will be read from.

STREAM_STRINGback to summary
protected static final String STREAM_STRING

This is a string constant that will be used for comparisons.

Tback to summary
protected static final int T
TRUEback to summary
private static final char[] TRUE

This is a string constant that will be used for comparisons.

utf8Decoderback to summary
private final CharsetDecoder utf8Decoder

Constructor Detail

BaseParserback to summary
pack-priv BaseParser(RandomAccessRead pdfSource)

Default constructor.

Method Detail

checkForEndOfStringback to summary
private int checkForEndOfString(final int bracesParameter) throws IOException

This is really a bug in the Document creators code, but it caused a crash in PDFBox, the first bug was in this format: /Title ( (5) /Creator which was patched in 1 place. However it missed the case where the number of opening and closing parenthesis isn't balanced The second bug was in this format /Title (c:\) /Producer

Parameters
bracesParameter:int

the number of braces currently open.

Returns:int

the corrected value of the brace counter

decodeBufferback to summary
private String decodeBuffer(ByteArrayOutputStream buffer) throws UnsupportedEncodingException

Tries to decode the buffer cotent to an UTF-8 String. If that fails, tries the alternative Encoding.

Parameters
buffer:ByteArrayOutputStream

the ByteArrayOutputStream containing the bytes to decode

Returns:String

the decoded String

getObjectFromPoolback to summary
private COSBase getObjectFromPool(COSObjectKey key) throws IOException
getObjectKeyback to summary
protected COSObjectKey getObjectKey(long num, int gen)

Returns the object key for the given combination of object and generation number. The object key from the cross reference table/stream will be reused if available. Otherwise a newly created object will be returned.

Parameters
num:long

the given object number

gen:int

the given generation number

Returns:COSObjectKey

the COS object key

isClosingback to summary
protected boolean isClosing() throws IOException

This will tell if the next character is a closing brace( close of PDF array ).

Returns:boolean

true if the next byte is ']', false otherwise.

Exceptions
IOException:
If an IO error occurs.
isClosingback to summary
protected boolean isClosing(int c)

This will tell if the next character is a closing brace( close of PDF array ).

Parameters
c:int

The character to check against end of line

Returns:boolean

true if the next byte is ']', false otherwise.

isCRback to summary
private boolean isCR(int c)
isDigitback to summary
protected boolean isDigit() throws IOException

This will tell if the next byte is a digit or not.

Returns:boolean

true if the next byte in the stream is a digit.

Exceptions
IOException:
If there is an error reading from the stream.
isDigitback to summary
protected static boolean isDigit(int c)

This will tell if the given value is a digit or not.

Parameters
c:int

The character to be checked

Returns:boolean

true if the next byte in the stream is a digit.

isEndOfNameback to summary
protected boolean isEndOfName(int ch)

Determine if a character terminates a PDF name.

Parameters
ch:int

The character

Returns:boolean

true if the character terminates a PDF name, otherwise false.

isEOFback to summary
protected boolean isEOF() throws IOException

This will tell if the end of the data is reached.

Returns:boolean

true if the end of the data is reached.

Exceptions
IOException:
If there is an error reading from the stream.
isEOLback to summary
protected boolean isEOL() throws IOException

This will tell if the next byte to be read is an end of line byte.

Returns:boolean

true if the next byte is 0x0A or 0x0D.

Exceptions
IOException:
If there is an error reading from the stream.
isEOLback to summary
protected boolean isEOL(int c)

This will tell if the next byte to be read is an end of line byte.

Parameters
c:int

The character to check against end of line

Returns:boolean

true if the next byte is 0x0A or 0x0D.

isHexDigitback to summary
private static boolean isHexDigit(char ch)
isLFback to summary
private boolean isLF(int c)
isSpaceback to summary
protected boolean isSpace() throws IOException

This will tell if the next byte is a space or not.

Returns:boolean

true if the next byte in the stream is a space character.

Exceptions
IOException:
If there is an error reading from the stream.
isSpaceback to summary
protected boolean isSpace(int c)

This will tell if the given value is a space or not.

Parameters
c:int

The character to check against space

Returns:boolean

true if the next byte in the stream is a space character.

isWhitespaceback to summary
protected boolean isWhitespace() throws IOException

This will tell if the next byte is whitespace or not.

Returns:boolean

true if the next byte in the stream is a whitespace character.

Exceptions
IOException:
If there is an error reading from the stream.
isWhitespaceback to summary
protected static boolean isWhitespace(int c)

This will tell if a character is whitespace or not. These values are specified in table 1 (page 12) of ISO 32000-1:2008.

Parameters
c:int

The character to check against whitespace

Returns:boolean

true if the character is a whitespace character.

parseCOSArrayback to summary
protected COSArray parseCOSArray() throws IOException

This will parse a PDF array object.

Returns:COSArray

The parsed PDF array.

Exceptions
IOException:
If there is an error parsing the stream.
parseCOSDictionaryback to summary
protected COSDictionary parseCOSDictionary(boolean isDirect) throws IOException

This will parse a PDF dictionary.

Parameters
isDirect:boolean

indicates whether the dictionary to be read is a direct object

Returns:COSDictionary

The parsed dictionary, never null.

Exceptions
IOException:
If there is an error reading the stream.
parseCOSDictionaryNameValuePairback to summary
private boolean parseCOSDictionaryNameValuePair(COSDictionary obj) throws IOException
parseCOSDictionaryValueback to summary
private COSBase parseCOSDictionaryValue() throws IOException

This will parse a PDF dictionary value.

Returns:COSBase

The parsed Dictionary object.

Exceptions
IOException:
If there is an error parsing the dictionary object.
parseCOSHexStringback to summary
private COSString parseCOSHexString() throws IOException

This will parse a PDF HEX string with fail fast semantic meaning that we stop if a not allowed character is found. This is necessary in order to detect malformed input and be able to skip to next object start. We assume starting '<' was already read.

Returns:COSString

The parsed PDF string.

Exceptions
IOException:
If there is an error reading from the stream.
parseCOSNameback to summary
protected COSName parseCOSName() throws IOException

This will parse a PDF name from the stream.

Returns:COSName

The parsed PDF name.

Exceptions
IOException:
If there is an error reading from the stream.
parseCOSNumberback to summary
private COSNumber parseCOSNumber() throws IOException
parseCOSStringback to summary
protected COSString parseCOSString() throws IOException

This will parse a PDF string.

Returns:COSString

The parsed PDF string.

Exceptions
IOException:
If there is an error reading from the stream.
parseDirObjectback to summary
protected COSBase parseDirObject() throws IOException

This will parse a directory object from the stream.

Returns:COSBase

The parsed object.

Exceptions
IOException:
If there is an error during parsing.
readExpectedCharback to summary
protected void readExpectedChar(char ec) throws IOException

Read one char and throw an exception if it is not the expected value.

Parameters
ec:char

the char value that is expected.

Exceptions
IOException:
if the read char is not the expected value or if an I/O error occurs.
readExpectedStringback to summary
protected final void readExpectedString(final char[] expectedString, boolean skipSpaces) throws IOException

Reads given pattern from source. Skipping whitespace at start and end if wanted.

Parameters
expectedString:char[]

pattern to be skipped

skipSpaces:boolean

if set to true spaces before and after the string will be skipped

Exceptions
IOException:
if pattern could not be read
readGenerationNumberback to summary
protected int readGenerationNumber() throws 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:int

the generation number being read.

Exceptions
IOException:
if an I/O error occurs
readIntback to summary
protected int readInt() throws IOException

This will read an integer from the stream.

Returns:int

The integer that was read from the stream.

Exceptions
IOException:
If there is an error reading from the stream.
readLineback to summary
protected String readLine() throws IOException

This will read bytes until the first end of line marker occurs.

Note

The EOL marker may consists of 1 (CR or LF) or 2 (CR and CL) bytes which is an important detail if one wants to unread the line.

Returns:String

The characters between the current position and the end of the line.

Exceptions
IOException:
If there is an error reading from the stream.
readLongback to summary
protected long readLong() throws IOException

This will read an long from the stream.

Returns:long

The long that was read from the stream.

Exceptions
IOException:
If there is an error reading from the stream.
readObjectNumberback to summary
protected long readObjectNumber() throws IOException

This will read a long from the Stream and throw an IOException if the long value is negative or has more than 10 digits (i.e. : bigger than OBJECT_NUMBER_THRESHOLD)

Returns:long

the object number being read.

Exceptions
IOException:
if an I/O error occurs
readStringback to summary
protected String readString() throws IOException

This will read the next string from the stream.

Returns:String

The string that was read from the stream, never null.

Exceptions
IOException:
If there is an error reading from the stream.
readStringback to summary
protected String readString(int length) throws IOException

This will read the next string from the stream up to a certain length.

Parameters
length:int

The length to stop reading at.

Returns:String

The string that was read from the stream of length 0 to length.

Exceptions
IOException:
If there is an error reading from the stream.
readStringNumberback to summary
protected final StringBuilder readStringNumber() throws IOException

This method is used to read a token by the readInt() and the readLong() method. Valid delimiters are any non digit values.

Returns:StringBuilder

the token to parse as integer or long by the calling method.

Exceptions
IOException:
throws by the source methods.
readUntilEndOfCOSDictionaryback to summary
private boolean readUntilEndOfCOSDictionary() throws IOException

Keep reading until the end of the dictionary object or the file has been hit, or until a '/' has been found.

Returns:boolean

true if the end of the object or the file has been found, false if not, i.e. that the caller can continue to parse the dictionary at the current position.

Exceptions
IOException:
if there is a reading error.
skipSpacesback to summary
protected void skipSpaces() throws IOException

This will skip all spaces and comments that are present.

Exceptions
IOException:
If there is an error reading from the stream.
skipWhiteSpacesback to summary
protected void skipWhiteSpaces() throws IOException