}
public void writeToFile(IssuesReport report, File toFile, boolean complete) {
- Writer writer = null;
- FileOutputStream fos = null;
try {
freemarker.log.Logger.selectLoggerLibrary(freemarker.log.Logger.LIBRARY_NONE);
freemarker.template.Configuration cfg = new freemarker.template.Configuration();
root.put("complete", complete);
Template template = cfg.getTemplate("issuesreport.ftl");
- fos = new FileOutputStream(toFile);
- writer = new OutputStreamWriter(fos, fs.encoding());
- template.process(root, writer);
- writer.flush();
+ try (FileOutputStream fos = new FileOutputStream(toFile); Writer writer = new OutputStreamWriter(fos, fs.encoding())) {
+ template.process(root, writer);
+ writer.flush();
+ }
} catch (Exception e) {
throw new IllegalStateException("Fail to generate HTML Issues Report to: " + toFile, e);
- } finally {
- IOUtils.closeQuietly(writer);
- IOUtils.closeQuietly(fos);
}
}
}
private void copyDependency(File target, String filename) {
- InputStream input = null;
- OutputStream output = null;
- try {
- input = getClass().getResourceAsStream("/org/sonar/batch/scan/report/issuesreport_files/" + filename);
- output = new FileOutputStream(new File(target, filename));
+ try (InputStream input = getClass().getResourceAsStream("/org/sonar/batch/scan/report/issuesreport_files/" + filename);
+ OutputStream output = new FileOutputStream(new File(target, filename))) {
IOUtils.copy(input, output);
-
} catch (IOException e) {
throw new IllegalStateException("Fail to copy file " + filename + " to " + target, e);
- } finally {
- IOUtils.closeQuietly(input);
- IOUtils.closeQuietly(output);
}
}
return true;
}
- }
-
- private static String ofNullableBytes(@Nullable byte[] shortBytes, @Nullable byte[] longBytes) {
- byte[] result;
- if (shortBytes == null) {
- if (longBytes == null) {
- return "";
+
+ private static String ofNullableBytes(@Nullable byte[] shortBytes, @Nullable byte[] longBytes) {
+ byte[] result;
+ if (shortBytes == null) {
+ if (longBytes == null) {
+ return "";
+ } else {
+ result = longBytes;
+ }
} else {
- result = longBytes;
+ result = shortBytes;
}
- } else {
- result = shortBytes;
+ return new String(result, StandardCharsets.UTF_8);
}
- return new String(result, StandardCharsets.UTF_8);
}
@Override