This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 37df508a385e CAMEL-22847: camel-core - Optimize simple hash function
to calc hash … (#20796)
37df508a385e is described below
commit 37df508a385ef2422b824aa7cf19c3480aca2b9d
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Jan 13 13:03:42 2026 +0100
CAMEL-22847: camel-core - Optimize simple hash function to calc hash …
(#20796)
* CAMEL-22847: camel-core - Optimize simple hash function to calc hash with
low memory
---
.../language/simple/SimpleExpressionBuilder.java | 23 ++++++++++++++++++----
.../apache/camel/language/simple/SimpleTest.java | 22 +++++++++++++++++++++
.../main/java/org/apache/camel/util/IOHelper.java | 4 ++--
3 files changed, 43 insertions(+), 6 deletions(-)
diff --git
a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java
b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java
index 02f807b828f9..fcbe0675b366 100644
---
a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java
+++
b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleExpressionBuilder.java
@@ -16,6 +16,10 @@
*/
package org.apache.camel.language.simple;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
@@ -52,6 +56,7 @@ import org.apache.camel.support.ShortUuidGenerator;
import org.apache.camel.support.SimpleUuidGenerator;
import org.apache.camel.support.builder.ExpressionBuilder;
import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
import org.apache.camel.util.OgnlHelper;
import org.apache.camel.util.SkipIterator;
@@ -559,14 +564,24 @@ public final class SimpleExpressionBuilder {
@Override
public Object evaluate(Exchange exchange) {
- byte[] data = exp.evaluate(exchange, byte[].class);
- if (data != null && data.length > 0) {
+ InputStream is = exp.evaluate(exchange, InputStream.class);
+ if (is != null) {
try {
+ // calculate the hash in chunks in case the payload is
big
MessageDigest digest =
MessageDigest.getInstance(algorithm);
- byte[] bytes = digest.digest(data);
- return StringHelper.bytesToHex(bytes);
+ DigestInputStream dis = new DigestInputStream(is,
digest);
+ IOHelper.copy(dis, new OutputStream() {
+ @Override
+ public void write(int b) throws IOException {
+ // ignore
+ }
+ });
+ return StringHelper.bytesToHex(digest.digest());
} catch (Exception e) {
throw
CamelExecutionException.wrapCamelExecutionException(exchange, e);
+ } finally {
+ // reset cached streams so they can be read again
+ MessageHelper.resetStreamCache(exchange.getMessage());
}
}
return null;
diff --git
a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
index ecb88e21b8fa..553f38e64241 100644
---
a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
+++
b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
@@ -16,6 +16,7 @@
*/
package org.apache.camel.language.simple;
+import java.io.File;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.ArrayList;
@@ -37,7 +38,9 @@ import org.apache.camel.ExpressionIllegalSyntaxException;
import org.apache.camel.InvalidPayloadException;
import org.apache.camel.LanguageTestSupport;
import org.apache.camel.Predicate;
+import org.apache.camel.StreamCache;
import org.apache.camel.component.bean.MethodNotFoundException;
+import org.apache.camel.converter.stream.FileInputStreamCache;
import org.apache.camel.language.bean.RuntimeBeanExpressionException;
import org.apache.camel.language.simple.myconverter.MyCustomDate;
import org.apache.camel.language.simple.types.SimpleIllegalSyntaxException;
@@ -2397,6 +2400,25 @@ public class SimpleTest extends LanguageTestSupport {
assertNull(s);
}
+ @Test
+ public void testHashStreamCache() throws Exception {
+ File f = new File("src/test/resources/log4j2.properties");
+ StreamCache sc = new FileInputStreamCache(f);
+ assertEquals(-1, sc.position());
+ exchange.getMessage().setBody(sc);
+ Expression expression =
context.resolveLanguage("simple").createExpression("${hash(${body})}");
+ String s = expression.evaluate(exchange, String.class);
+ assertNotNull(s);
+ // should reset so we can read it again
+ assertEquals(-1, sc.position());
+
+ MessageDigest digest = MessageDigest.getInstance("SHA-256");
+ byte[] arr = context.getTypeConverter().convertTo(byte[].class, f);
+ byte[] bytes = digest.digest(arr);
+ String expected = StringHelper.bytesToHex(bytes);
+ assertEquals(expected, s);
+ }
+
@Test
public void testTrim() {
exchange.getMessage().setBody(" Hello World ");
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
b/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
index 58e2f74a3396..e5f74deed6cb 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java
@@ -274,7 +274,7 @@ public final class IOHelper {
*
* @param input the input stream buffer
* @param output the output stream buffer
- * @throws IOException
+ * @throws IOException for I/O errors
*/
public static void copyAndCloseInput(InputStream input, OutputStream
output) throws IOException {
copy(input, output);
@@ -289,7 +289,7 @@ public final class IOHelper {
* @param input the input stream buffer
* @param output the output stream buffer
* @param bufferSize the size of the buffer used for the copies
- * @throws IOException
+ * @throws IOException for I/O errors
*/
public static void copyAndCloseInput(InputStream input, OutputStream
output, int bufferSize) throws IOException {
copy(input, output, bufferSize);