diff --git a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/invoice/InvoiceServices.java b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/invoice/InvoiceServices.java index 9a1f5a66d5..cb967cc76f 100644 --- a/applications/accounting/src/main/java/org/apache/ofbiz/accounting/invoice/InvoiceServices.java +++ b/applications/accounting/src/main/java/org/apache/ofbiz/accounting/invoice/InvoiceServices.java @@ -3672,8 +3672,7 @@ public class InvoiceServices { String organizationPartyId = (String) context.get("organizationPartyId"); String encoding = System.getProperty("file.encoding"); String csvString = Charset.forName(encoding).decode(fileBytes).toString(); - final BufferedReader csvReader = new BufferedReader(new StringReader(csvString)); - CSVFormat fmt = CSVFormat.DEFAULT.withHeader(); + CSVFormat fmt = CSVFormat.DEFAULT; List errMsgs = new LinkedList<>(); List newErrMsgs; String lastInvoiceId = null; @@ -3681,7 +3680,7 @@ public class InvoiceServices { String newInvoiceId = null; int invoicesCreated = 0; - try { + try (BufferedReader csvReader = new BufferedReader(new StringReader(csvString))) { for (final CSVRecord rec : fmt.parse(csvReader)) { currentInvoiceId = rec.get("invoiceId"); if (lastInvoiceId == null || !currentInvoiceId.equals(lastInvoiceId)) { @@ -3758,11 +3757,18 @@ public class InvoiceServices { try { invoiceResult = dispatcher.runSync("createInvoice", invoice); if (ServiceUtil.isError(invoiceResult)) { + // Eclipse reports here: Resource leak: '' is not closed at this location + // but it's OK. As csvReader is in a try-with-ressource it will be closed anyway + // I prefer to not put @SuppressWarnings("resource") to the whole method + // BTW to be consistent Eclipse should also reports the same issue in PartyService (see there) return ServiceUtil.returnError(ServiceUtil.getErrorMessage(invoiceResult)); } } catch (GenericServiceException e) { - csvReader.close(); Debug.logError(e, MODULE); + // Eclipse reports here: Resource leak: '' is not closed at this location + // but it's OK. As csvReader is in a try-with-ressource it will be closed anyway + // I prefer to not put @SuppressWarnings("resource") to the whole method + // BTW to be consistent Eclipse should also reports the same issue in PartyService (see there) return ServiceUtil.returnError(e.getMessage()); } newInvoiceId = (String) invoiceResult.get("invoiceId"); @@ -3824,10 +3830,13 @@ public class InvoiceServices { try { Map result = dispatcher.runSync("createInvoiceItem", invoiceItem); if (ServiceUtil.isError(result)) { + // Eclipse reports here: Resource leak: '' is not closed at this location + // but it's OK. As csvReader is in a try-with-ressource it will be closed anyway + // I prefer to not put @SuppressWarnings("resource") to the whole method + // BTW to be consistent Eclipse should also reports the same issue in PartyService (see there) return ServiceUtil.returnError(ServiceUtil.getErrorMessage(result)); } } catch (GenericServiceException e) { - csvReader.close(); Debug.logError(e, MODULE); return ServiceUtil.returnError(e.getMessage()); } diff --git a/applications/party/src/main/java/org/apache/ofbiz/party/party/PartyServices.java b/applications/party/src/main/java/org/apache/ofbiz/party/party/PartyServices.java index 574d931ac4..466c2a325a 100644 --- a/applications/party/src/main/java/org/apache/ofbiz/party/party/PartyServices.java +++ b/applications/party/src/main/java/org/apache/ofbiz/party/party/PartyServices.java @@ -2303,7 +2303,7 @@ public class PartyServices { ByteBuffer fileBytes = (ByteBuffer) context.get("uploadedFile"); String encoding = System.getProperty("file.encoding"); String csvString = Charset.forName(encoding).decode(fileBytes).toString(); - CSVFormat fmt = CSVFormat.DEFAULT.withHeader(); + CSVFormat fmt = CSVFormat.DEFAULT; List errMsgs = new LinkedList<>(); List newErrMsgs = new LinkedList<>(); String lastPartyId = null; // last partyId read from the csv file @@ -2333,7 +2333,7 @@ public class PartyServices { try (BufferedReader csvReader = new BufferedReader(new StringReader(csvString))) { - for (final CSVRecord rec : fmt.parse(csvReader)) { + for (CSVRecord rec : fmt.parse(csvReader)) { if (UtilValidate.isNotEmpty(rec.get("partyId"))) { currentPartyId = rec.get("partyId"); }