Croway commented on code in PR #27132:
URL: https://github.com/apache/camel/pull/27132#discussion_r4147192314


##########
core/camel-support/src/main/java/org/apache/camel/support/PayloadHelper.java:
##########
@@ -0,0 +1,121 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.support;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.StreamCache;
+import org.apache.camel.WrappedFile;
+import org.apache.camel.support.builder.OutputStreamBuilder;
+import org.apache.camel.util.IOHelper;
+
+/**
+ * Helper for components that need to know the length of a (possibly big) 
message body, for example to upload it.
+ * <p/>
+ * The length is determined without reading the body. When the length cannot 
be determined that way, the body can be
+ * copied with {@link #cacheStream(Exchange, InputStream)}, which uses stream 
caching and therefore spools big payloads
+ * to disk when spooling is enabled, instead of loading them into memory.
+ */
+public final class PayloadHelper {
+
+    private PayloadHelper() {
+    }
+
+    /**
+     * Gets the length of the message body, without reading the body.
+     *
+     * @param  message the message
+     * @return         the length in bytes, or <tt>-1</tt> if the length 
cannot be determined without reading the body
+     */
+    public static long getBodyLength(Message message) {
+        Object body = message.getBody();
+        if (body instanceof WrappedFile<?> wf) {
+            long length = wf.getFileLength();
+            if (length > 0) {
+                return length;
+            }
+            Object file = wf.getFile();
+            body = file instanceof File || file instanceof Path ? file : 
wf.getBody();
+        }
+        return getLength(body);
+    }
+
+    /**
+     * Gets the length of the given value, without reading it.
+     * <p/>
+     * The length is known for files, byte arrays and buffers, stream caches, 
and input streams backed by a byte array
+     * or a file.
+     *
+     * @param  value the value, such as a message body or an input stream
+     * @return       the length in bytes, or <tt>-1</tt> if the length cannot 
be determined without reading the value
+     */
+    public static long getLength(Object value) {
+        try {
+            if (value instanceof File file) {
+                return file.isFile() ? file.length() : -1;
+            } else if (value instanceof Path path) {
+                return Files.isRegularFile(path) ? Files.size(path) : -1;
+            } else if (value instanceof byte[] bytes) {
+                return bytes.length;
+            } else if (value instanceof ByteBuffer buffer) {
+                return buffer.remaining();
+            } else if (value instanceof StreamCache cache && cache.length() > 
0) {

Review Comment:
   Thanks, confirmed and fixed in 5c6cb2a22375. `getLength` now only trusts a 
`StreamCache` that is also an `InputStream`, so `ReaderCache`, `SourceCache` 
and a reader-backed `StreamSourceCache` return `-1`, and the length is taken 
from the converted stream as on `main`. Added 
`AWS2S3ProducerPayloadLengthTest#uploadReaderCacheBodyWithNonAsciiText` (3000 
chars of `æøå`): on the previous commit it fails with `expected: <6000> but 
was: <3000>`.
   
   _Claude Code on behalf of Federico Mariani (Croway)_



##########
core/camel-support/src/main/java/org/apache/camel/support/PayloadHelper.java:
##########
@@ -0,0 +1,121 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.support;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.StreamCache;
+import org.apache.camel.WrappedFile;
+import org.apache.camel.support.builder.OutputStreamBuilder;
+import org.apache.camel.util.IOHelper;
+
+/**
+ * Helper for components that need to know the length of a (possibly big) 
message body, for example to upload it.
+ * <p/>
+ * The length is determined without reading the body. When the length cannot 
be determined that way, the body can be
+ * copied with {@link #cacheStream(Exchange, InputStream)}, which uses stream 
caching and therefore spools big payloads
+ * to disk when spooling is enabled, instead of loading them into memory.
+ */
+public final class PayloadHelper {
+
+    private PayloadHelper() {
+    }
+
+    /**
+     * Gets the length of the message body, without reading the body.
+     *
+     * @param  message the message
+     * @return         the length in bytes, or <tt>-1</tt> if the length 
cannot be determined without reading the body
+     */
+    public static long getBodyLength(Message message) {
+        Object body = message.getBody();
+        if (body instanceof WrappedFile<?> wf) {
+            long length = wf.getFileLength();
+            if (length > 0) {
+                return length;
+            }
+            Object file = wf.getFile();
+            body = file instanceof File || file instanceof Path ? file : 
wf.getBody();
+        }
+        return getLength(body);
+    }
+
+    /**
+     * Gets the length of the given value, without reading it.
+     * <p/>
+     * The length is known for files, byte arrays and buffers, stream caches, 
and input streams backed by a byte array
+     * or a file.
+     *
+     * @param  value the value, such as a message body or an input stream
+     * @return       the length in bytes, or <tt>-1</tt> if the length cannot 
be determined without reading the value
+     */
+    public static long getLength(Object value) {
+        try {
+            if (value instanceof File file) {
+                return file.isFile() ? file.length() : -1;
+            } else if (value instanceof Path path) {
+                return Files.isRegularFile(path) ? Files.size(path) : -1;
+            } else if (value instanceof byte[] bytes) {
+                return bytes.length;
+            } else if (value instanceof ByteBuffer buffer) {
+                return buffer.remaining();

Review Comment:
   Agreed. I dropped the `ByteBuffer` case, so it returns `-1` and the length 
comes from the converted `ByteArrayInputStream`, which is what is uploaded 
(5c6cb2a22375).
   
   _Claude Code on behalf of Federico Mariani (Croway)_



##########
core/camel-util/src/main/java/org/apache/camel/util/IOHelper.java:
##########
@@ -769,9 +796,9 @@ public int read() throws IOException {
                     pendingHighSurrogate = bufferedChars.get(limit - 1);
                     bufferedChars.limit(limit - 1);
                 }
-                bufferBytes = defaultStreamCharset.encode(bufferedChars);
+                bufferBytes = charset.encode(bufferedChars);

Review Comment:
   Fixed in 5c6cb2a22375 as suggested: one `CharsetEncoder` for the whole 
stream, with `REPLACE` for malformed and unmappable input, `encode(chars, 
bytes, endOfInput)` per chunk and `flush` at the end. The encoder also keeps an 
unfinished surrogate pair for the next chunk, so the manual surrogate handling 
is gone; the existing 
`UtilEdgeCasesTest#testEncodingInputStreamWithSurrogatePairAtBufferEnd` still 
passes. Added 
`GenericFileHelperTest#shouldEncodeStreamBodyWithCharsetThatWritesByteOrderMark`
 (8000 chars to `UTF-16`): on the previous commit it fails with `expected: 
<16002> but was: <16004>`. Note that `EncodingInputStream` (the file consumer 
with `charset`) was built on the same per-chunk `Charset.encode` on `main`, so 
this also fixes the repeated BOM there for files over 4096 chars when the JVM 
default charset writes one.
   
   _Claude Code on behalf of Federico Mariani (Croway)_



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to