return new PdfReference(
(Reference)token,
file
);
case Literal:
return new PdfTextString(
Encoding.encode((String)token)
);
case DictionaryBegin:
PdfDictionary dictionary = new PdfDictionary();
while(true)
{
// Key.
moveNext(); if(tokenType == TokenTypeEnum.DictionaryEnd) break;
PdfName key = (PdfName)parsePdfObject();
// Value.
moveNext();
PdfDirectObject value = (PdfDirectObject)parsePdfObject();
// Add the current entry to the dictionary!
dictionary.put(key,value);
}
int oldOffset = (int)stream.getPosition();
moveNext();
// Is this dictionary the header of a stream object [PDF:1.6:3.2.7]?
if((tokenType == TokenTypeEnum.Keyword)
&& token.equals(Keyword.BeginStream)) // Stream.
{
// Keep track of current position!
long position = stream.getPosition();
// Get the stream length!
/*
NOTE: Indirect reference resolution is an outbound call (stream pointer hazard!),
so we need to recover our current position after it returns.
*/
int length = ((PdfInteger)File.resolve(dictionary.get(PdfName.Length))).getRawValue();
// Move to the stream data beginning!
stream.seek(position); skipEOL();
// Copy the stream data to the instance!
byte[] data = new byte[length];
try
{stream.read(data);}
catch(EOFException e)
{throw new FileFormatException("Unexpected EOF (malformed stream object).",e,stream.getPosition());}
moveNext(); // Postcondition (last token should be 'endstream' keyword).
Object streamType = dictionary.get(PdfName.Type);
if(PdfName.ObjStm.equals(streamType)) // Object stream [PDF:1.6:3.4.6].
return new ObjectStream(
dictionary,
new Buffer(data),
file
);
else if(PdfName.XRef.equals(streamType)) // Cross-reference stream [PDF:1.6:3.4.7].
return new XRefStream(
dictionary,
new Buffer(data),
file
);
else // Generic stream.
return new PdfStream(
dictionary,
new Buffer(data)
);
}
else // Stand-alone dictionary.
{
stream.seek(oldOffset); // Restores postcondition (last token should be the dictionary end).
return dictionary;
}
case ArrayBegin:
PdfArray array = new PdfArray();
while(true)
{
// Value.
moveNext(); if(tokenType == TokenTypeEnum.ArrayEnd) break;
// Add the current item to the array!
array.add((PdfDirectObject)parsePdfObject());
}
return array;
case Real:
return new PdfReal((Float)token);
case Boolean:
return PdfBoolean.get((Boolean)token);
case Date:
return new PdfDate((Date)token);
case Hex:
return new PdfTextString(
(String)token,
PdfString.SerializationModeEnum.Hex
);
case Null:
return null;