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 cb20ae3db42f CAMEL-25574: camel-zip-deflater - The gzip unmarshal does 
not move the mark of the message body (#27758)
cb20ae3db42f is described below

commit cb20ae3db42f5db5b250ced48abce574f08924a3
Author: allthingssecurity <[email protected]>
AuthorDate: Tue Oct 13 11:44:32 2026 +0530

    CAMEL-25574: camel-zip-deflater - The gzip unmarshal does not move the mark 
of the message body (#27758)
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../deflater/GzipDeflaterDataFormat.java           |   6 +-
 .../deflater/GzipDataFormatBodyReadAgainTest.java  | 123 +++++++++++++++++++++
 2 files changed, 128 insertions(+), 1 deletion(-)

diff --git 
a/components/camel-zip-deflater/src/main/java/org/apache/camel/dataformat/deflater/GzipDeflaterDataFormat.java
 
b/components/camel-zip-deflater/src/main/java/org/apache/camel/dataformat/deflater/GzipDeflaterDataFormat.java
index a426d4cec0e4..78bd5ae099a1 100644
--- 
a/components/camel-zip-deflater/src/main/java/org/apache/camel/dataformat/deflater/GzipDeflaterDataFormat.java
+++ 
b/components/camel-zip-deflater/src/main/java/org/apache/camel/dataformat/deflater/GzipDeflaterDataFormat.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.dataformat.deflater;
 
+import java.io.BufferedInputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
 
@@ -59,8 +60,11 @@ public class GzipDeflaterDataFormat extends ServiceSupport 
implements DataFormat
 
         OutputStreamBuilder osb = OutputStreamBuilder.withExchange(exchange);
         try {
+            // The gzip reader marks and resets a stream that supports mark, 
and uses it as it is. The stream may be the
+            // message body (a stream cache), which goes back to its last mark 
when it is reset to be read again, so
+            // give the reader a stream of its own to mark.
             unzipInput = GzipCompressorInputStream.builder()
-                    .setInputStream(inputStream)
+                    .setInputStream(new BufferedInputStream(inputStream))
                     .setDecompressConcatenated(true)
                     .get();
             IOHelper.copy(unzipInput, osb);
diff --git 
a/components/camel-zip-deflater/src/test/java/org/apache/camel/dataformat/deflater/GzipDataFormatBodyReadAgainTest.java
 
b/components/camel-zip-deflater/src/test/java/org/apache/camel/dataformat/deflater/GzipDataFormatBodyReadAgainTest.java
new file mode 100644
index 000000000000..8b3cc0529d21
--- /dev/null
+++ 
b/components/camel-zip-deflater/src/test/java/org/apache/camel/dataformat/deflater/GzipDataFormatBodyReadAgainTest.java
@@ -0,0 +1,123 @@
+/*
+ * 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.dataformat.deflater;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Random;
+import java.util.zip.GZIPOutputStream;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+
+/**
+ * The gzip body is a stream, which stream caching (on by default) keeps in 
memory so that it can be read again after
+ * the unmarshal: by a dead letter channel that uses the original message, or 
by a later step of the route.
+ */
+class GzipDataFormatBodyReadAgainTest extends CamelTestSupport {
+
+    @Test
+    void deadLetterChannelGetsTheWholeOriginalBody() throws Exception {
+        byte[] gzip = gzip("Hello World".getBytes(StandardCharsets.UTF_8));
+        MockEndpoint dead = getMockEndpoint("mock:dead");
+        dead.expectedMessageCount(1);
+
+        template.sendBody("direct:failing", new ByteArrayInputStream(gzip));
+
+        dead.assertIsSatisfied();
+        assertArrayEquals(gzip, 
dead.getReceivedExchanges().get(0).getIn().getBody(byte[].class));
+    }
+
+    @Test
+    void deadLetterChannelGetsTheWholeOriginalBodyOfALargeMessage() throws 
Exception {
+        // random bytes do not compress: the gzip body is larger than the read 
buffer of the gzip reader
+        byte[] data = new byte[20000];
+        new Random(42).nextBytes(data);
+        byte[] gzip = gzip(data);
+        MockEndpoint dead = getMockEndpoint("mock:dead");
+        dead.expectedMessageCount(1);
+
+        template.sendBody("direct:failing", new ByteArrayInputStream(gzip));
+
+        dead.assertIsSatisfied();
+        assertArrayEquals(gzip, 
dead.getReceivedExchanges().get(0).getIn().getBody(byte[].class));
+    }
+
+    @Test
+    void theCompressedBodyKeptInAPropertyCanBeReadAfterTheUnmarshal() throws 
Exception {
+        byte[] gzip = gzip("Hello World".getBytes(StandardCharsets.UTF_8));
+        MockEndpoint archive = getMockEndpoint("mock:archive");
+        archive.expectedMessageCount(1);
+
+        // not a ByteArrayInputStream, so the stream is copied into the stream 
cache
+        template.sendBody("direct:archive", new InputStream() {
+            private final InputStream delegate = new 
ByteArrayInputStream(gzip);
+
+            @Override
+            public int read() throws IOException {
+                return delegate.read();
+            }
+
+            @Override
+            public int read(byte[] b, int off, int len) throws IOException {
+                return delegate.read(b, off, len);
+            }
+        });
+
+        archive.assertIsSatisfied();
+        assertArrayEquals(gzip, 
archive.getReceivedExchanges().get(0).getIn().getBody(byte[].class));
+    }
+
+    private static byte[] gzip(byte[] data) throws Exception {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        try (GZIPOutputStream gzip = new GZIPOutputStream(bos)) {
+            gzip.write(data);
+        }
+        return bos.toByteArray();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                from("direct:failing")
+                        
.errorHandler(deadLetterChannel("direct:dead").useOriginalMessage())
+                        .unmarshal().gzipDeflater()
+                        .throwException(new IllegalStateException("Forced"));
+
+                from("direct:dead")
+                        .convertBodyTo(byte[].class)
+                        .to("mock:dead");
+
+                from("direct:archive")
+                        .setProperty("compressed", body())
+                        .unmarshal().gzipDeflater()
+                        .setBody(exchangeProperty("compressed"))
+                        .convertBodyTo(byte[].class)
+                        .to("mock:archive");
+            }
+        };
+    }
+}

Reply via email to