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

mridulpathak pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 25481a06ee Close the URL stream when a RecordIterator cannot be created
25481a06ee is described below

commit 25481a06ee9d094256a36d9322edff11401be116
Author: Mridul Pathak <[email protected]>
AuthorDate: Fri Oct 30 10:18:23 2026 +0530

    Close the URL stream when a RecordIterator cannot be created
    
    When RecordIterator was created from a URL and reading the start of the 
file failed, the constructor threw without closing the stream it had opened. 
The exceptions for an unsupported encoding and for a failed read of the start 
lines also dropped the underlying exception. The URL constructor now closes the 
stream before rethrowing, and both exceptions keep their cause.
---
 .../java/org/apache/ofbiz/datafile/RecordIterator.java   | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git 
a/framework/datafile/src/main/java/org/apache/ofbiz/datafile/RecordIterator.java
 
b/framework/datafile/src/main/java/org/apache/ofbiz/datafile/RecordIterator.java
index e7ba1c1459..8ec4cacae9 100644
--- 
a/framework/datafile/src/main/java/org/apache/ofbiz/datafile/RecordIterator.java
+++ 
b/framework/datafile/src/main/java/org/apache/ofbiz/datafile/RecordIterator.java
@@ -64,7 +64,17 @@ public class RecordIterator {
         } catch (IOException e) {
             throw new DataFileException("Error open URL: " + 
fileUrl.toString(), e);
         }
-        this.setupStream(urlStream, fileUrl.toString());
+        try {
+            this.setupStream(urlStream, fileUrl.toString());
+        } catch (DataFileException | RuntimeException e) {
+            // the stream was opened here, so close it when the iterator 
cannot be created
+            try {
+                urlStream.close();
+            } catch (IOException closeException) {
+                e.addSuppressed(closeException);
+            }
+            throw e;
+        }
     }
 
     public RecordIterator(InputStream dataFileStream, ModelDataFile 
modelDataFile, String locationInfo) throws DataFileException {
@@ -84,7 +94,7 @@ public class RecordIterator {
         try {
             this.br = new BufferedReader(new InputStreamReader(dataFileStream, 
Charset.forName(charsetStr)));
         } catch (Exception e) {
-            throw new DataFileException(charsetStr + " is not supported");
+            throw new DataFileException(charsetStr + " is not supported", e);
         }
         //move the cursor to the good start line
         try {
@@ -92,7 +102,7 @@ public class RecordIterator {
                 br.readLine();
             }
         } catch (IOException e) {
-            throw new DataFileException("Impossible to read the buffer");
+            throw new DataFileException("Impossible to read the buffer", e);
         }
         // get the line seeded
         this.getNextLine();

Reply via email to