Use java.util.HexFormat where appropriate

See gh-31477
This commit is contained in:
dreis2211
2022-06-21 08:30:19 +02:00
committed by Andy Wilkinson
parent 76cd2466ac
commit cc91009b70
3 changed files with 11 additions and 28 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import java.io.IOException;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
/**
* Utility class used to calculate digests.
@@ -41,11 +42,8 @@ final class Digest {
try {
try (DigestInputStream inputStream = new DigestInputStream(supplier.openStream(),
MessageDigest.getInstance("SHA-1"))) {
byte[] buffer = new byte[4098];
while (inputStream.read(buffer) != -1) {
// Read the entire stream
}
return bytesToHex(inputStream.getMessageDigest().digest());
inputStream.readAllBytes();
return HexFormat.of().formatHex(inputStream.getMessageDigest().digest());
}
}
catch (NoSuchAlgorithmException ex) {
@@ -53,12 +51,4 @@ final class Digest {
}
}
private static String bytesToHex(byte[] bytes) {
StringBuilder hex = new StringBuilder();
for (byte b : bytes) {
hex.append(String.format("%02x", b));
}
return hex.toString();
}
}