You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by nf...@apache.org on 2026/07/21 09:36:46 UTC

(jackrabbit-oak) branch trunk updated: OAK-12325: Use heap based comparison of blob content (#3045)

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

nfsantos pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 44eff55bda OAK-12325: Use heap based comparison of blob content (#3045)
44eff55bda is described below

commit 44eff55bda2759bfe6ace0cf3ea7a2766c6a99b9
Author: Nuno Santos <ns...@adobe.com>
AuthorDate: Tue Jul 21 11:36:41 2026 +0200

    OAK-12325: Use heap based comparison of blob content (#3045)
---
 .../oak/plugins/memory/AbstractBlob.java           | 34 ++++++++++++++++++++--
 1 file changed, 31 insertions(+), 3 deletions(-)

diff --git a/oak-store-spi/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java b/oak-store-spi/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
index 32723973b1..a838a8bd54 100644
--- a/oak-store-spi/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
+++ b/oak-store-spi/src/main/java/org/apache/jackrabbit/oak/plugins/memory/AbstractBlob.java
@@ -19,12 +19,12 @@
 package org.apache.jackrabbit.oak.plugins.memory;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.math.BigInteger;
 import java.nio.ByteBuffer;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
-
-import org.apache.commons.io.IOUtils;
+import java.util.Arrays;
 
 import org.apache.jackrabbit.oak.api.Blob;
 import org.apache.jackrabbit.oak.commons.properties.SystemPropertySupplier;
@@ -75,12 +75,40 @@ public abstract class AbstractBlob implements Blob {
         }
 
         try {
-            return IOUtils.contentEquals(a.getNewStream(), b.getNewStream());
+            try (InputStream ais = a.getNewStream(); InputStream bis = b.getNewStream()) {
+                return contentEquals(ais, bis);
+            }
         } catch (IOException e) {
             throw new IllegalStateException("Blob equality check failed", e);
         }
     }
 
+    private static boolean contentEquals(InputStream is1, InputStream is2) throws IOException {
+        if (is1 == is2) {
+            return true;
+        }
+
+        byte[] buf1 = new byte[4096];
+        byte[] buf2 = new byte[4096];
+
+        while (true) {
+            // readNBytes handles "short reads" automatically, blocking until requested bytes or EOF
+            int read1 = is1.readNBytes(buf1, 0, buf1.length);
+            int read2 = is2.readNBytes(buf2, 0, buf2.length);
+
+            if (read1 != read2) {
+                return false; // Length mismatch
+            }
+            if (read1 == 0) {
+                return true; // Reached EOF on both streams identically
+            }
+            // Arrays.equals is highly optimized via CPU intrinsics
+            if (!Arrays.equals(buf1, 0, read1, buf2, 0, read2)) {
+                return false; // Content mismatch
+            }
+        }
+    }
+
     private ByteBuffer hashCode; // synchronized access
 
     protected AbstractBlob(ByteBuffer hashCode) {