iText 9.8.0 API
iText.IO.Source.PdfTokenizer Class Reference

Tokenizes PDF syntax from a random-access byte source. More...

Inheritance diagram for iText.IO.Source.PdfTokenizer:

Public Types

enum   TokenType {
  Number, String, Name, Comment,
  StartArray, EndArray, StartDic, EndDic,
  Ref, Obj, EndObj, Other,
  EndOfFile
}
  Token types recognized by this tokenizer. More...
 

Public Member Functions

  PdfTokenizer (RandomAccessFileOrArray file)
  Creates a PdfTokenizer for the specified RandomAccessFileOrArray. More...
 
virtual void  Seek (long pos)
  Sets the position from which the next byte is read. More...
 
virtual void  ReadFully (byte[] bytes)
  Reads enough bytes to fill a destination array. More...
 
virtual long  GetPosition ()
  Gets the current source position. More...
 
virtual void  Close ()
  Closes the underlying source when closing is enabled. More...
 
virtual long  Length ()
  Gets the length of the underlying source. More...
 
virtual int  Read ()
  Reads one byte and advances the source position. More...
 
virtual int  Peek ()
  Gets the next byte of pdf source without moving source position. More...
 
virtual int  Peek (byte[] buffer)
  Gets the next buffer.length bytes of pdf source without moving source position. More...
 
virtual String  ReadString (int size)
  Reads up to a requested number of bytes as character values. More...
 
virtual PdfTokenizer.TokenType  GetTokenType ()
  Gets the type of the most recently parsed token. More...
 
virtual byte[]  GetByteContent ()
  Copies the bytes of the most recently parsed token. More...
 
virtual String  GetStringValue ()
  Converts the current token bytes to a string using the platform default charset. More...
 
virtual byte[]  GetDecodedStringContent ()
  Decodes the current PDF string token. More...
 
virtual bool  TokenValueEqualsTo (byte[] cmp)
  Tests whether the current token bytes equal a candidate byte sequence. More...
 
virtual int  GetObjNr ()
  Gets the object number parsed from the current indirect reference. More...
 
virtual int  GetGenNr ()
  Gets the generation number parsed from the current indirect reference. More...
 
virtual void  BackOnePosition (int ch)
  Pushes a read byte back so it becomes the next byte read. More...
 
virtual int  GetHeaderOffset ()
  Finds a PDF or FDF header in the first kilobyte of the source. More...
 
virtual String  CheckPdfHeader ()
  Validates a PDF header at offset zero and returns its version text. More...
 
virtual void  CheckFdfHeader ()
  Validates that an FDF header begins at offset zero. More...
 
virtual long  GetStartxref ()
  Locates the final startxref marker near the end of the source. More...
 
virtual long  GetNextEof ()
  Gets next %EOF marker in current PDF file. More...
 
virtual void  NextValidToken ()
  Reads the next non-comment token and recognizes indirect references and object declarations. More...
 
virtual bool  NextToken ()
  Parses the next PDF token into this tokenizer's mutable token state. More...
 
virtual long  GetLongValue ()
  Parses the current token value as a long. More...
 
virtual int  GetIntValue ()
  Parses the current token value as an int. More...
 
virtual bool  IsHexString ()
  Tests whether the current string token uses hexadecimal notation. More...
 
virtual bool  IsCloseStream ()
  Tests whether Close() closes the underlying source. More...
 
virtual void  SetCloseStream (bool closeStream)
  Configures whether Close() closes the underlying source. More...
 
virtual RandomAccessFileOrArray  GetSafeFile ()
  Creates an independent view of the underlying source. More...
 
virtual void  ThrowError (String error, params Object[] messageParams)
  Helper method to handle content errors. More...
 
virtual bool  ReadLineSegment (ByteBuffer buffer)
  Reads data into the provided byte[]. More...
 
virtual bool  ReadLineSegment (ByteBuffer buffer, bool isNullWhitespace)
  Reads data into the provided byte[]. More...
 

Static Public Member Functions

static byte[]  DecodeStringContent (byte[] content, bool hexWriting)
  Resolve escape symbols or hexadecimal symbols. More...
 
static bool  IsWhitespace (int ch)
  Is a certain character a whitespace? Currently checks on the following: '0', '9', '10', '12', '13', '32'. More...
 
static bool  CheckTrailer (ByteBuffer line)
  Checks whether line equals to 'trailer'. More...
 
static int[]  CheckObjectStart (PdfTokenizer lineTokenizer)
  Check whether line starts with object declaration. More...
 

Static Public Attributes

static readonly byte[]  Obj = ByteUtils.GetIsoBytes("obj")
 
static readonly byte[]  R = ByteUtils.GetIsoBytes("R")
 
static readonly byte[]  Xref = ByteUtils.GetIsoBytes("xref")
 
static readonly byte[]  Startxref = ByteUtils.GetIsoBytes("startxref")
 
static readonly byte[]  Stream = ByteUtils.GetIsoBytes("stream")
 
static readonly byte[]  Trailer = ByteUtils.GetIsoBytes("trailer")
 
static readonly byte[]  N = ByteUtils.GetIsoBytes("n")
 
static readonly byte[]  F = ByteUtils.GetIsoBytes("f")
 
static readonly byte[]  Null = ByteUtils.GetIsoBytes("null")
 
static readonly byte[]  True = ByteUtils.GetIsoBytes("true")
 
static readonly byte[]  False = ByteUtils.GetIsoBytes("false")
 

Static Package Functions

static byte[]  DecodeStringContent (byte[] content, int from, int to, bool hexWriting)
  Resolve escape symbols or hexadecimal symbols. More...
 
static bool  IsWhitespace (int ch, bool isWhitespace)
  Checks whether a character is a whitespace. More...
 
static bool  IsDelimiter (int ch)
  Tests whether a character is a PDF delimiter. More...
 
static bool  IsDelimiterWhitespace (int ch)
  Tests whether a character is a PDF delimiter or whitespace character. More...
 

Package Attributes

PdfTokenizer.TokenType  type
  The type of the most recently parsed token. More...
 
int  reference
  The object number parsed for the current indirect reference. More...
 
int  generation
  The generation number parsed for the current indirect reference. More...
 
bool  hexString
  Whether the current string token uses hexadecimal notation. More...
 
ByteBuffer  outBuf
  The mutable bytes of the most recently parsed token. More...
 

Detailed Description

Tokenizes PDF syntax from a random-access byte source.

Tokenizes PDF syntax from a random-access byte source.

Instances maintain a mutable stream position and token state and are not thread-safe.

Member Enumeration Documentation

◆ TokenType

Token types recognized by this tokenizer.

Constructor & Destructor Documentation

◆ PdfTokenizer()

iText.IO.Source.PdfTokenizer.PdfTokenizer ( RandomAccessFileOrArray  file )
inline

Creates a PdfTokenizer for the specified RandomAccessFileOrArray.

Creates a PdfTokenizer for the specified RandomAccessFileOrArray. The beginning of the file is read to determine the location of the header, and the data source is adjusted as necessary to account for any junk that occurs in the byte source before the header

Parameters
file the source

Member Function Documentation

◆ BackOnePosition()

virtual void iText.IO.Source.PdfTokenizer.BackOnePosition ( int  ch )
inlinevirtual

Pushes a read byte back so it becomes the next byte read.

Parameters
ch the byte value to push back; -1 is ignored

◆ CheckFdfHeader()

virtual void iText.IO.Source.PdfTokenizer.CheckFdfHeader ( )
inlinevirtual

Validates that an FDF header begins at offset zero.

◆ CheckObjectStart()

static int [] iText.IO.Source.PdfTokenizer.CheckObjectStart ( PdfTokenizer  lineTokenizer )
inlinestatic

Check whether line starts with object declaration.

Parameters
lineTokenizer tokenizer, built by single line.
Returns
object number and generation if check is successful, otherwise - null.

◆ CheckPdfHeader()

virtual String iText.IO.Source.PdfTokenizer.CheckPdfHeader ( )
inlinevirtual

Validates a PDF header at offset zero and returns its version text.

Returns
the header text without its percent sign

◆ CheckTrailer()

static bool iText.IO.Source.PdfTokenizer.CheckTrailer ( ByteBuffer  line )
inlinestatic

Checks whether line equals to 'trailer'.

Parameters
line for check
Returns
true, if line is equals to 'trailer', otherwise false

◆ Close()

virtual void iText.IO.Source.PdfTokenizer.Close ( )
inlinevirtual

Closes the underlying source when closing is enabled.

◆ DecodeStringContent() [1/2]

static byte [] iText.IO.Source.PdfTokenizer.DecodeStringContent ( byte[]  content,
bool  hexWriting 
)
inlinestatic

Resolve escape symbols or hexadecimal symbols.

Resolve escape symbols or hexadecimal symbols.
NOTE Due to PdfReference 1.7 part 3.2.3 String value contain ASCII characters, so we can convert it directly to byte array.

Parameters
content string bytes to be decoded
hexWriting true if given string is hex-encoded, e.g. '<69546578…>'. False otherwise, e.g. '((iText( some version)…)'
Returns
byte[] for decrypting or for creating System.String.

◆ DecodeStringContent() [2/2]

static byte [] iText.IO.Source.PdfTokenizer.DecodeStringContent ( byte[]  content,
int  from,
int  to,
bool  hexWriting 
)
inlinestaticpackage

Resolve escape symbols or hexadecimal symbols.

Resolve escape symbols or hexadecimal symbols.

NOTE Due to PdfReference 1.7 part 3.2.3 String value contain ASCII characters, so we can convert it directly to byte array.

Parameters
content string bytes to be decoded
from given start index
to given end index
hexWriting true if given string is hex-encoded, e.g. '<69546578…>'. False otherwise, e.g. '((iText( some version)…)'
Returns
byte[] for decrypting or for creating System.String.

◆ GetByteContent()

virtual byte [] iText.IO.Source.PdfTokenizer.GetByteContent ( )
inlinevirtual

Copies the bytes of the most recently parsed token.

Returns
a new array containing the current token bytes

◆ GetDecodedStringContent()

virtual byte [] iText.IO.Source.PdfTokenizer.GetDecodedStringContent ( )
inlinevirtual

Decodes the current PDF string token.

Returns
a new array containing decoded literal or hexadecimal string bytes

◆ GetGenNr()

virtual int iText.IO.Source.PdfTokenizer.GetGenNr ( )
inlinevirtual

Gets the generation number parsed from the current indirect reference.

Returns
the parsed generation number

◆ GetHeaderOffset()

virtual int iText.IO.Source.PdfTokenizer.GetHeaderOffset ( )
inlinevirtual

Finds a PDF or FDF header in the first kilobyte of the source.

Returns
the zero-based byte offset of the header

◆ GetIntValue()

virtual int iText.IO.Source.PdfTokenizer.GetIntValue ( )
inlinevirtual

Parses the current token value as an int.

Returns
the parsed numeric value

◆ GetLongValue()

virtual long iText.IO.Source.PdfTokenizer.GetLongValue ( )
inlinevirtual

Parses the current token value as a long.

Returns
the parsed numeric value

◆ GetNextEof()

virtual long iText.IO.Source.PdfTokenizer.GetNextEof ( )
inlinevirtual

Gets next %EOF marker in current PDF file.

Returns
next %EOF marker position

◆ GetObjNr()

virtual int iText.IO.Source.PdfTokenizer.GetObjNr ( )
inlinevirtual

Gets the object number parsed from the current indirect reference.

Returns
the parsed object number

◆ GetPosition()

virtual long iText.IO.Source.PdfTokenizer.GetPosition ( )
inlinevirtual

Gets the current source position.

Returns
the absolute offset of the next byte to read

◆ GetSafeFile()

virtual RandomAccessFileOrArray iText.IO.Source.PdfTokenizer.GetSafeFile ( )
inlinevirtual

Creates an independent view of the underlying source.

Returns
a view with its own position; closing it does not close this tokenizer's source

◆ GetStartxref()

virtual long iText.IO.Source.PdfTokenizer.GetStartxref ( )
inlinevirtual

Locates the final startxref marker near the end of the source.

Returns
the absolute byte offset of the marker

◆ GetStringValue()

virtual String iText.IO.Source.PdfTokenizer.GetStringValue ( )
inlinevirtual

Converts the current token bytes to a string using the platform default charset.

Returns
the current token value as a string

◆ GetTokenType()

virtual PdfTokenizer.TokenType iText.IO.Source.PdfTokenizer.GetTokenType ( )
inlinevirtual

Gets the type of the most recently parsed token.

Returns
the current token type

◆ IsCloseStream()

virtual bool iText.IO.Source.PdfTokenizer.IsCloseStream ( )
inlinevirtual

Tests whether Close() closes the underlying source.

Returns

true if this tokenizer owns closing the source

◆ IsDelimiter()

static bool iText.IO.Source.PdfTokenizer.IsDelimiter ( int  ch )
inlinestaticpackage

Tests whether a character is a PDF delimiter.

Parameters
ch the character value to test
Returns

true when ch is a PDF delimiter

◆ IsDelimiterWhitespace()

static bool iText.IO.Source.PdfTokenizer.IsDelimiterWhitespace ( int  ch )
inlinestaticpackage

Tests whether a character is a PDF delimiter or whitespace character.

Parameters
ch the character value to test
Returns

true when ch is a delimiter or configured whitespace value

◆ IsHexString()

virtual bool iText.IO.Source.PdfTokenizer.IsHexString ( )
inlinevirtual

Tests whether the current string token uses hexadecimal notation.

Returns

true for a hexadecimal string token

◆ IsWhitespace() [1/2]

static bool iText.IO.Source.PdfTokenizer.IsWhitespace ( int  ch )
inlinestatic

Is a certain character a whitespace? Currently checks on the following: '0', '9', '10', '12', '13', '32'.

Is a certain character a whitespace? Currently checks on the following: '0', '9', '10', '12', '13', '32'.
The same as calling isWhiteSpace(ch, true).

Parameters
ch int
Returns
boolean

◆ IsWhitespace() [2/2]

static bool iText.IO.Source.PdfTokenizer.IsWhitespace ( int  ch,
bool  isWhitespace 
)
inlinestaticpackage

Checks whether a character is a whitespace.

Checks whether a character is a whitespace. Currently checks on the following: '0', '9', '10', '12', '13', '32'.

Parameters
ch int
isWhitespace boolean
Returns
boolean

◆ Length()

virtual long iText.IO.Source.PdfTokenizer.Length ( )
inlinevirtual

Gets the length of the underlying source.

Returns
the number of readable bytes

◆ NextToken()

virtual bool iText.IO.Source.PdfTokenizer.NextToken ( )
inlinevirtual

Parses the next PDF token into this tokenizer's mutable token state.

Returns

true when a token was read, or false at EOF

◆ NextValidToken()

virtual void iText.IO.Source.PdfTokenizer.NextValidToken ( )
inlinevirtual

Reads the next non-comment token and recognizes indirect references and object declarations.

Reads the next non-comment token and recognizes indirect references and object declarations.

The source position and current token state are advanced to the recognized token.

◆ Peek() [1/2]

virtual int iText.IO.Source.PdfTokenizer.Peek ( )
inlinevirtual

Gets the next byte of pdf source without moving source position.

Returns
the byte, or -1 if EOF is reached

◆ Peek() [2/2]

virtual int iText.IO.Source.PdfTokenizer.Peek ( byte[]  buffer )
inlinevirtual

Gets the next buffer.length bytes of pdf source without moving source position.

Parameters
buffer buffer to store read bytes
Returns
the number of read bytes. If it is less than buffer.length it means EOF has been reached

◆ Read()

virtual int iText.IO.Source.PdfTokenizer.Read ( )
inlinevirtual

Reads one byte and advances the source position.

Returns
the unsigned byte value, or -1 at EOF

◆ ReadFully()

virtual void iText.IO.Source.PdfTokenizer.ReadFully ( byte[]  bytes )
inlinevirtual

Reads enough bytes to fill a destination array.

Parameters
bytes the destination array

◆ ReadLineSegment() [1/2]

virtual bool iText.IO.Source.PdfTokenizer.ReadLineSegment ( ByteBuffer  buffer )
inlinevirtual

Reads data into the provided byte[].

Reads data into the provided byte[]. Checks on leading whitespace. See isWhiteSpace(int) or isWhiteSpace(int, boolean) for a list of whitespace characters.
The same as calling readLineSegment(input, true).

Parameters
buffer a ByteBuffer to which the result of reading will be saved
Returns
true, if something was read or if the end of the input stream is not reached

◆ ReadLineSegment() [2/2]

virtual bool iText.IO.Source.PdfTokenizer.ReadLineSegment ( ByteBuffer  buffer,
bool  isNullWhitespace 
)
inlinevirtual

Reads data into the provided byte[].

Reads data into the provided byte[]. Checks on leading whitespace. See isWhiteSpace(int) or isWhiteSpace(int, boolean) for a list of whitespace characters.

Parameters
buffer a ByteBuffer to which the result of reading will be saved
isNullWhitespace boolean to indicate whether '0' is whitespace or not. If in doubt, use true or overloaded method readLineSegment(input)
Returns
true, if something was read or if the end of the input stream is not reached

◆ ReadString()

virtual String iText.IO.Source.PdfTokenizer.ReadString ( int  size )
inlinevirtual

Reads up to a requested number of bytes as character values.

Parameters
size the maximum number of bytes to read
Returns
a string containing the bytes read before EOF

◆ Seek()

virtual void iText.IO.Source.PdfTokenizer.Seek ( long  pos )
inlinevirtual

Sets the position from which the next byte is read.

Parameters
pos the absolute byte offset in the underlying source

◆ SetCloseStream()

virtual void iText.IO.Source.PdfTokenizer.SetCloseStream ( bool  closeStream )
inlinevirtual

Configures whether Close() closes the underlying source.

Parameters
closeStream

true to close the source, false to leave it open

◆ ThrowError()

virtual void iText.IO.Source.PdfTokenizer.ThrowError ( String  error,
params Object[]  messageParams 
)
inlinevirtual

Helper method to handle content errors.

Helper method to handle content errors. Add file position to PdfRuntimeException.

Parameters
error message.
messageParams error params.

◆ TokenValueEqualsTo()

virtual bool iText.IO.Source.PdfTokenizer.TokenValueEqualsTo ( byte[]  cmp )
inlinevirtual

Tests whether the current token bytes equal a candidate byte sequence.

Parameters
cmp the bytes to compare; null never matches
Returns

true if cmp equals the current token bytes

Member Data Documentation

◆ generation

int iText.IO.Source.PdfTokenizer.generation
package

The generation number parsed for the current indirect reference.

◆ hexString

bool iText.IO.Source.PdfTokenizer.hexString
package

Whether the current string token uses hexadecimal notation.

◆ outBuf

ByteBuffer iText.IO.Source.PdfTokenizer.outBuf
package

The mutable bytes of the most recently parsed token.

◆ reference

int iText.IO.Source.PdfTokenizer.reference
package

The object number parsed for the current indirect reference.

◆ type

PdfTokenizer.TokenType iText.IO.Source.PdfTokenizer.type
package

The type of the most recently parsed token.