Use java.util.HexFormat where appropriate

See gh-31477
pull/31494/head
dreis2211 2 years ago committed by Andy Wilkinson
parent 76cd2466ac
commit cc91009b70

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import java.nio.ByteBuffer;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel; import java.nio.channels.WritableByteChannel;
import java.util.HexFormat;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -45,7 +46,7 @@ public class HttpTunnelPayload {
private static final int BUFFER_SIZE = 1024 * 100; private static final int BUFFER_SIZE = 1024 * 100;
protected static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray(); private static final HexFormat HEX_FORMAT = HexFormat.of().withUpperCase();
private static final Log logger = LogFactory.getLog(HttpTunnelPayload.class); private static final Log logger = LogFactory.getLog(HttpTunnelPayload.class);
@ -173,13 +174,7 @@ public class HttpTunnelPayload {
*/ */
public String toHexString() { public String toHexString() {
byte[] bytes = this.data.array(); byte[] bytes = this.data.array();
char[] hex = new char[this.data.remaining() * 2]; return HEX_FORMAT.formatHex(bytes);
for (int i = this.data.position(); i < this.data.remaining(); i++) {
int b = bytes[i] & 0xFF;
hex[i * 2] = HEX_CHARS[b >>> 4];
hex[i * 2 + 1] = HEX_CHARS[b & 0x0F];
}
return new String(hex);
} }
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,10 +16,10 @@
package org.springframework.boot.buildpack.platform.docker.type; package org.springframework.boot.buildpack.platform.docker.type;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
import java.util.function.Function; import java.util.function.Function;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -126,9 +126,7 @@ public final class VolumeName {
private static String asHexString(byte[] digest, int digestLength) { private static String asHexString(byte[] digest, int digestLength) {
Assert.isTrue(digestLength <= digest.length, Assert.isTrue(digestLength <= digest.length,
() -> "DigestLength must be less than or equal to " + digest.length); () -> "DigestLength must be less than or equal to " + digest.length);
byte[] shortDigest = new byte[6]; return HexFormat.of().formatHex(digest, 0, digestLength);
System.arraycopy(digest, 0, shortDigest, 0, shortDigest.length);
return String.format("%0" + digestLength + "x", new BigInteger(1, shortDigest));
} }
/** /**

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.DigestInputStream;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
/** /**
* Utility class used to calculate digests. * Utility class used to calculate digests.
@ -41,11 +42,8 @@ final class Digest {
try { try {
try (DigestInputStream inputStream = new DigestInputStream(supplier.openStream(), try (DigestInputStream inputStream = new DigestInputStream(supplier.openStream(),
MessageDigest.getInstance("SHA-1"))) { MessageDigest.getInstance("SHA-1"))) {
byte[] buffer = new byte[4098]; inputStream.readAllBytes();
while (inputStream.read(buffer) != -1) { return HexFormat.of().formatHex(inputStream.getMessageDigest().digest());
// Read the entire stream
}
return bytesToHex(inputStream.getMessageDigest().digest());
} }
} }
catch (NoSuchAlgorithmException ex) { 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();
}
} }

Loading…
Cancel
Save