/** * Gives data file as stream * * @return data file stream */ public InputStream getStream() { logger.debug(""); return document.openStream(); }
private byte[] getDocumentBytes(DSSDocument document) { try { return IOUtils.toByteArray(document.openStream()); } catch (IOException e) { logger.error("Error getting document content: " + e.getMessage()); throw new TechnicalException("Error getting document content: " + e.getMessage(), e); } }
/** * This method returns the {@link org.w3c.dom.Document} created based on the {@link eu.europa.esig.dss.DSSDocument}. * * @param dssDocument * The DSS representation of the document from which the dssDocument is created. * @return a new {@link org.w3c.dom.Document} from {@link eu.europa.esig.dss.DSSDocument} */ public static Document buildDOM(final DSSDocument dssDocument) { return buildDOM(dssDocument.openStream()); }
@Override public byte[] getAdESSignature() { logger.debug("Getting full XAdES signature byte array"); try { return IOUtils.toByteArray(signatureDocument.openStream()); } catch (IOException e) { throw new TechnicalException("Error parsing xades signature: " + e.getMessage(), e); } }
/** * Gives file bytes * * @return data as bytes */ public byte[] getBytes() { logger.debug(""); try { return IOUtils.toByteArray(document.openStream()); } catch (IOException e) { throw new TechnicalException("Error reading document bytes: " + e.getMessage(), e); } }
/** * Reads the first byte from the DSSDocument * * @param dssDocument * the document * @return the first byte */ public static byte readFirstByte(final DSSDocument dssDocument) { byte[] result = new byte[1]; try (InputStream inputStream = dssDocument.openStream()) { inputStream.read(result, 0, 1); } catch (IOException e) { throw new DSSException(e); } return result[0]; }
/** * Reads maximum {@code headerLength} bytes from {@code dssDocument} to the given {@code byte} array. * * @param dssDocument * {@code DSSDocument} to read * @param headerLength * {@code int}: maximum number of bytes to read * @param destinationByteArray * destination {@code byte} array * @return the number of read bytes */ public static int readToArray(final DSSDocument dssDocument, final int headerLength, final byte[] destinationByteArray) { try (InputStream inputStream = dssDocument.openStream()) { return inputStream.read(destinationByteArray, 0, headerLength); } catch (IOException e) { throw new DSSException(e); } }
public static byte[] digest(DigestAlgorithm digestAlgorithm, DSSDocument document) { try (InputStream is = document.openStream()) { return digest(digestAlgorithm, is); } catch (IOException e) { throw new DSSException(e); } }
/** * This method create a new document from a sub-part of another document * * @param origin * the original document * @param start * the start position to retrieve * @param end * the end position to retrieve * @return a new DSSDocument */ public static DSSDocument splitDocument(DSSDocument origin, int start, int end) { try (InputStream is = origin.openStream(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayOutputStream baos = new ByteArrayOutputStream()) { int i = 0; int r; while ((r = bis.read()) != -1) { if (i >= start && i <= end) { baos.write(r); } i++; } baos.flush(); return new InMemoryDocument(baos.toByteArray()); } catch (Exception e) { throw new DSSException("Unable to split document", e); } }
/** * Get the contents of an {@code DSSDocument} as a {@code byte[]}. * * @param document * the document to read * @return the content as byte array */ public static byte[] toByteArray(final DSSDocument document) { try (InputStream is = document.openStream()) { return toByteArray(is); } catch (IOException e) { throw new DSSException(e); } }
private static boolean parseAsicContainer(BufferedInputStream stream, MimeType mtype) throws IOException { stream.mark(stream.available() + 1); ZipInputStream zipInputStream = new ZipInputStream(stream); try { ZipEntry entry; while ((entry = zipInputStream.getNextEntry()) != null) { if (StringUtils.equalsIgnoreCase("mimetype", entry.getName())) { InputStream zipFileInputStream = zipInputStream; BOMInputStream bomInputStream = new BOMInputStream(zipFileInputStream); DSSDocument document = new InMemoryDocument(bomInputStream); String mimeType = StringUtils.trim(IOUtils.toString(IOUtils.toByteArray(document.openStream()), "UTF-8")); if (StringUtils.equalsIgnoreCase(mimeType, mtype.getMimeTypeString())) { return true; } } } } catch (IOException e) { logger.error("Error reading asic container stream: " + e.getMessage()); throw new TechnicalException("Error reading asic container stream: ", e); } finally { stream.reset(); } return false; }
/** * @param asicEntries list of ASIC entries */ public void writeExistingEntries(Collection<AsicEntry> asicEntries) { logger.debug("Writing existing zip container entries"); for (AsicEntry asicEntry : asicEntries) { DSSDocument content = asicEntry.getContent(); ZipEntry zipEntry = asicEntry.getZipEntry(); if (!StringUtils.equalsIgnoreCase(ZIP_ENTRY_MIMETYPE, zipEntry.getName())) { zipOutputStream.setLevel(ZipEntry.DEFLATED); } new StreamEntryCallback(zipEntry, content.openStream(), false).write(); } }
DSSDocument signedDocument = service.signDocument(documentToBeSigned, parameters, signatureValue); return IOUtils.toByteArray(signedDocument.openStream());