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

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

Introduction

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

Prototype

public static byte[] toByteArray(InputStream in) throws IOException 

Source Link

Document

Reads the input stream and returns its contents as a byte array.

Usage

From source file:TSAClient.java

License:Apache License

private byte[] getTSAResponse(byte[] request) throws IOException {
    LOG.debug("Opening connection to TSA server");

    // todo: support proxy servers
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);/*from   w  ww . j av  a2s  .  com*/
    connection.setDoInput(true);
    connection.setRequestProperty("Content-Type", "application/timestamp-query");

    LOG.debug("Established connection to TSA server");

    if (username != null && password != null && !username.isEmpty() && !password.isEmpty()) {
        connection.setRequestProperty(username, password);
    }

    // read response
    OutputStream output = null;
    try {
        output = connection.getOutputStream();
        output.write(request);
    } finally {
        IOUtils.closeQuietly(output);
    }

    LOG.debug("Waiting for response from TSA server");

    InputStream input = null;
    byte[] response;
    try {
        input = connection.getInputStream();
        response = IOUtils.toByteArray(input);
    } finally {
        IOUtils.closeQuietly(input);
    }

    LOG.debug("Received response from TSA server");

    return response;
}

From source file:alfio.controller.api.WebhookApiController.java

License:Open Source License

private static Optional<String> readRequest(HttpServletRequest request) {
    try (ServletInputStream is = request.getInputStream()) {
        return Optional.ofNullable(IOUtils.toByteArray(is)).map(b -> new String(b, Charset.forName("UTF-8")));
    } catch (Exception e) {
        log.error("exception during request conversion", e);
        return Optional.empty();
    }//from w  w  w  .  java2 s .co m
}

From source file:ch.dowa.jassturnier.ResourceLoader.java

public static byte[] getIcon(String filename) {
    try {//w  ww. j  ava  2s  .c  om
        return IOUtils.toByteArray(rl.getClass().getResourceAsStream("icons/" + filename));
    } catch (IOException ex) {
        Logger.getLogger(ResourceLoader.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:com.modemo.javase.signature.ValidationTimeStamp.java

License:Apache License

/**
 * Creates a signed timestamp token by the given input stream.
 * /*from   w  w w.  j a  va  2 s  . c om*/
 * @param content InputStream of the content to sign
 * @return the byte[] of the timestamp token
 * @throws IOException
 */
public byte[] getTimeStampToken(InputStream content) throws IOException {
    return tsaClient.getTimeStampToken(IOUtils.toByteArray(content));
}

From source file:com.netsteadfast.greenstep.util.BusinessProcessManagementUtils.java

License:Apache License

private static byte[] getDiagramByte(ProcessInstance pi) throws Exception {
    byte[] data = null;
    ProcessDiagramGenerator processDiagramGenerator = new DefaultProcessDiagramGenerator();
    BpmnModel model = repositoryService.getBpmnModel(pi.getProcessDefinitionId());
    InputStream is = processDiagramGenerator.generateDiagram(model, "png",
            runtimeService.getActiveActivityIds(pi.getId()));
    data = IOUtils.toByteArray(is);
    is.close();//from   w  w w. j  ava2  s  .  c om
    is = null;
    return data;
}

From source file:com.netsteadfast.greenstep.util.BusinessProcessManagementUtils.java

License:Apache License

public static byte[] getProcessDefinitionDiagramById(String processDefinitionId) throws Exception {
    ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
            .processDefinitionId(processDefinitionId).singleResult();
    if (pd == null) {
        return null;
    }/*from   w  w  w.ja  va 2s .co  m*/
    byte data[] = null;
    ProcessDefinitionEntity pde = (ProcessDefinitionEntity) pd;
    InputStream is = repositoryService.getResourceAsStream(pde.getDeploymentId(), pde.getDiagramResourceName());
    data = IOUtils.toByteArray(is);
    is.close();
    is = null;
    return data;
}

From source file:com.sinefine.util.pdf.Pdfs.java

License:Apache License

/**
 * Returns {@code true} if the two PDF InputStreams are equal, {@code false} otherwise.
 *
 * <p>/*from  w  w w .j ava2  s.c  o m*/
 * This method is equivalent to:</p>
 * <ul>
 * <li>{@linkplain #areEqual(byte[], byte[])}, where the byte arrays represent the input
 * streams.</li>
 * </ul>
 *
 * @param actual The first PDF input stream
 * @param expected The second PDF input stream
 * @return {@code true} if the two PDF InputStreams are equal, {@code false} otherwise.
 * @throws IOException if an error occurs whilst processing the input streams.
 */
public static boolean areEqual(final InputStream actual, final InputStream expected) throws IOException {
    return areEqual(IOUtils.toByteArray(actual), IOUtils.toByteArray(expected));
}

From source file:com.sinefine.util.pdf.Pdfs.java

License:Apache License

/**
 * Returns {@code true} if the difference in size between the actual input stream and the expected
 * input stream is less than the tolerated difference.
 *
 * <p>/*from w ww .j  av a  2s .  co  m*/
 * This method is equivalent to the method {@link #areContentsSimilarSize(byte[], byte[], float)}
 * where the input streams are converted to byte arrays.
 * </p>
 *
 * @param actual the actual input stream.
 * @param expected the expected input stream.
 * @param tolerance the tolerance.
 * @return {@code true} if the difference in size between the actual input stream and the expected
 *     input stream is less than the tolerated difference.
 * @throws IOException if an I/O error occurs whilst trying to read the input streams.
 * @see #areContentsSimilarSize(byte[], byte[], float)
 */
public static boolean areContentsSimilarSize(final InputStream actual, final InputStream expected,
        final float tolerance) throws IOException {
    if (tolerance < 0 || tolerance > 1) {
        throw new IllegalArgumentException("The tolerance (" + tolerance + ") must be between 0 and 1!");
    }
    if (actual == null || expected == null) {
        return false;
    } else {
        return areContentsSimilarSize(IOUtils.toByteArray(actual), IOUtils.toByteArray(expected), tolerance);
    }
}

From source file:com.sinefine.util.pdf.Pdfs.java

License:Apache License

/**
 * Returns {@code true} if the <em>images</em> of the two PDF input streams are the same,
 * {@code false} otherwise.//from  w w  w. ja  v a  2s .com
 *
 * <p>
 * This method is equivalent to {@link #areImagesSame(byte[], byte[])} where the input streams are
 * converted to byte arrays.
 * </p>
 *
 * @param actual The first PDF input stream.
 * @param expected The second PDF input stream.
 * @return {@code true} if the contents of the two PDF byte arrays are equal, {@code false}
 *     otherwise.
 * @throws IOException if an error occurs whilst processing the input streams.
 */
public static boolean areImagesSame(final InputStream actual, final InputStream expected) throws IOException {
    if (actual == null || expected == null) {
        return false;
    } else {
        return areImagesSame(IOUtils.toByteArray(actual), IOUtils.toByteArray(expected));
    }
}

From source file:eu.europa.esig.dss.pades.validation.PDFDocumentValidator.java

License:Open Source License

@Override
public DSSDocument getOriginalDocument(String signatureId) throws DSSException {
    if (StringUtils.isBlank(signatureId)) {
        throw new NullPointerException("signatureId");
    }//from w  ww  . j  a va  2s.c  om
    List<AdvancedSignature> signatures = getSignatures();
    for (AdvancedSignature signature : signatures) {
        PAdESSignature padesSignature = (PAdESSignature) signature;
        if (padesSignature.getId().equals(signatureId)) {
            CAdESSignature cadesSignature = padesSignature.getCAdESSignature();
            DSSDocument inMemoryDocument = null;
            DSSDocument firstDocument = null;
            for (DSSDocument document : cadesSignature.getDetachedContents()) {
                byte[] content;
                try {
                    content = IOUtils.toByteArray(document.openStream());
                } catch (IOException e) {
                    throw new DSSException(e);
                }
                content = isBase64Encoded(content) ? Base64.decode(content) : content;
                if (firstDocument == null) {
                    firstDocument = new InMemoryDocument(content);
                    inMemoryDocument = firstDocument;
                } else {
                    DSSDocument doc = new InMemoryDocument(content);
                    inMemoryDocument.setNextDocument(document);
                    inMemoryDocument = document;
                }
            }
            return firstDocument;
        }
    }
    throw new DSSException("The signature with the given id was not found!");
}