Example usage for org.apache.pdfbox.cos COSBase isDirect

List of usage examples for org.apache.pdfbox.cos COSBase isDirect

Introduction

In this page you can find the example usage for org.apache.pdfbox.cos COSBase isDirect.

Prototype

public boolean isDirect() 

Source Link

Document

If the state is set true, the dictionary will be written direct into the called object.

Usage

From source file:com.aaasec.sigserv.csspsupport.pdfbox.modifications.CsCOSWriter.java

License:Apache License

/**
 * visitFromStream method comment./*ww  w.  j  av  a2 s  .c  o  m*/
 *
 * @param obj The object that is being visited.
 *
 * @throws COSVisitorException If there is an exception while visiting this
 * object.
 *
 * @return null
 */
public Object visitFromStream(COSStream obj) throws COSVisitorException {
    InputStream input = null;
    try {
        if (willEncrypt) {
            document.getSecurityHandler().encryptStream(obj, currentObjectKey.getNumber(),
                    currentObjectKey.getGeneration());
        }

        COSObject lengthObject = null;
        // check if the length object is required to be direct, like in
        // a cross reference stream dictionary
        COSBase lengthEntry = obj.getDictionaryObject(COSName.LENGTH);
        String type = obj.getNameAsString(COSName.TYPE);
        if (lengthEntry != null && lengthEntry.isDirect() || "XRef".equals(type)) {
            // the length might be the non encoded length,
            // set the real one as direct object
            COSInteger cosInteger = COSInteger.get(obj.getFilteredLength());
            cosInteger.setDirect(true);
            obj.setItem(COSName.LENGTH, cosInteger);

        } else {
            // make the length an implicit indirect object
            // set the length of the stream and write stream dictionary
            lengthObject = new COSObject(null);

            obj.setItem(COSName.LENGTH, lengthObject);
        }
        input = obj.getFilteredStream();
        //obj.accept(this);
        // write the stream content
        visitFromDictionary(obj);
        getStandardOutput().write(STREAM);
        getStandardOutput().writeCRLF();
        byte[] buffer = new byte[1024];
        int amountRead = 0;
        int totalAmountWritten = 0;
        while ((amountRead = input.read(buffer, 0, 1024)) != -1) {
            getStandardOutput().write(buffer, 0, amountRead);
            totalAmountWritten += amountRead;
        }
        // set the length as an indirect object
        if (lengthObject != null) {
            lengthObject.setObject(COSInteger.get(totalAmountWritten));
        }
        getStandardOutput().writeCRLF();
        getStandardOutput().write(ENDSTREAM);
        getStandardOutput().writeEOL();
        return null;
    } catch (Exception e) {
        throw new COSVisitorException(e);
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                throw new COSVisitorException(e);
            }
        }
    }
}