@Override public byte[] getBytes() throws DSSException { final InputStream inputStream = openStream(); final byte[] bytes = DSSUtils.toByteArray(inputStream); IOUtils.closeQuietly(inputStream); return bytes; }
@Override public String getBase64Encoded() { final InputStream inputStream = openStream(); final byte[] bytes = DSSUtils.toByteArray(inputStream); IOUtils.closeQuietly(inputStream); return Base64.encodeBase64String(bytes); }
@Override public String getDigest(final DigestAlgorithm digestAlgorithm) { final InputStream inputStream = openStream(); final byte[] digestBytes = DSSUtils.digest(digestAlgorithm, inputStream); IOUtils.closeQuietly(inputStream); final String base64Encode = Base64.encodeBase64String(digestBytes); return base64Encode; }
@Override public void save(final String filePath) { try { final FileOutputStream fos = new FileOutputStream(filePath); DSSUtils.write(getBytes(), fos); fos.close(); } catch (DSSException e) { throw new DSSException(e); } catch (IOException e) { throw new DSSException(e); } }
/** * This method returns the {@code ASN1Sequence} encapsulated in {@code DEROctetString}. The {@code * DEROctetString} is represented as {@code byte} array. * * @param bytes {@code byte} representation of {@code DEROctetString} * @return encapsulated {@code ASN1Sequence} * @throws DSSException in case of a decoding problem */ public static ASN1Sequence getAsn1SequenceFromDerOctetString(byte[] bytes) throws DSSException { ASN1InputStream input = null; try { input = new ASN1InputStream(bytes); final DEROctetString s = (DEROctetString) input.readObject(); final byte[] content = s.getOctets(); input.close(); input = new ASN1InputStream(content); final ASN1Sequence seq = (ASN1Sequence) input.readObject(); return seq; } catch (IOException e) { throw new DSSException("Error when converting byte array to ASN1Sequence!", e); } finally { DSSUtils.closeQuietly(input); } }
/** * This method generates a bouncycastle {@code TimeStampToken} based on base 64 encoded {@code * String}. * * @param base64EncodedTimestamp * @return bouncycastle {@code TimeStampToken} * @throws DSSException */ public static TimeStampToken createTimeStampToken(final String base64EncodedTimestamp) throws DSSException { try { final byte[] tokenBytes = DSSUtils.base64Decode(base64EncodedTimestamp); final CMSSignedData signedData = new CMSSignedData(tokenBytes); return new TimeStampToken(signedData); } catch (DSSException e) { throw new DSSException(e); } catch (CMSException e) { throw new DSSException(e); } catch (TSPException e) { throw new DSSException(e); } catch (IOException e) { throw new DSSException(e); } }
/** * Creates dss document that retains the data in memory * * @param inputStream input stream representing the document * @param name the file name if the data originates from a file * @param mimeType the mime type of the file if the data originates from a file * @throws IOException */ public InMemoryDocument(final InputStream inputStream, final String name, final MimeType mimeType) throws DSSException { this(DSSUtils.toByteArray(inputStream), name, mimeType); }
/** * Creates dss document that retains the data in memory * * @param inputStream input stream representing the document * @throws DSSException */ public InMemoryDocument(final InputStream inputStream) throws DSSException { this(DSSUtils.toByteArray(inputStream), null, null); }
@Override public String getDigest(final DigestAlgorithm digestAlgorithm) { final byte[] digestBytes = DSSUtils.digest(digestAlgorithm, bytes); final String base64Encode = Base64.encodeBase64String(digestBytes); return base64Encode; }
@Override public void save(final String path) throws IOException { final InputStream inputStream = openStream(); DSSUtils.saveToFile(inputStream, path); IOUtils.closeQuietly(inputStream); }
@Override public InputStream openStream() throws DSSException { final InputStream inputStream = DSSUtils.toInputStream(file); return inputStream; }