package org.argeo.util;
+import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.nio.file.DirectoryStream;
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");
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;
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);
}
}
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');
List<String> 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());
}
}
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);
}
}