Merge pull request #776 from W25X80/feature_resource_leaks

Fix potential resource leaks in core file processing classes
This commit is contained in:
Jochen Staerk
2025-03-25 19:17:55 +01:00
committed by GitHub
6 changed files with 31 additions and 25 deletions

View File

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

View File

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

View File

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

View File

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

View File

@@ -142,8 +142,9 @@ public class ZUGFeRDVisualizer {
public String visualize(String xmlFilename, Language lang)
throws IOException, TransformerException, ParserConfigurationException {
FileInputStream fis = new FileInputStream(xmlFilename);
return visualize(fis, lang);
try (FileInputStream fis = new FileInputStream(xmlFilename)) {
return visualize(fis, lang);
}
}
public String visualize(InputStream inputXml, Language lang)
@@ -233,12 +234,14 @@ public class ZUGFeRDVisualizer {
protected String toFOP(String xmlFilename)
throws IOException, TransformerException, ParserConfigurationException {
FileInputStream fis = new FileInputStream(xmlFilename);
EStandard theStandard = findOutStandardFromRootNode(fis);
fis = new FileInputStream(xmlFilename);//rewind :-(
return toFOP(fis, theStandard);
EStandard theStandard;
try (FileInputStream fis = new FileInputStream(xmlFilename)) {
theStandard = findOutStandardFromRootNode(fis);
}
try (FileInputStream fis = new FileInputStream(xmlFilename)) {
return toFOP(fis, theStandard);
}
}
protected String toFOP(InputStream is, EStandard theStandard)

View File

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