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;
}
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);
}
}
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();
}
}
Aggregations