From 965fbf449433d8d35a91f17487d6f490d0d41218 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 21 Jun 2022 17:03:26 +0100 Subject: [PATCH] Polish "Use java.util.HexFormat where appropriate" See gh-31477 --- .../boot/system/ApplicationTemp.java | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationTemp.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationTemp.java index 45996a39fa..9d904a6384 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationTemp.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/system/ApplicationTemp.java @@ -27,6 +27,7 @@ import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.security.MessageDigest; import java.util.EnumSet; +import java.util.HexFormat; import org.springframework.util.Assert; import org.springframework.util.StringUtils; @@ -41,8 +42,6 @@ import org.springframework.util.StringUtils; */ public class ApplicationTemp { - private static final char[] HEX_CHARS = "0123456789ABCDEF".toCharArray(); - private static final FileAttribute[] NO_FILE_ATTRIBUTES = {}; private static final EnumSet DIRECTORY_PERMISSIONS = EnumSet.of(PosixFilePermission.OWNER_READ, @@ -92,7 +91,7 @@ public class ApplicationTemp { private Path getPath() { if (this.path == null) { synchronized (this) { - String hash = toHexString(generateHash(this.sourceClass)); + String hash = HexFormat.of().withUpperCase().formatHex(generateHash(this.sourceClass)); this.path = createDirectory(getTempDirectory().resolve(hash)); } } @@ -160,14 +159,4 @@ public class ApplicationTemp { return source.toString().getBytes(); } - private String toHexString(byte[] bytes) { - char[] hex = new char[bytes.length * 2]; - for (int i = 0; i < bytes.length; 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); - } - }