diff --git a/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java b/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java index 54465d81..a91fbe65 100755 --- a/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java +++ b/Mustang-CLI/src/main/java/org/mustangproject/commandline/Main.java @@ -519,10 +519,11 @@ public class Main { ensureFileNotExists(outName); // All params are good! continue... - ZUGFeRDExporterFromA1 ze = new ZUGFeRDExporterFromA1().convertOnly().load(pdfName); - - ze.export(outName); - System.out.println("Written to " + outName); + try (ZUGFeRDExporterFromA1 ze = new ZUGFeRDExporterFromA1()) { + ze.convertOnly().load(pdfName); + ze.export(outName); + System.out.println("Written to " + outName); + } } private static void performExtract(String pdfName, String xmlName) throws IOException { @@ -842,13 +843,7 @@ public class Main { } else { zvi.toPDF(sourceName, outName); } - } catch (FileNotFoundException e) { - LOGGER.error(e.getMessage(), e); - } catch (UnsupportedEncodingException e) { - LOGGER.error(e.getMessage(), e); - } catch (TransformerException e) { - LOGGER.error(e.getMessage(), e); - } catch (IOException e) { + } catch (TransformerException | IOException e) { LOGGER.error(e.getMessage(), e); } System.out.println("Written to " + outName); @@ -876,11 +871,8 @@ public class Main { * @throws Exception e.g. if the specified resource does not exist at the specified location */ static public String ExportResource(String resourceName) throws Exception { - InputStream stream = null; - OutputStream resStreamOut = null; String jarFolder; - try { - stream = Main.class.getResourceAsStream(resourceName);//note that each / is a directory down in the "jar tree" been the jar the root of the tree + try (InputStream stream = Main.class.getResourceAsStream(resourceName)) {//note that each / is a directory down in the "jar tree" been the jar the root of the tree if (stream == null) { throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file."); } @@ -888,15 +880,11 @@ public class Main { int readBytes; byte[] buffer = new byte[4096]; jarFolder = System.getProperty("user.dir"); - resStreamOut = new FileOutputStream(jarFolder + resourceName); - while ((readBytes = stream.read(buffer)) > 0) { - resStreamOut.write(buffer, 0, readBytes); + try (FileOutputStream resStreamOut = new FileOutputStream(jarFolder + resourceName)) { + while ((readBytes = stream.read(buffer)) > 0) { + resStreamOut.write(buffer, 0, readBytes); + } } - } catch (Exception ex) { - throw ex; - } finally { - stream.close(); - resStreamOut.close(); } return jarFolder + resourceName; @@ -918,7 +906,8 @@ public class Main { if (fileName == null) return false; File f = new File(fileName); - return f.exists(); + // "exists" also returns true for directories + return f.isFile(); } } diff --git a/validator/src/main/java/org/mustangproject/validator/PDFValidator.java b/validator/src/main/java/org/mustangproject/validator/PDFValidator.java index 47a5cb0d..3ef9d09c 100644 --- a/validator/src/main/java/org/mustangproject/validator/PDFValidator.java +++ b/validator/src/main/java/org/mustangproject/validator/PDFValidator.java @@ -4,7 +4,6 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; -import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths;