This is an automated email from the ASF dual-hosted git repository.

oscerd 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 d347583a37ff CAMEL-24530: camel-djl - close the InputStream opened 
from File/Path bodies in DJLConverter (#25852)
d347583a37ff is described below

commit d347583a37ff84389717e41d358b4281a6568456
Author: Andrea Cosentino <[email protected]>
AuthorDate: Fri Aug 28 07:19:13 2026 +0200

    CAMEL-24530: camel-djl - close the InputStream opened from File/Path bodies 
in DJLConverter (#25852)
    
    Close the InputStream that DJLConverter opens from File/Path message 
bodies, fixing a file-handle leak.
    
    Co-authored-by: Claude Opus 4.8 <[email protected]>
---
 .../apache/camel/component/djl/DJLConverter.java   | 16 +++++--
 .../camel/component/djl/DJLConverterTest.java      | 49 ++++++++++++++++++++++
 2 files changed, 61 insertions(+), 4 deletions(-)

diff --git 
a/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/DJLConverter.java
 
b/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/DJLConverter.java
index 1a841cde1745..8bcd6c895916 100644
--- 
a/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/DJLConverter.java
+++ 
b/components/camel-ai/camel-djl/src/main/java/org/apache/camel/component/djl/DJLConverter.java
@@ -48,12 +48,16 @@ public class DJLConverter {
 
     @Converter
     public static Image toImage(File file) throws IOException {
-        return toImage(new FileInputStream(file));
+        try (InputStream inputStream = new FileInputStream(file)) {
+            return toImage(inputStream);
+        }
     }
 
     @Converter
     public static Image toImage(Path path) throws IOException {
-        return toImage(Files.newInputStream(path));
+        try (InputStream inputStream = Files.newInputStream(path)) {
+            return toImage(inputStream);
+        }
     }
 
     @Converter
@@ -105,12 +109,16 @@ public class DJLConverter {
 
     @Converter
     public static Audio toAudio(File file) throws IOException {
-        return toAudio(new FileInputStream(file));
+        try (InputStream inputStream = new FileInputStream(file)) {
+            return toAudio(inputStream);
+        }
     }
 
     @Converter
     public static Audio toAudio(Path path) throws IOException {
-        return toAudio(Files.newInputStream(path));
+        try (InputStream inputStream = Files.newInputStream(path)) {
+            return toAudio(inputStream);
+        }
     }
 
     @Converter
diff --git 
a/components/camel-ai/camel-djl/src/test/java/org/apache/camel/component/djl/DJLConverterTest.java
 
b/components/camel-ai/camel-djl/src/test/java/org/apache/camel/component/djl/DJLConverterTest.java
new file mode 100644
index 000000000000..19e74069b0ef
--- /dev/null
+++ 
b/components/camel-ai/camel-djl/src/test/java/org/apache/camel/component/djl/DJLConverterTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.component.djl;
+
+import java.io.File;
+import java.nio.file.Path;
+
+import ai.djl.modality.cv.Image;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class DJLConverterTest {
+
+    private static final String IMAGE_RESOURCE = "/data/detect/kitten.jpg";
+
+    // Converting from a File opens a stream internally; it must be closed 
(try-with-resources) so the
+    // file descriptor is not leaked per conversion. The conversion must still 
return a valid Image.
+    @Test
+    void toImageFromFileReturnsImageWithoutLeakingStream() throws Exception {
+        File file = new 
File(DJLConverterTest.class.getResource(IMAGE_RESOURCE).toURI());
+        Image image = DJLConverter.toImage(file);
+        assertNotNull(image);
+        assertTrue(image.getWidth() > 0 && image.getHeight() > 0);
+    }
+
+    @Test
+    void toImageFromPathReturnsImageWithoutLeakingStream() throws Exception {
+        Path path = 
Path.of(DJLConverterTest.class.getResource(IMAGE_RESOURCE).toURI());
+        Image image = DJLConverter.toImage(path);
+        assertNotNull(image);
+        assertTrue(image.getWidth() > 0 && image.getHeight() > 0);
+    }
+}

Reply via email to