davsclaus commented on code in PR #27132: URL: https://github.com/apache/camel/pull/27132#discussion_r4146726258
########## 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: `ReaderCache.length()` and `SourceCache.length()` return a character count, and `StreamSourceCache` does too when it wraps a reader. Callers here use the result as a byte length: S3, minio, gcs and cos call `getLength(body)` before converting to `InputStream`. So a `ReaderCache` body with non-ASCII text gets a too-short `contentLength`, and the upload is truncated. I reproduced this: 3000 chars of `æøå` give `contentLength=3000` with a 6000-byte stream in both `AWS2S3Producer` and `BlobStreamAndLength`. On `main` this was correct, because the length came from the converted `ByteArrayInputStream`. Only trust the length of stream caches that are byte streams: ```suggestion } else if (value instanceof StreamCache cache && value instanceof InputStream && cache.length() > 0) { ``` ########## 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: `Charset.encode()` creates a new encoder on every call. For `UTF-16` (and `x-UTF-16LE-BOM` etc.) each call writes a BOM, so every 4096-char chunk starts with `FE FF`. An 8000-char stream body sent to sftp/ftp/smb with `charset=UTF-16` is written as 16004 bytes instead of the 16002 that the previous `getBytes(charset)` produced. Stateful encoders (ISO-2022-JP) are also reset at each chunk. Could you keep a single `CharsetEncoder` for the whole stream? Create it with `charset.newEncoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE)`, call `encoder.encode(in, out, endOfInput)` per chunk, and call `encoder.flush(out)` at the end. That produces the same bytes as the previous whole-String encoding. A >4096-char UTF-16 case in `GenericFileHelperTest` would cover it. ########## 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: `NIOConverter.toInputStream(ByteBuffer)` copies from index 0 to `limit()`, and ignores `position()`. When `position() > 0`, this length does not match the bytes that are uploaded. Returning `-1` here (or `limit()`) keeps the two consistent. -- 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]
