Search in sources :

Example 31 with COSObjectKey

use of org.apache.pdfbox.cos.COSObjectKey in project pdfbox by apache.

the class COSWriter method getObjectKey.

/**
 * This will get the object key for the object.
 *
 * @param obj The object to get the key for.
 *
 * @return The object key for the object.
 */
private COSObjectKey getObjectKey(COSBase obj) {
    COSBase actual = obj;
    if (actual instanceof COSObject) {
        actual = ((COSObject) obj).getObject();
    }
    COSObjectKey key = null;
    if (actual != null) {
        key = objectKeys.get(actual);
    }
    if (key == null) {
        key = objectKeys.get(obj);
    }
    if (key == null) {
        setNumber(getNumber() + 1);
        key = new COSObjectKey(getNumber(), 0);
        objectKeys.put(obj, key);
        if (actual != null) {
            objectKeys.put(actual, key);
        }
    }
    return key;
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSObject(org.apache.pdfbox.cos.COSObject) COSBase(org.apache.pdfbox.cos.COSBase)

Example 32 with COSObjectKey

use of org.apache.pdfbox.cos.COSObjectKey in project pdfbox by apache.

the class StreamValidationProcess method checkStreamLength.

protected void checkStreamLength(PreflightContext context, COSObject cObj) throws ValidationException {
    COSStream streamObj = (COSStream) cObj.getObject();
    int length = streamObj.getInt(COSName.LENGTH);
    InputStream ra = null;
    try {
        ra = context.getSource().getInputStream();
        Long offset = context.getDocument().getDocument().getXrefTable().get(new COSObjectKey(cObj));
        // ---- go to the beginning of the object
        long skipped = 0;
        if (offset != null) {
            while (skipped != offset) {
                long curSkip = ra.skip(offset - skipped);
                if (curSkip < 0) {
                    addValidationError(context, new ValidationError(ERROR_SYNTAX_STREAM_DAMAGED, "Unable to skip bytes in the PDFFile to check stream length"));
                    return;
                }
                skipped += curSkip;
            }
            // ---- go to the stream key word
            if (readUntilStream(ra)) {
                int c = ra.read();
                if (c == '\r') {
                    ra.read();
                }
                // else c is '\n' no more character to read
                // ---- Here is the true beginning of the Stream Content.
                // ---- Read the given length of bytes and check the 10 next bytes
                // ---- to see if there are endstream.
                byte[] buffer = new byte[1024];
                int nbBytesToRead = length;
                do {
                    int cr;
                    if (nbBytesToRead > buffer.length) {
                        cr = ra.read(buffer);
                    } else {
                        cr = ra.read(buffer, 0, nbBytesToRead);
                    }
                    if (cr == -1) {
                        addStreamLengthValidationError(context, cObj, length, "");
                        return;
                    } else {
                        nbBytesToRead -= cr;
                    }
                } while (nbBytesToRead > 0);
                int len = "endstream".length() + 2;
                byte[] buffer2 = new byte[len];
                for (int i = 0; i < len; ++i) {
                    buffer2[i] = (byte) ra.read();
                }
                // ---- check the content of 10 last characters
                String endStream = new String(buffer2, Charsets.ISO_8859_1);
                if (buffer2[0] == '\r' && buffer2[1] == '\n') {
                    if (!endStream.contains("endstream")) {
                        addStreamLengthValidationError(context, cObj, length, endStream);
                    }
                } else if (buffer2[0] == '\r' && buffer2[1] == 'e') {
                    if (!endStream.contains("endstream")) {
                        addStreamLengthValidationError(context, cObj, length, endStream);
                    }
                } else if (buffer2[0] == '\n' && buffer2[1] == 'e') {
                    if (!endStream.contains("endstream")) {
                        addStreamLengthValidationError(context, cObj, length, endStream);
                    }
                } else {
                    if (!endStream.startsWith("endStream")) {
                        addStreamLengthValidationError(context, cObj, length, endStream);
                    }
                }
            } else {
                addStreamLengthValidationError(context, cObj, length, "");
            }
        }
    } catch (IOException e) {
        throw new ValidationException("Unable to read a stream to validate: " + e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(ra);
    }
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSStream(org.apache.pdfbox.cos.COSStream) ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) InputStream(java.io.InputStream) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) IOException(java.io.IOException)

Example 33 with COSObjectKey

use of org.apache.pdfbox.cos.COSObjectKey in project pdfbox by apache.

the class COSUtils method getAsInteger.

/**
 * Return the COSBase object as Integer if the COSBase object is an instance of COSInteger or a reference to a
 * COSInteger object. In other cases, this method returns null;
 *
 * @param cbase the object to get.
 * @param cDoc the document.
 * @return the object as Integer if the object is a COSInteger or a reference to it. Returns null otherwise.
 */
public static Integer getAsInteger(COSBase cbase, COSDocument cDoc) {
    if (cbase instanceof COSObject) {
        COSObjectKey key = new COSObjectKey((COSObject) cbase);
        COSObject obj = cDoc.getObjectFromPool(key);
        if (obj == null) {
            return null;
        } else if (obj.getObject() instanceof COSNumber) {
            return ((COSNumber) obj.getObject()).intValue();
        } else {
            return null;
        }
    } else if (cbase instanceof COSNumber) {
        return ((COSNumber) cbase).intValue();
    } else {
        return null;
    }
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSObject(org.apache.pdfbox.cos.COSObject) COSNumber(org.apache.pdfbox.cos.COSNumber)

Example 34 with COSObjectKey

use of org.apache.pdfbox.cos.COSObjectKey in project pdfbox by apache.

the class COSUtils method getCOSObjectAsClass.

/**
 * Return the COSObject object as class if the COSObject object is a reference to an object of
 * that class. If not, then this method returns null;
 *
 * @param cosObject the object to get.
 * @param cDoc the document.
 * @param claz the class.
 * @return the object as class if the object is a reference to that class. Returns null
 * otherwise.
 */
private static COSBase getCOSObjectAsClass(COSObject cosObject, COSDocument cDoc, Class claz) {
    COSObjectKey key = new COSObjectKey(cosObject);
    COSObject obj = cDoc.getObjectFromPool(key);
    if (obj != null && claz.isInstance(obj.getObject())) {
        return obj.getObject();
    } else {
        return null;
    }
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSObject(org.apache.pdfbox.cos.COSObject)

Example 35 with COSObjectKey

use of org.apache.pdfbox.cos.COSObjectKey in project pdfbox by apache.

the class COSUtils method isClass.

/**
 * return true if the elt is of class or a reference to a that class.
 *
 * @param elt the object to check.
 * @param doc the document.
 * @param claz the class.
 * @return true if the object is a of that class or a reference to it.
 */
private static boolean isClass(COSBase elt, COSDocument doc, Class claz) {
    if (elt instanceof COSObject) {
        COSObjectKey key = new COSObjectKey((COSObject) elt);
        COSObject obj = doc.getObjectFromPool(key);
        return (obj != null && claz.isInstance(obj.getObject()));
    }
    return claz.isInstance(elt);
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSObject(org.apache.pdfbox.cos.COSObject)

Aggregations

COSObjectKey (org.apache.pdfbox.cos.COSObjectKey)39 COSObject (org.apache.pdfbox.cos.COSObject)25 IOException (java.io.IOException)16 COSDocument (org.apache.pdfbox.cos.COSDocument)13 COSBase (org.apache.pdfbox.cos.COSBase)12 COSDictionary (org.apache.pdfbox.cos.COSDictionary)8 COSStream (org.apache.pdfbox.cos.COSStream)7 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)7 COSArray (org.apache.pdfbox.cos.COSArray)6 COSString (org.apache.pdfbox.cos.COSString)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 COSNumber (org.apache.pdfbox.cos.COSNumber)4 COSInteger (org.apache.pdfbox.cos.COSInteger)3 COSName (org.apache.pdfbox.cos.COSName)3 InputStream (java.io.InputStream)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 PDFObjectStreamParser (org.apache.pdfbox.pdfparser.PDFObjectStreamParser)2