in src/main/org/apache/tools/ant/taskdefs/Checksum.java [470:574]
private boolean generateChecksums() throws BuildException {
boolean checksumMatches = true;
InputStream fis = null;
OutputStream fos = null;
byte[] buf = new byte[readBufferSize];
try {
for (Map.Entry<File, Object> e : includeFileMap.entrySet()) {
messageDigest.reset();
File src = e.getKey();
if (!isCondition) {
log("Calculating " + algorithm + " checksum for " + src, Project.MSG_VERBOSE);
}
fis = Files.newInputStream(src.toPath());
DigestInputStream dis = new DigestInputStream(fis,
messageDigest);
while (dis.read(buf, 0, readBufferSize) != -1) {
// Empty statement
}
dis.close();
fis.close();
fis = null;
byte[] fileDigest = messageDigest.digest();
if (totalproperty != null) {
allDigests.put(src, fileDigest);
}
String checksum = createDigestString(fileDigest);
//can either be a property name string or a file
Object destination = e.getValue();
if (destination instanceof String) {
String prop = (String) destination;
if (isCondition) {
checksumMatches
= checksumMatches && checksum.equals(property);
} else {
getProject().setNewProperty(prop, checksum);
}
} else if (destination instanceof File) {
if (isCondition) {
File existingFile = (File) destination;
if (existingFile.exists()) {
try {
String suppliedChecksum =
readChecksum(existingFile);
checksumMatches = checksumMatches
&& checksum.equals(suppliedChecksum);
} catch (BuildException be) {
// file is on wrong format, swallow
checksumMatches = false;
}
} else {
checksumMatches = false;
}
} else {
File dest = (File) destination;
fos = Files.newOutputStream(dest.toPath());
fos.write(format.format(new Object[] {
checksum,
src.getName(),
FileUtils
.getRelativePath(dest
.getParentFile(),
src),
FileUtils
.getRelativePath(getProject()
.getBaseDir(),
src),
src.getAbsolutePath()
}).getBytes());
fos.write(System.lineSeparator().getBytes());
fos.close();
fos = null;
}
}
}
if (totalproperty != null) {
// Calculate the total checksum
// Convert the keys (source files) into a sorted array.
File[] keyArray = allDigests.keySet().toArray(new File[0]);
// File is Comparable, but sort-order is platform
// dependent (case-insensitive on Windows)
Arrays.sort(keyArray, Comparator.nullsFirst(
Comparator.comparing(this::getRelativeFilePath)));
// Loop over the checksums and generate a total hash.
messageDigest.reset();
for (File src : keyArray) {
// Add the digest for the file content
byte[] digest = allDigests.get(src);
messageDigest.update(digest);
// Add the file path
String fileName = getRelativeFilePath(src);
messageDigest.update(fileName.getBytes());
}
String totalChecksum = createDigestString(messageDigest.digest());
getProject().setNewProperty(totalproperty, totalChecksum);
}
} catch (Exception e) {
throw new BuildException(e, getLocation());
} finally {
FileUtils.close(fis);
FileUtils.close(fos);
}
return checksumMatches;
}