org.jpedal.io.types.Offsets Maven / Gradle / Ivy
/*
* ===========================================
* Java Pdf Extraction Decoding Access Library
* ===========================================
*
* Project Info: http://www.idrsolutions.com
* Help section for developers at http://www.idrsolutions.com/support/
*
* (C) Copyright 1997-2016 IDRsolutions and Contributors.
*
* This file is part of JPedal/JPDF2HTML5
*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---------------
* Offsets.java
* ---------------
*/
package org.jpedal.io.types;
import java.io.IOException;
import org.jpedal.io.RandomAccessBuffer;
import org.jpedal.utils.LogWriter;
import org.jpedal.utils.NumberUtils;
import org.jpedal.utils.Sorts;
import org.jpedal.utils.repositories.Vector_Int;
import org.jpedal.utils.repositories.Vector_boolean;
/**
* byte positions of objects in PDF file
*/
public class Offsets extends Vector_Int{
private boolean refTableInvalid;
/**flag to show if compressed*/
private Vector_boolean isCompressed=new Vector_boolean(2000);
/**generation of each object*/
private Vector_Int generation = new Vector_Int( 2000 );
/**location of end ref*/
private Vector_Int xref=new Vector_Int(100);
public Offsets(final int i) {
super(i);
}
public void addXref(final int pointer) {
xref.addElement(pointer);
}
/**
* precalculate sizes for each object
*/
public int[] calculateObjectLength(final int eof) {
if(refTableInvalid) {
return null;
}
//add eol to refs as catchall
xref.addElement(eof);
final int[] xrefs=xref.get();
//get order list of refs
final int xrefCount=xrefs.length;
int[] xrefID=new int[xrefCount];
for(int i=0;ieof){ //allow for invalid ref outside file see 27419
break;
}
int objLength = end - start - 1;
//adjust for any xref
if (xrefs[xrefID[j]] < end) {
objLength = xrefs[xrefID[j]] - start - 1;
while (xrefs[xrefID[j]] < end + 1) {
j++;
}
}
ObjLengthTable[id[i]] = objLength;
start = end;
while (xrefs[xrefID[j]] < start + 1) {
j++;
}
i++;
}
//special case - last object
ObjLengthTable[id[i]]=xrefs[xrefID[j]]-start-1;
//System.out.println("*"+id[i]+" "+start+" "+xref+" "+eof);
return ObjLengthTable;
}
public void dispose() {
xref=null;
generation=null;
isCompressed=null;
}
/**
* read table of values
*/
public int readXRefs(int current, final byte[] Bytes, final int endTable, int i, final long eof, final RandomAccessBuffer pdf_datafile){
char flag;
int id,tokenCount,generation,lineLen,startLine,endLine;
boolean skipNext=false;
boolean isFirstValue=true;
final int[] breaks=new int[5];
final int[] starts=new int[5];
// loop to read all references
while (i < endTable) { //exit end at trailer
startLine=i;
endLine=-1;
/*
* read line locations
*/
//move to start of value ignoring spaces or returns
while (Bytes[i] != 10 && Bytes[i] != 13) {
//scan for %
if((endLine==-1)&&(Bytes[i]==37)) {
endLine = i - 1;
}
i++;
}
//set end if no comment
if(endLine==-1) {
endLine = i - 1;
}
//strip any spaces
while(Bytes[startLine]==32) {
startLine++;
}
//strip any spaces
while(Bytes[endLine]==32) {
endLine--;
}
i++;
/*
* decode the line
*/
tokenCount=0;
lineLen=endLine-startLine+1;
if(lineLen>0){
//decide if line is a section header or value
//first count tokens
int lastChar=1,currentChar;
for(int j=1;j eof) {
bufSize = (int) (eof - id);
}
if(bufSize>0){
final byte[] buffer = getBytes(id, bufSize,pdf_datafile);
//look for space o b j
for(int ii=4;iithis.elementAt(current_number)))
{
this.setElementAt( current_offset, current_number );
generation.setElementAt( current_generation, current_number );
isCompressed.setElementAt(isEntryCompressed,current_number);
}else{
//LogWriter.writeLog("Object "+current_number + ", generation "+
//current_generation + " already exists as"+
//existing_generation);
}
}
/**
* general routine to turn reference into id with object name
*/
public final boolean isCompressed( final int ref)
{
return isCompressed.elementAt(ref);
}
public boolean isRefTableInvalid(){
return refTableInvalid;
}
public void setRefTableInvalid(final boolean value) {
refTableInvalid=value;
}
}