diff options
Diffstat (limited to 'src/main/org/apache/tools/ant/types/selectors/modifiedselector/ChecksumAlgorithm.java')
-rw-r--r-- | src/main/org/apache/tools/ant/types/selectors/modifiedselector/ChecksumAlgorithm.java | 37 |
1 files changed, 15 insertions, 22 deletions
diff --git a/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ChecksumAlgorithm.java b/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ChecksumAlgorithm.java index 26c694590..a660caf03 100644 --- a/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ChecksumAlgorithm.java +++ b/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ChecksumAlgorithm.java @@ -20,7 +20,6 @@ package org.apache.tools.ant.types.selectors.modifiedselector; import java.io.BufferedInputStream; import java.io.File; -import java.io.InputStream; import java.nio.file.Files; import java.security.NoSuchAlgorithmException; import java.util.Locale; @@ -105,6 +104,7 @@ public class ChecksumAlgorithm implements Algorithm { * This algorithm supports only CRC and Adler. * @return <i>true</i> if all is ok, otherwise <i>false</i>. */ + @Override public boolean isValid() { return "CRC".equals(algorithm) || "ADLER".equals(algorithm); } @@ -115,26 +115,22 @@ public class ChecksumAlgorithm implements Algorithm { * @param file File object for which the value should be evaluated. * @return The value for that file */ + @Override public String getValue(File file) { initChecksum(); - String rval = null; - - try { - if (file.canRead()) { - checksum.reset(); - InputStream fis = Files.newInputStream(file.toPath()); - CheckedInputStream check = new CheckedInputStream(fis, checksum); - BufferedInputStream in = new BufferedInputStream(check); - while (in.read() != -1) { - // Read the file - } - rval = Long.toString(check.getChecksum().getValue()); - in.close(); + + if (file.canRead()) { + checksum.reset(); + try (CheckedInputStream check = new CheckedInputStream( + new BufferedInputStream(Files.newInputStream(file.toPath())), checksum)) { + // Read the file + while (check.read() != -1) + ; + return Long.toString(check.getChecksum().getValue()); + } catch (Exception e) { } - } catch (Exception e) { - rval = null; } - return rval; + return null; } @@ -142,11 +138,8 @@ public class ChecksumAlgorithm implements Algorithm { * Override Object.toString(). * @return some information about this algorithm. */ + @Override public String toString() { - StringBuilder buf = new StringBuilder(); - buf.append("<ChecksumAlgorithm:"); - buf.append("algorithm=").append(algorithm); - buf.append(">"); - return buf.toString(); + return String.format("<ChecksumAlgorithm:algorithm=%s>", algorithm); } } |