Fix resource leaks in core file processing classes

- Add try-with-resources for FileInputStream in ZUGFeRDVisualizer
- Fix unclosed InputStream in ZUGFeRDInvoiceImporter with try-with-resources
- Add explicit close() call for XMLWriter in ZUGFeRDValidator
- Properly close DataInputStream in ZUGFeRDExporterFromPDFA
- Fix resource management in Main.convertInputStreamToString
This commit is contained in:
Ivan Pereverziev
2025-03-13 11:26:35 +01:00
parent 5e511acf3c
commit 7fa2d70128
6 changed files with 31 additions and 25 deletions

View File

@@ -307,12 +307,13 @@ public class Main {
// Plain Java // Plain Java
// based on https://mkyong.com/java/how-to-convert-inputstream-to-string-in-java/ // based on https://mkyong.com/java/how-to-convert-inputstream-to-string-in-java/
private static String convertInputStreamToString(InputStream is) { private static String convertInputStreamToString(InputStream is) {
try (InputStream inputStream = is) {
int DEFAULT_BUFFER_SIZE = 8192; int DEFAULT_BUFFER_SIZE = 8192;
ByteArrayOutputStream result = new ByteArrayOutputStream(); ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length; int length;
try {
while ((length = is.read(buffer)) != -1) { while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length); result.write(buffer, 0, length);
} }
@@ -320,11 +321,10 @@ public class Main {
return result.toString(StandardCharsets.UTF_8.name()); return result.toString(StandardCharsets.UTF_8.name());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
}
return null; return null;
// Java 10 // Java 10
// return result.toString(StandardCharsets.UTF_8); // return result.toString(StandardCharsets.UTF_8);
}
} }
/*** /***

View File

@@ -220,6 +220,7 @@ public class XMLTools extends XMLWriter {
} }
public static byte[] getBytesFromStream(InputStream fileinput) throws IOException { public static byte[] getBytesFromStream(InputStream fileinput) throws IOException {
// Stream closing responsibility is with the caller
return IOUtils.toByteArray(fileinput); return IOUtils.toByteArray(fileinput);
} }

View File

@@ -90,10 +90,11 @@ public class ZUGFeRDExporterFromPDFA implements IZUGFeRDExporter {
protected byte[] inputstreamToByteArray(InputStream fileInputStream) throws IOException { protected byte[] inputstreamToByteArray(InputStream fileInputStream) throws IOException {
byte[] bytes = new byte[fileInputStream.available()]; byte[] bytes = new byte[fileInputStream.available()];
DataInputStream dataInputStream = new DataInputStream(fileInputStream); try (DataInputStream dataInputStream = new DataInputStream(fileInputStream)) {
dataInputStream.readFully(bytes); dataInputStream.readFully(bytes);
return bytes; return bytes;
} }
}
/*** /***
* *

View File

@@ -138,9 +138,9 @@ public class ZUGFeRDInvoiceImporter {
return; return;
} }
final InputStream XMP = doc.getDocumentCatalog().getMetadata().exportXMPMetadata(); try (final InputStream XMP = doc.getDocumentCatalog().getMetadata().exportXMPMetadata()) {
xmpString = new String(XMLTools.getBytesFromStream(XMP), StandardCharsets.UTF_8); xmpString = new String(XMLTools.getBytesFromStream(XMP), StandardCharsets.UTF_8);
}
final PDEmbeddedFilesNameTreeNode etn = names.getEmbeddedFiles(); final PDEmbeddedFilesNameTreeNode etn = names.getEmbeddedFiles();
if (etn == null) { if (etn == null) {

View File

@@ -131,9 +131,10 @@ public class ZUGFeRDVisualizer {
public String visualize(String xmlFilename, Language lang) public String visualize(String xmlFilename, Language lang)
throws IOException, TransformerException, ParserConfigurationException { throws IOException, TransformerException, ParserConfigurationException {
FileInputStream fis = new FileInputStream(xmlFilename); try (FileInputStream fis = new FileInputStream(xmlFilename)) {
return visualize(fis, lang); return visualize(fis, lang);
} }
}
public String visualize(InputStream inputXml, Language lang) public String visualize(InputStream inputXml, Language lang)
throws IOException, TransformerException, ParserConfigurationException { throws IOException, TransformerException, ParserConfigurationException {
@@ -222,13 +223,15 @@ public class ZUGFeRDVisualizer {
protected String toFOP(String xmlFilename) protected String toFOP(String xmlFilename)
throws IOException, TransformerException, ParserConfigurationException { throws IOException, TransformerException, ParserConfigurationException {
EStandard theStandard;
try (FileInputStream fis = new FileInputStream(xmlFilename)) {
theStandard = findOutStandardFromRootNode(fis);
}
FileInputStream fis = new FileInputStream(xmlFilename); try (FileInputStream fis = new FileInputStream(xmlFilename)) {
EStandard theStandard = findOutStandardFromRootNode(fis);
fis = new FileInputStream(xmlFilename);//rewind :-(
return toFOP(fis, theStandard); return toFOP(fis, theStandard);
} }
}
protected String toFOP(InputStream is, EStandard theStandard) protected String toFOP(InputStream is, EStandard theStandard)
throws TransformerException, IOException { throws TransformerException, IOException {

View File

@@ -301,6 +301,7 @@ public class ZUGFeRDValidator {
XMLWriter writer = new XMLWriter(sw, format); XMLWriter writer = new XMLWriter(sw, format);
try { try {
writer.write(document); writer.write(document);
writer.close();
} catch (Exception e) { } catch (Exception e) {
LOGGER.error(e.getMessage()); LOGGER.error(e.getMessage());
} }