public void parse() throws IOException {
try
{
COSArray xrefFormat = (COSArray)stream.getDictionaryObject(COSName.W);
COSArray indexArray = (COSArray)stream.getDictionaryObject(COSName.INDEX);
/*
* If Index doesn't exist, we will use the default values.
*/
if(indexArray == null)
{
indexArray = new COSArray();
indexArray.add(COSInteger.ZERO);
indexArray.add(stream.getDictionaryObject(COSName.SIZE));
}
ArrayList objNums = new ArrayList();
/*
* Populates objNums with all object numbers available
*/
Iterator indexIter = indexArray.iterator();
while(indexIter.hasNext())
{
int objID = ((COSInteger)indexIter.next()).intValue();
int size = ((COSInteger)indexIter.next()).intValue();
for(int i = 0; i < size; i++)
{
objNums.add(new Integer(objID + i));
}
}
Iterator objIter = objNums.iterator();
/*
* Calculating the size of the line in bytes
*/
int w0 = xrefFormat.getInt(0);
int w1 = xrefFormat.getInt(1);
int w2 = xrefFormat.getInt(2);
int lineSize = w0 + w1 + w2;
while(pdfSource.available() > 0 && objIter.hasNext())
{
byte[] currLine = new byte[lineSize];
pdfSource.read(currLine);
int type = 0;
/*
* Grabs the number of bytes specified for the first column in
* the W array and stores it.
*/
for(int i = 0; i < w0; i++)
{
type += (currLine[i] & 0x00ff) < < ((w0 - i - 1)* 8);
}
//Need to remember the current objID
Integer objID = (Integer)objIter.next();
/*
* 3 different types of entries.
*/
switch(type)
{
case 0:
/*
* Skipping free objects
*/
break;
case 1:
int offset = 0;
for(int i = 0; i < w1; i++)
{
offset += (currLine[i + w0] & 0x00ff) < < ((w1 - i - 1) * 8);
}
int genNum = 0;
for(int i = 0; i < w2; i++)
{
genNum += (currLine[i + w0 + w1] & 0x00ff) < < ((w2 - i - 1) * 8);
}
COSObjectKey objKey = new COSObjectKey(objID.intValue(), genNum);
document.setXRef(objKey, offset);
break;
case 2:
/*
* These objects are handled by the dereferenceObjects() method
* since they're only pointing to object numbers
*/
break;
default:
break;
}
}
}
finally
{
pdfSource.close();
}
}
Parses through the unfiltered stream and populates the xrefTable HashMap. |