You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2025/02/05 05:53:22 UTC

(camel) branch main updated: CAMEL-21711: camel-core - IOConverter file/path to string should handle binary files (#17050)

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 10d483ef9ea CAMEL-21711: camel-core - IOConverter file/path to string should handle binary files (#17050)
10d483ef9ea is described below

commit 10d483ef9eaecf54c777d490ae908198797063ec
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Feb 5 06:53:17 2025 +0100

    CAMEL-21711: camel-core - IOConverter file/path to string should handle binary files (#17050)
---
 .../main/java/org/apache/camel/converter/IOConverter.java   | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/core/camel-base/src/main/java/org/apache/camel/converter/IOConverter.java b/core/camel-base/src/main/java/org/apache/camel/converter/IOConverter.java
index 7256877bb7f..a1e9b66edcb 100644
--- a/core/camel-base/src/main/java/org/apache/camel/converter/IOConverter.java
+++ b/core/camel-base/src/main/java/org/apache/camel/converter/IOConverter.java
@@ -39,6 +39,7 @@ import java.io.StringReader;
 import java.io.Writer;
 import java.net.URL;
 import java.nio.ByteBuffer;
+import java.nio.charset.Charset;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Iterator;
@@ -177,7 +178,17 @@ public final class IOConverter {
 
     @Converter(order = 22)
     public static String toString(Path file, Exchange exchange) throws IOException {
-        return Files.readString(file, ExchangeHelper.getCharset(exchange));
+        Charset cs = ExchangeHelper.getCharset(exchange, false);
+        if (cs != null) {
+            return Files.readString(file, cs);
+        } else {
+            byte[] arr = Files.readAllBytes(file);
+            if (arr.length == 0) {
+                return "";
+            } else {
+                return new String(arr);
+            }
+        }
     }
 
     @Converter(order = 23)