Search in sources :

Example 51 with DigestOutputStream

use of java.security.DigestOutputStream in project sling by apache.

the class FileDistributionPackageBuilder method createPackageForAdd.

@Override
protected DistributionPackage createPackageForAdd(@Nonnull ResourceResolver resourceResolver, @Nonnull final DistributionRequest request) throws DistributionException {
    DistributionPackage distributionPackage;
    OutputStream outputStream = null;
    String digestMessage = null;
    final File file;
    try {
        file = File.createTempFile("distrpck-create-" + randomUUID(), "." + getType(), tempDirectory);
        if (digestAlgorithm != null) {
            outputStream = openDigestOutputStream(new FileOutputStream(file), digestAlgorithm);
        } else {
            outputStream = new FileOutputStream(file);
        }
        final DistributionExportFilter filter = distributionContentSerializer.isRequestFiltering() ? null : DistributionExportFilter.createFilter(request, nodeFilters, propertyFilters);
        DistributionExportOptions distributionExportOptions = new DistributionExportOptions(request, filter);
        distributionContentSerializer.exportToStream(resourceResolver, distributionExportOptions, outputStream);
        outputStream.flush();
        if (digestAlgorithm != null) {
            digestMessage = readDigestMessage((DigestOutputStream) outputStream);
        }
        distributionPackage = new FileDistributionPackage(file, getType(), digestAlgorithm, digestMessage);
    } catch (IOException e) {
        throw new DistributionException(e);
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
    return distributionPackage;
}
Also used : DistributionExportOptions(org.apache.sling.distribution.serialization.DistributionExportOptions) DistributionPackage(org.apache.sling.distribution.packaging.DistributionPackage) DigestUtils.openDigestOutputStream(org.apache.sling.distribution.util.impl.DigestUtils.openDigestOutputStream) DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) DigestUtils.openDigestOutputStream(org.apache.sling.distribution.util.impl.DigestUtils.openDigestOutputStream) FileOutputStream(java.io.FileOutputStream) DigestOutputStream(java.security.DigestOutputStream) FileOutputStream(java.io.FileOutputStream) DistributionExportFilter(org.apache.sling.distribution.serialization.DistributionExportFilter) DistributionException(org.apache.sling.distribution.common.DistributionException) IOException(java.io.IOException) File(java.io.File)

Example 52 with DigestOutputStream

use of java.security.DigestOutputStream in project bnd by bndtools.

the class CachingUriResourceHandle method copyWithSHA.

private String copyWithSHA(InputStream input, OutputStream output) throws IOException {
    try {
        MessageDigest digest = MessageDigest.getInstance(SHA_256);
        DigestOutputStream digestOutput = new DigestOutputStream(output, digest);
        IO.copy(input, digestOutput);
        return Hex.toHexString(digest.digest());
    } catch (NoSuchAlgorithmException e) {
        // Can't happen... hopefully...
        throw new IOException(e.getMessage(), e);
    } finally {
        IO.close(input);
        IO.close(output);
    }
}
Also used : DigestOutputStream(java.security.DigestOutputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest)

Example 53 with DigestOutputStream

use of java.security.DigestOutputStream in project Essentials by drtshock.

the class ManagedFile method copyResourceAscii.

public static void copyResourceAscii(final String resourceName, final File file) throws IOException {
    final InputStreamReader reader = new InputStreamReader(ManagedFile.class.getResourceAsStream(resourceName));
    try {
        final MessageDigest digest = getDigest();
        final DigestOutputStream digestStream = new DigestOutputStream(new FileOutputStream(file), digest);
        try {
            final OutputStreamWriter writer = new OutputStreamWriter(digestStream);
            try {
                final char[] buffer = new char[BUFFERSIZE];
                do {
                    final int length = reader.read(buffer);
                    if (length >= 0) {
                        writer.write(buffer, 0, length);
                    } else {
                        break;
                    }
                } while (true);
                writer.write("\n");
                writer.flush();
                final BigInteger hashInt = new BigInteger(1, digest.digest());
                digestStream.on(false);
                digestStream.write('#');
                digestStream.write(hashInt.toString(16).getBytes());
            } finally {
                writer.close();
            }
        } finally {
            digestStream.close();
        }
    } finally {
        reader.close();
    }
}
Also used : DigestOutputStream(java.security.DigestOutputStream) BigInteger(java.math.BigInteger) MessageDigest(java.security.MessageDigest)

Aggregations

DigestOutputStream (java.security.DigestOutputStream)53 MessageDigest (java.security.MessageDigest)43 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)20 OutputStream (java.io.OutputStream)18 IOException (java.io.IOException)17 File (java.io.File)9 FileOutputStream (java.io.FileOutputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)6 DigestInputStream (java.security.DigestInputStream)5 NullOutputStream (org.apache.commons.io.output.NullOutputStream)5 InputStream (java.io.InputStream)4 Map (java.util.Map)4 Support_OutputStream (tests.support.Support_OutputStream)4 Base64OutputStream (android.util.Base64OutputStream)3 BufferedOutputStream (java.io.BufferedOutputStream)3 DataOutputStream (java.io.DataOutputStream)3 FileInputStream (java.io.FileInputStream)3 Attributes (java.util.jar.Attributes)3 Manifest (java.util.jar.Manifest)3