diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..b9c5f72 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-06-30 - Replace String.format with HexFormat for byte-to-hex conversion +**Learning:** Using a loop with `String.format("%02x", b)` to convert byte arrays to hex strings causes unnecessary memory allocations and is a performance bottleneck in this application. +**Action:** Always use `java.util.HexFormat.of().formatHex(bytes)` instead of manual string formatting loops for converting bytes to hex strings to optimize performance and memory usage. diff --git a/src/main/java/com/clearfolio/viewer/service/DefaultDocumentConversionService.java b/src/main/java/com/clearfolio/viewer/service/DefaultDocumentConversionService.java index 9c59593..3cd4597 100644 --- a/src/main/java/com/clearfolio/viewer/service/DefaultDocumentConversionService.java +++ b/src/main/java/com/clearfolio/viewer/service/DefaultDocumentConversionService.java @@ -121,12 +121,7 @@ private String contentHash(MultipartFile file) { } byte[] raw = digest.digest(); - StringBuilder hex = new StringBuilder(raw.length * 2); - for (byte b : raw) { - hex.append(String.format("%02x", b)); - } - - return hex.toString(); + return java.util.HexFormat.of().formatHex(raw); } catch (NoSuchAlgorithmException ex) { throw new IllegalStateException("SHA-256 digest unavailable", ex); } catch (IOException ex) {