This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch v3
in repository https://gitbox.apache.org/repos/asf/causeway.git


The following commit(s) were added to refs/heads/v3 by this push:
     new 580b1b7437 CAUSEWAY-3702: fixes byte[] hex-dump utility
580b1b7437 is described below

commit 580b1b7437764b9a9173019c2d78b9d3b93eb10e
Author: Andi Huber <[email protected]>
AuthorDate: Tue Mar 26 17:01:30 2024 +0100

    CAUSEWAY-3702: fixes byte[] hex-dump utility
    
    - also adds Blob#sha256Hex for convenience
---
 .../src/main/java/org/apache/causeway/applib/value/Blob.java | 10 ++++++----
 .../org/apache/causeway/commons/internal/base/_Bytes.java    | 12 ++++++++----
 .../org/apache/causeway/commons/internal/base/BytesTest.java |  9 +++++++++
 3 files changed, 23 insertions(+), 8 deletions(-)

diff --git 
a/api/applib/src/main/java/org/apache/causeway/applib/value/Blob.java 
b/api/applib/src/main/java/org/apache/causeway/applib/value/Blob.java
index 52b44d2397..60f279490f 100644
--- a/api/applib/src/main/java/org/apache/causeway/applib/value/Blob.java
+++ b/api/applib/src/main/java/org/apache/causeway/applib/value/Blob.java
@@ -274,12 +274,14 @@ public final class Blob implements NamedWithMimeType {
         return HashUtils.tryDigest(hashAlgorithm, bytes, 4*1024); // 4k default
     }
 
-    public Try<HashUtils.Hash> tryMd5() {
-        return tryHash(HashAlgorithm.MD5);
+    public String md5Hex() {
+        return tryHash(HashAlgorithm.MD5)
+                .valueAsNonNullElseFail()
+                .asHexString();
     }
 
-    public String md5Hex() {
-        return tryMd5()
+    public String sha256Hex() {
+        return tryHash(HashAlgorithm.SHA256)
                 .valueAsNonNullElseFail()
                 .asHexString();
     }
diff --git 
a/commons/src/main/java/org/apache/causeway/commons/internal/base/_Bytes.java 
b/commons/src/main/java/org/apache/causeway/commons/internal/base/_Bytes.java
index 3007942f76..cba2480b47 100644
--- 
a/commons/src/main/java/org/apache/causeway/commons/internal/base/_Bytes.java
+++ 
b/commons/src/main/java/org/apache/causeway/commons/internal/base/_Bytes.java
@@ -115,7 +115,7 @@ public final class _Bytes {
             return IntStream.empty();
         }
         return IntStream.range(0, bytes.length)
-                .map(index->(int)bytes[index]);
+                .map(index->(bytes[index] & 0xff));
     }
 
     /**
@@ -123,10 +123,10 @@ public final class _Bytes {
      * while the element wise type conversion is preserving sign,
      * but ignoring overflow.
      * @apiNote The Java byte type is signed.
-     * @implNote certainly not the most sufficient algorithm, as we resort to 
boxing and temporary list creation
+     * @implNote certainly not the most efficient algorithm, as we resort to 
boxing and temporary list creation
      * @see #streamAsInts(byte[])
      */
-    public static byte[] ofIntStream(final @Nullable IntStream intStream) {
+    private static byte[] ofIntStream(final @Nullable IntStream intStream) {
         if(intStream==null) {
             return new byte[0];
         }
@@ -152,7 +152,11 @@ public final class _Bytes {
         if(bytes==null) {
             return "";
         }
-        return _Bytes.streamAsInts(bytes).mapToObj(Integer::toHexString)
+        return _Bytes.streamAsInts(bytes)
+                .mapToObj(Integer::toHexString)
+                .map(s->s.length()==1
+                    ? "0" + s
+                    : s)
                 .collect(Collectors.joining(_Strings.nullToEmpty(delimiter)));
     }
 
diff --git 
a/commons/src/test/java/org/apache/causeway/commons/internal/base/BytesTest.java
 
b/commons/src/test/java/org/apache/causeway/commons/internal/base/BytesTest.java
index f64b059b4d..8bce7584e6 100644
--- 
a/commons/src/test/java/org/apache/causeway/commons/internal/base/BytesTest.java
+++ 
b/commons/src/test/java/org/apache/causeway/commons/internal/base/BytesTest.java
@@ -29,6 +29,7 @@ import org.junit.jupiter.params.provider.ValueSource;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.lessThan;
 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertNull;
 
@@ -173,6 +174,14 @@ class BytesTest {
                         _Bytes.encodeToBase64(Base64.getUrlEncoder(), 
testimonial)));
     }
 
+    // -- HEX
+
+    @Test
+    void hexDump(){
+        final byte[] bytes = {Byte.MIN_VALUE, -1, 0, 1, Byte.MAX_VALUE};
+        assertEquals("80 ff 00 01 7f", _Bytes.hexDump(bytes));
+    }
+
     // -- OPERATOR COMPOSITION
 
     @Test

Reply via email to