davsclaus commented on code in PR #17780:
URL: https://github.com/apache/camel/pull/17780#discussion_r2047196657


##########
core/camel-support/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java:
##########
@@ -50,9 +50,11 @@ public static StreamCache 
convertToStreamCache(ByteArrayInputStream stream, Exch
     public static StreamCache convertToStreamCache(InputStream stream, 
Exchange exchange) throws IOException {
         // transfer the input stream to a cached output stream, and then 
creates a new stream cache view
         // of the data, which ensures the input stream is cached and 
re-readable.
-        CachedOutputStream cos = new CachedOutputStream(exchange);
-        IOHelper.copyAndCloseInput(stream, cos);
-        return cos.newStreamCache();
+        // Use try-with-resources to ensure the CachedOutputStream is closed 
automatically
+        try (CachedOutputStream cos = new CachedOutputStream(exchange)) {

Review Comment:
   This is wrong as COS should not be closed - there is some special code for 
stream cahcing when its based on file disk and whatnot.
   
   



##########
core/camel-support/src/main/java/org/apache/camel/support/InputStreamIterator.java:
##########
@@ -65,11 +67,18 @@ public int available() throws IOException {
 
     private InputStream nextChunk() throws IOException {
         if (it.hasNext()) {
+            Object next = null;
             try {
-                byte[] buf = converter.mandatoryConvertTo(byte[].class, 
it.next());
+                next = it.next();
+                byte[] buf = converter.mandatoryConvertTo(byte[].class, next);
                 return new ByteArrayInputStream(buf);
             } catch (NoTypeConversionAvailableException e) {
                 throw new IOException(e);
+            } finally {
+                // ensure the original object is closed if it was closeable
+                if (next != null && next instanceof Closeable closeableNext) {

Review Comment:
   This is also wrong as it.next can be part of some object that should only be 
closed when its fully done. 



##########
core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java:
##########
@@ -497,22 +497,16 @@ private static void loadLines(InputStream in, Set<String> 
lines, Function<String
     private String doGetVersion() {
         String version = null;
 
-        InputStream is = null;
         // try to load from maven properties first
-        try {
-            Properties p = new Properties();
-            is = MainHelper.class
-                    
.getResourceAsStream("/META-INF/maven/org.apache.camel/camel-main/pom.properties");
+        try (InputStream is = MainHelper.class
+                
.getResourceAsStream("/META-INF/maven/org.apache.camel/camel-main/pom.properties"))
 {
             if (is != null) {
+                Properties p = new Properties();
                 p.load(is);
                 version = p.getProperty("version", "");
             }
         } catch (Exception e) {
-            // ignore
-        } finally {
-            if (is != null) {
-                IOHelper.close(is);
-            }
+            LOG.warn("Error loading version information from pom.properties: 
{}", e.getMessage(), e);

Review Comment:
   The old code ignores this so make it the same



-- 
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: commits-unsubscr...@camel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to