Example usage for org.apache.pdfbox.io IOUtils closeQuietly

List of usage examples for org.apache.pdfbox.io IOUtils closeQuietly

Introduction

In this page you can find the example usage for org.apache.pdfbox.io IOUtils closeQuietly.

Prototype

public static void closeQuietly(Closeable closeable) 

Source Link

Document

Null safe close of the given Closeable suppressing any exception.

Usage

From source file:org.olat.instantMessaging.manager.ChatLogHelper.java

License:Apache License

public void archive(OLATResourceable ores, File exportDirectory) {
    ObjectOutputStream out = null;
    try {//w  w w .j  a v  a 2 s  . c om
        File file = new File(exportDirectory, "chat.xml");
        Writer writer = new FileWriter(file);
        out = logXStream.createObjectOutputStream(writer);

        int counter = 0;
        List<InstantMessage> messages;
        do {
            messages = imDao.getMessages(ores, null, counter, BATCH_SIZE);
            for (InstantMessage message : messages) {
                out.writeObject(message);
            }
            counter += messages.size();
        } while (messages.size() == BATCH_SIZE);
    } catch (IOException e) {
        log.error("", e);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:org.structr.web.servlet.DeploymentServlet.java

License:Open Source License

/**
 * Unzip given file to given output directory.
 *
 * @param file// w w  w. j av  a  2 s  . c o  m
 * @param outputDir
 * @throws IOException
 */
private void unzip(final File file, final String outputDir) throws IOException {

    try (final ZipFile zipFile = new ZipFile(file)) {

        final Enumeration<? extends ZipEntry> entries = zipFile.entries();

        while (entries.hasMoreElements()) {

            final ZipEntry entry = entries.nextElement();
            final File targetFile = new File(outputDir, entry.getName());

            if (entry.isDirectory()) {

                targetFile.mkdirs();

            } else {

                targetFile.getParentFile().mkdirs();
                InputStream in = zipFile.getInputStream(entry);

                try (OutputStream out = new FileOutputStream(targetFile)) {

                    IOUtils.copy(in, out);
                    IOUtils.closeQuietly(in);
                }
            }
        }
    }
}

From source file:us.kagome.pdfbox.PDFMergerExample.java

License:Apache License

/**
 * Creates a compound PDF document from a list of input documents.
 * <p>//from ww  w. j av  a2s.  c om
 * The merged document is PDF/A-1b compliant, provided the source documents are as well. It
 * contains document properties title, creator and subject, currently hard-coded.
 *
 * @param sources list of source PDF document streams.
 * @return compound PDF document as a readable input stream.
 * @throws IOException if anything goes wrong during PDF merge.
 */
public InputStream merge(final List<InputStream> sources) throws IOException {
    String title = "My title";
    String creator = "Alexander Kriegisch";
    String subject = "Subject with umlauts ";

    ByteArrayOutputStream mergedPDFOutputStream = null;
    COSStream cosStream = null;
    try {
        // If you're merging in a servlet, you can modify this example to use the outputStream only
        // as the response as shown here: http://stackoverflow.com/a/36894346/535646
        mergedPDFOutputStream = new ByteArrayOutputStream();
        cosStream = new COSStream();

        PDFMergerUtility pdfMerger = createPDFMergerUtility(sources, mergedPDFOutputStream);

        // PDF and XMP properties must be identical, otherwise document is not PDF/A compliant
        PDDocumentInformation pdfDocumentInfo = createPDFDocumentInfo(title, creator, subject);
        PDMetadata xmpMetadata = createXMPMetadata(cosStream, title, creator, subject);
        //            pdfMerger.setDestinationDocumentInformation(pdfDocumentInfo);
        //            pdfMerger.setDestinationMetadata(xmpMetadata);

        LOG.info("Merging " + sources.size() + " source documents into one PDF");
        pdfMerger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
        LOG.info("PDF merge successful, size = {" + mergedPDFOutputStream.size() + "} bytes");

        return new ByteArrayInputStream(mergedPDFOutputStream.toByteArray());
    } catch (Exception e) {
        throw new IOException("PDF merge problem", e);
    } finally {
        for (InputStream source : sources) {
            IOUtils.closeQuietly(source);
        }
        IOUtils.closeQuietly(cosStream);
        IOUtils.closeQuietly(mergedPDFOutputStream);
    }
}

From source file:us.kagome.pdfbox.PDFMergerExample.java

License:Apache License

private PDMetadata createXMPMetadata(COSStream cosStream, String title, String creator, String subject)
        throws BadFieldValueException, TransformerException, IOException {
    LOG.info("Setting XMP metadata (title, author, subject) for merged PDF");
    XMPMetadata xmpMetadata = XMPMetadata.createXMPMetadata();

    // PDF/A-1b properties
    PDFAIdentificationSchema pdfaSchema = xmpMetadata.createAndAddPFAIdentificationSchema();
    pdfaSchema.setPart(1);/*from  ww  w  . j  a  va2s  .  c  o m*/
    pdfaSchema.setConformance("B");

    // Dublin Core properties
    DublinCoreSchema dublinCoreSchema = xmpMetadata.createAndAddDublinCoreSchema();
    dublinCoreSchema.setTitle(title);
    dublinCoreSchema.addCreator(creator);
    dublinCoreSchema.setDescription(subject);

    // XMP Basic properties
    XMPBasicSchema basicSchema = xmpMetadata.createAndAddXMPBasicSchema();
    Calendar creationDate = Calendar.getInstance();
    basicSchema.setCreateDate(creationDate);
    basicSchema.setModifyDate(creationDate);
    basicSchema.setMetadataDate(creationDate);
    basicSchema.setCreatorTool(creator);

    // Create and return XMP data structure in XML format
    ByteArrayOutputStream xmpOutputStream = null;
    OutputStream cosXMPStream = null;
    try {
        xmpOutputStream = new ByteArrayOutputStream();
        cosXMPStream = cosStream.createOutputStream();
        new XmpSerializer().serialize(xmpMetadata, xmpOutputStream, true);
        cosXMPStream.write(xmpOutputStream.toByteArray());
        return new PDMetadata(cosStream);
    } finally {
        IOUtils.closeQuietly(xmpOutputStream);
        IOUtils.closeQuietly(cosXMPStream);
    }
}