X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.util%2Fsrc%2Forg%2Fargeo%2Futil%2FDirH.java;h=013897d23644f09e49a9a5862651f90ce649da6a;hb=3c1cdc594d954520b14646102b366290bdad58c7;hp=4035b96bc15b6c4409f10819f72e2c8450cdc5df;hpb=a5c66ac837e01e447161c3d73fe35eadc26cf304;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.util/src/org/argeo/util/DirH.java b/org.argeo.util/src/org/argeo/util/DirH.java index 4035b96bc..013897d23 100644 --- a/org.argeo.util/src/org/argeo/util/DirH.java +++ b/org.argeo.util/src/org/argeo/util/DirH.java @@ -1,5 +1,6 @@ package org.argeo.util; +import java.io.IOException; import java.io.PrintStream; import java.nio.charset.Charset; import java.nio.file.DirectoryStream; @@ -12,7 +13,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -/** Hashes the hashes of the files in a directory.*/ +/** Hashes the hashes of the files in a directory. */ public class DirH { private final static Charset charset = Charset.forName("UTF-16"); @@ -30,12 +31,11 @@ public class DirH { private final byte[] dirName; /** - * @param dirName - * can be null or empty + * @param dirName can be null or empty */ private DirH(byte[][] hashes, byte[][] fileNames, byte[] dirName) { if (hashes.length != fileNames.length) - throw new UtilsException(hashes.length + " hashes and " + fileNames.length + " file names"); + throw new IllegalArgumentException(hashes.length + " hashes and " + fileNames.length + " file names"); this.hashes = hashes; this.fileNames = fileNames; this.dirName = dirName == null ? new byte[0] : dirName; @@ -49,7 +49,7 @@ public class DirH { hashSize = hashes[0].length; for (int i = 0; i < hashes.length; i++) { if (hashes[i].length != hashSize) - throw new UtilsException( + throw new IllegalArgumentException( "Hash size for " + new String(fileNames[i], charset) + " is " + hashes[i].length); } @@ -63,19 +63,19 @@ public class DirH { } digest = md.digest(); } catch (NoSuchAlgorithmException e) { - throw new UtilsException("Cannot digest", e); + throw new IllegalArgumentException("Cannot digest", e); } } public void print(PrintStream out) { - out.print(DigestUtils.encodeHexString(digest)); + out.print(DigestUtils.toHexString(digest)); if (dirName.length > 0) { out.print(' '); out.print(new String(dirName, charset)); } out.print('\n'); for (int i = 0; i < hashes.length; i++) { - out.print(DigestUtils.encodeHexString(hashes[i])); + out.print(DigestUtils.toHexString(hashes[i])); out.print(' '); out.print(new String(fileNames[i], charset)); out.print('\n'); @@ -88,7 +88,7 @@ public class DirH { List fNames = new ArrayList<>(); for (Path file : files) { if (!Files.isDirectory(file)) { - byte[] digest = DigestUtils.digestRaw(algorithm, file, bufferSize); + byte[] digest = DigestUtils.digestAsBytes(algorithm, file, bufferSize); hs.add(digest); fNames.add(file.getFileName().toString()); } @@ -100,8 +100,8 @@ public class DirH { } byte[][] hashes = hs.toArray(new byte[hs.size()][]); return new DirH(hashes, fileNames, dir.toString().getBytes(charset)); - } catch (Exception e) { - throw new UtilsException("Cannot digest " + dir, e); + } catch (IOException e) { + throw new RuntimeException("Cannot digest " + dir, e); } }