use of com.tom_roush.pdfbox.cos.COSObjectKey in project PdfBox-Android by TomRoush.
the class BaseParser method parseCOSArray.
/**
* This will parse a PDF array object.
*
* @return The parsed PDF array.
*
* @throws IOException If there is an error parsing the stream.
*/
protected COSArray parseCOSArray() throws IOException {
long startPosition = seqSource.getPosition();
readExpectedChar('[');
COSArray po = new COSArray();
COSBase pbo;
skipSpaces();
int i;
while (((i = seqSource.peek()) > 0) && ((char) i != ']')) {
pbo = parseDirObject();
if (pbo instanceof COSObject) {
// We have to check if the expected values are there or not PDFBOX-385
if (po.size() > 0 && po.get(po.size() - 1) instanceof COSInteger) {
COSInteger genNumber = (COSInteger) po.remove(po.size() - 1);
if (po.size() > 0 && po.get(po.size() - 1) instanceof COSInteger) {
COSInteger number = (COSInteger) po.remove(po.size() - 1);
COSObjectKey key = new COSObjectKey(number.longValue(), genNumber.intValue());
pbo = getObjectFromPool(key);
} else {
// the object reference is somehow wrong
pbo = null;
}
} else {
pbo = null;
}
}
if (pbo != null) {
po.add(pbo);
} else {
// it could be a bad object in the array which is just skipped
Log.w("PdfBox-Android", "Corrupt object reference at offset " + seqSource.getPosition() + ", start offset: " + startPosition);
// This could also be an "endobj" or "endstream" which means we can assume that
// the array has ended.
String isThisTheEnd = readString();
seqSource.unread(isThisTheEnd.getBytes(ISO_8859_1));
if (ENDOBJ_STRING.equals(isThisTheEnd) || ENDSTREAM_STRING.equals(isThisTheEnd)) {
return po;
}
}
skipSpaces();
}
// read ']'
seqSource.read();
skipSpaces();
return po;
}
use of com.tom_roush.pdfbox.cos.COSObjectKey in project PdfBox-Android by TomRoush.
the class PDFXrefStreamParser method parse.
/**
* Parses through the unfiltered stream and populates the xrefTable HashMap.
* @throws IOException If there is an error while parsing the stream.
*/
public void parse() throws IOException {
byte[] currLine = new byte[w[0] + w[1] + w[2]];
while (!seqSource.isEOF() && objectNumbers.hasNext()) {
seqSource.read(currLine);
// get the current objID
long objID = objectNumbers.next();
// default value is 1 if w[0] == 0, otherwise parse first field
int type = w[0] == 0 ? 1 : (int) parseValue(currLine, 0, w[0]);
// Skip free objects (type 0) and invalid types
if (type == 0) {
continue;
}
// second field holds the offset (type 1) or the object stream number (type 2)
long offset = parseValue(currLine, w[0], w[1]);
// third field holds the generation number for type 1 entries
int genNum = type == 1 ? (int) parseValue(currLine, w[0] + w[1], w[2]) : 0;
COSObjectKey objKey = new COSObjectKey(objID, genNum);
if (type == 1) {
xrefTrailerResolver.setXRef(objKey, offset);
} else {
// For XRef aware parsers we have to know which objects contain object streams. We will store this
// information in normal xref mapping table but add object stream number with minus sign in order to
// distinguish from file offsets
xrefTrailerResolver.setXRef(objKey, -offset);
}
}
close();
}
use of com.tom_roush.pdfbox.cos.COSObjectKey in project PdfBox-Android by TomRoush.
the class COSWriter method prepareIncrement.
private void prepareIncrement(PDDocument doc) {
try {
if (doc != null) {
COSDocument cosDoc = doc.getDocument();
Map<COSObjectKey, Long> xrefTable = cosDoc.getXrefTable();
Set<COSObjectKey> keySet = xrefTable.keySet();
long highestNumber = doc.getDocument().getHighestXRefObjectNumber();
for (COSObjectKey cosObjectKey : keySet) {
COSBase object = cosDoc.getObjectFromPool(cosObjectKey).getObject();
if (object != null && cosObjectKey != null && !(object instanceof COSNumber)) {
objectKeys.put(object, cosObjectKey);
keyObject.put(cosObjectKey, object);
}
if (cosObjectKey != null) {
long num = cosObjectKey.getNumber();
if (num > highestNumber) {
highestNumber = num;
}
}
}
setNumber(highestNumber);
}
} catch (IOException e) {
Log.e("PdfBox-Android", e.getMessage(), e);
}
}
use of com.tom_roush.pdfbox.cos.COSObjectKey in project PdfBox-Android by TomRoush.
the class COSWriter method writeReference.
/**
* visitFromObjRef method comment.
*
* @param obj The object that is being visited.
*
* @throws IOException If there is an exception while visiting this object.
*/
public void writeReference(COSBase obj) throws IOException {
COSObjectKey key = getObjectKey(obj);
getStandardOutput().write(String.valueOf(key.getNumber()).getBytes(Charsets.ISO_8859_1));
getStandardOutput().write(SPACE);
getStandardOutput().write(String.valueOf(key.getGeneration()).getBytes(Charsets.ISO_8859_1));
getStandardOutput().write(SPACE);
getStandardOutput().write(REFERENCE);
}
use of com.tom_roush.pdfbox.cos.COSObjectKey in project PdfBox-Android by TomRoush.
the class COSWriter method addObjectToWrite.
private void addObjectToWrite(COSBase object) {
COSBase actual = object;
if (actual instanceof COSObject) {
actual = ((COSObject) actual).getObject();
}
if (!writtenObjects.contains(object) && !objectsToWriteSet.contains(object) && !actualsAdded.contains(actual)) {
COSBase cosBase = null;
COSObjectKey cosObjectKey = null;
if (actual != null) {
cosObjectKey = objectKeys.get(actual);
}
if (cosObjectKey != null) {
cosBase = keyObject.get(cosObjectKey);
}
if (actual != null && objectKeys.containsKey(actual) && object instanceof COSUpdateInfo && !((COSUpdateInfo) object).isNeedToBeUpdated() && cosBase instanceof COSUpdateInfo && !((COSUpdateInfo) cosBase).isNeedToBeUpdated()) {
return;
}
objectsToWrite.add(object);
objectsToWriteSet.add(object);
if (actual != null) {
actualsAdded.add(actual);
}
}
}
Aggregations