Merge pull request #776 from W25X80/feature_resource_leaks
Fix potential resource leaks in core file processing classes
This commit is contained in:
@@ -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) {
|
||||||
int DEFAULT_BUFFER_SIZE = 8192;
|
try (InputStream inputStream = is) {
|
||||||
ByteArrayOutputStream result = new ByteArrayOutputStream();
|
int DEFAULT_BUFFER_SIZE = 8192;
|
||||||
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
|
ByteArrayOutputStream result = new ByteArrayOutputStream();
|
||||||
int length;
|
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
|
||||||
try {
|
int length;
|
||||||
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;
|
||||||
|
// Java 10
|
||||||
|
// return result.toString(StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
// Java 10
|
|
||||||
// return result.toString(StandardCharsets.UTF_8);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
|
|||||||
@@ -220,7 +220,8 @@ public class XMLTools extends XMLWriter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] getBytesFromStream(InputStream fileinput) throws IOException {
|
public static byte[] getBytesFromStream(InputStream fileinput) throws IOException {
|
||||||
return IOUtils.toByteArray (fileinput);
|
// Stream closing responsibility is with the caller
|
||||||
|
return IOUtils.toByteArray(fileinput);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -90,9 +90,10 @@ 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/***
|
/***
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
@@ -142,8 +142,9 @@ 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)
|
||||||
@@ -233,12 +234,14 @@ public class ZUGFeRDVisualizer {
|
|||||||
|
|
||||||
protected String toFOP(String xmlFilename)
|
protected String toFOP(String xmlFilename)
|
||||||
throws IOException, TransformerException, ParserConfigurationException {
|
throws IOException, TransformerException, ParserConfigurationException {
|
||||||
|
EStandard theStandard;
|
||||||
FileInputStream fis = new FileInputStream(xmlFilename);
|
try (FileInputStream fis = new FileInputStream(xmlFilename)) {
|
||||||
EStandard theStandard = findOutStandardFromRootNode(fis);
|
theStandard = findOutStandardFromRootNode(fis);
|
||||||
fis = new FileInputStream(xmlFilename);//rewind :-(
|
}
|
||||||
|
|
||||||
return toFOP(fis, theStandard);
|
try (FileInputStream fis = new FileInputStream(xmlFilename)) {
|
||||||
|
return toFOP(fis, theStandard);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String toFOP(InputStream is, EStandard theStandard)
|
protected String toFOP(InputStream is, EStandard theStandard)
|
||||||
|
|||||||
@@ -312,6 +312,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());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user