diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2019-11-30 23:00:08 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2019-11-30 23:00:08 +0000 |
commit | b4b661acc1538933aba0b9aecb9c86dfbf81fe12 (patch) | |
tree | ba7aebda4e7c49b8a78a6d60d8313a87e203c63c /src/java | |
parent | 4f8aeb705ef1ce252201646e4aa5a89312f8be5f (diff) | |
download | poi-b4b661acc1538933aba0b9aecb9c86dfbf81fe12.tar.gz poi-b4b661acc1538933aba0b9aecb9c86dfbf81fe12.zip |
Sonar fixes - use try-with-resources
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1870653 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java')
7 files changed, 57 insertions, 69 deletions
diff --git a/src/java/org/apache/poi/hssf/dev/BiffDrawingToXml.java b/src/java/org/apache/poi/hssf/dev/BiffDrawingToXml.java index 0a9f78b0c3..3fc3f5d3be 100644 --- a/src/java/org/apache/poi/hssf/dev/BiffDrawingToXml.java +++ b/src/java/org/apache/poi/hssf/dev/BiffDrawingToXml.java @@ -123,42 +123,40 @@ public class BiffDrawingToXml { return; } String input = getInputFileName(params); - FileInputStream inp = new FileInputStream(input); String output = getOutputFileName(input); - FileOutputStream outputStream = new FileOutputStream(output); - writeToFile(outputStream, inp, isExcludeWorkbookRecords(params), params); - inp.close(); - outputStream.close(); + try (FileInputStream inp = new FileInputStream(input); + FileOutputStream outputStream = new FileOutputStream(output)) { + writeToFile(outputStream, inp, isExcludeWorkbookRecords(params), params); + } } public static void writeToFile(OutputStream fos, InputStream xlsWorkbook, boolean excludeWorkbookRecords, String[] params) throws IOException { - HSSFWorkbook workbook = new HSSFWorkbook(xlsWorkbook); - InternalWorkbook internalWorkbook = workbook.getInternalWorkbook(); - DrawingGroupRecord r = (DrawingGroupRecord) internalWorkbook.findFirstRecordBySid(DrawingGroupRecord.sid); + try (HSSFWorkbook workbook = new HSSFWorkbook(xlsWorkbook)) { + InternalWorkbook internalWorkbook = workbook.getInternalWorkbook(); + DrawingGroupRecord r = (DrawingGroupRecord) internalWorkbook.findFirstRecordBySid(DrawingGroupRecord.sid); - StringBuilder builder = new StringBuilder(); - builder.append("<workbook>\n"); - String tab = "\t"; - if (!excludeWorkbookRecords && r != null) { - r.decode(); - List<EscherRecord> escherRecords = r.getEscherRecords(); - for (EscherRecord record : escherRecords) { - builder.append(record.toXml(tab)); + StringBuilder builder = new StringBuilder(); + builder.append("<workbook>\n"); + String tab = "\t"; + if (!excludeWorkbookRecords && r != null) { + r.decode(); + List<EscherRecord> escherRecords = r.getEscherRecords(); + for (EscherRecord record : escherRecords) { + builder.append(record.toXml(tab)); + } } - } - List<Integer> sheets = getSheetsIndexes(params, workbook); - for (Integer i : sheets) { - HSSFPatriarch p = workbook.getSheetAt(i).getDrawingPatriarch(); - if(p != null ) { - builder.append(tab).append("<sheet").append(i).append(">\n"); - builder.append(p.getBoundAggregate().toXml(tab + "\t")); - builder.append(tab).append("</sheet").append(i).append(">\n"); + List<Integer> sheets = getSheetsIndexes(params, workbook); + for (Integer i : sheets) { + HSSFPatriarch p = workbook.getSheetAt(i).getDrawingPatriarch(); + if (p != null) { + builder.append(tab).append("<sheet").append(i).append(">\n"); + builder.append(p.getBoundAggregate().toXml(tab + "\t")); + builder.append(tab).append("</sheet").append(i).append(">\n"); + } } + builder.append("</workbook>\n"); + fos.write(builder.toString().getBytes(StringUtil.UTF8)); } - builder.append("</workbook>\n"); - fos.write(builder.toString().getBytes(StringUtil.UTF8)); - fos.close(); - workbook.close(); } } diff --git a/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java b/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java index 55c32fb4f5..ec40f097f1 100644 --- a/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java +++ b/src/java/org/apache/poi/hssf/extractor/ExcelExtractor.java @@ -223,24 +223,17 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p return; } - InputStream is; - if(cmdArgs.getInputFile() == null) { - is = System.in; - } else { - is = new FileInputStream(cmdArgs.getInputFile()); + try (InputStream is = cmdArgs.getInputFile() == null ? System.in : new FileInputStream(cmdArgs.getInputFile()); + HSSFWorkbook wb = new HSSFWorkbook(is); + ExcelExtractor extractor = new ExcelExtractor(wb); + ) { + extractor.setIncludeSheetNames(cmdArgs.shouldShowSheetNames()); + extractor.setFormulasNotResults(!cmdArgs.shouldEvaluateFormulas()); + extractor.setIncludeCellComments(cmdArgs.shouldShowCellComments()); + extractor.setIncludeBlankCells(cmdArgs.shouldShowBlankCells()); + extractor.setIncludeHeadersFooters(cmdArgs.shouldIncludeHeadersFooters()); + System.out.println(extractor.getText()); } - HSSFWorkbook wb = new HSSFWorkbook(is); - is.close(); - - ExcelExtractor extractor = new ExcelExtractor(wb); - extractor.setIncludeSheetNames(cmdArgs.shouldShowSheetNames()); - extractor.setFormulasNotResults(!cmdArgs.shouldEvaluateFormulas()); - extractor.setIncludeCellComments(cmdArgs.shouldShowCellComments()); - extractor.setIncludeBlankCells(cmdArgs.shouldShowBlankCells()); - extractor.setIncludeHeadersFooters(cmdArgs.shouldIncludeHeadersFooters()); - System.out.println(extractor.getText()); - extractor.close(); - wb.close(); } @Override diff --git a/src/java/org/apache/poi/poifs/crypt/standard/StandardDecryptor.java b/src/java/org/apache/poi/poifs/crypt/standard/StandardDecryptor.java index 67da57129a..9c7926033d 100644 --- a/src/java/org/apache/poi/poifs/crypt/standard/StandardDecryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/standard/StandardDecryptor.java @@ -121,7 +121,7 @@ public class StandardDecryptor extends Decryptor implements Cloneable { } @Override - @SuppressWarnings("resource") + @SuppressWarnings({"resource", "squid:S2095"}) public InputStream getDataStream(DirectoryNode dir) throws IOException { DocumentInputStream dis = dir.createDocumentInputStream(DEFAULT_POIFS_ENTRY); diff --git a/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java b/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java index 7de19b9a7b..e3662b40ba 100644 --- a/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java @@ -127,7 +127,7 @@ public class StandardEncryptor extends Encryptor implements Cloneable { protected final File fileOut; protected final DirectoryNode dir; - @SuppressWarnings("resource") + @SuppressWarnings({"resource", "squid:S2095"}) private StandardCipherOutputStream(DirectoryNode dir, File fileOut) throws IOException { // although not documented, we need the same padding as with agile encryption // and instead of calculating the missing bytes for the block size ourselves diff --git a/src/java/org/apache/poi/poifs/dev/POIFSLister.java b/src/java/org/apache/poi/poifs/dev/POIFSLister.java index 5ed20c75ab..f9696bccfb 100644 --- a/src/java/org/apache/poi/poifs/dev/POIFSLister.java +++ b/src/java/org/apache/poi/poifs/dev/POIFSLister.java @@ -62,16 +62,15 @@ public class POIFSLister { } public static void viewFile(final String filename, boolean withSizes) throws IOException { - POIFSFileSystem fs = new POIFSFileSystem(new File(filename)); - displayDirectory(fs.getRoot(), "", withSizes); - fs.close(); + try (POIFSFileSystem fs = new POIFSFileSystem(new File(filename))) { + displayDirectory(fs.getRoot(), "", withSizes); + } } public static void viewFileOld(final String filename, boolean withSizes) throws IOException { - try (FileInputStream fis = new FileInputStream(filename)) { - POIFSFileSystem fs = new POIFSFileSystem(fis); + try (FileInputStream fis = new FileInputStream(filename); + POIFSFileSystem fs = new POIFSFileSystem(fis)) { displayDirectory(fs.getRoot(), "", withSizes); - fs.close(); } } diff --git a/src/java/org/apache/poi/poifs/filesystem/Ole10Native.java b/src/java/org/apache/poi/poifs/filesystem/Ole10Native.java index 57084e6391..81481d3f65 100644 --- a/src/java/org/apache/poi/poifs/filesystem/Ole10Native.java +++ b/src/java/org/apache/poi/poifs/filesystem/Ole10Native.java @@ -107,13 +107,11 @@ public class Ole10Native { * @throws Ole10NativeException on invalid or unexcepted data format */ public static Ole10Native createFromEmbeddedOleObject(DirectoryNode directory) throws IOException, Ole10NativeException { - DocumentEntry nativeEntry = - (DocumentEntry)directory.getEntry(OLE10_NATIVE); - byte[] data = IOUtils.safelyAllocate(nativeEntry.getSize(), MAX_RECORD_LENGTH); - int readBytes = directory.createDocumentInputStream(nativeEntry).read(data); - assert(readBytes == data.length); - - return new Ole10Native(data, 0); + DocumentEntry nativeEntry = (DocumentEntry)directory.getEntry(OLE10_NATIVE); + try (DocumentInputStream dis = directory.createDocumentInputStream(nativeEntry)) { + byte[] data = IOUtils.toByteArray(dis, nativeEntry.getSize(), MAX_RECORD_LENGTH); + return new Ole10Native(data, 0); + } } /** diff --git a/src/java/org/apache/poi/poifs/macros/VBAMacroExtractor.java b/src/java/org/apache/poi/poifs/macros/VBAMacroExtractor.java index 57a7b1486f..9874270a9d 100644 --- a/src/java/org/apache/poi/poifs/macros/VBAMacroExtractor.java +++ b/src/java/org/apache/poi/poifs/macros/VBAMacroExtractor.java @@ -78,10 +78,11 @@ public class VBAMacroExtractor { } else { System.err.println("STDOUT"); } - - VBAMacroReader reader = new VBAMacroReader(input); - Map<String,String> macros = reader.readMacros(); - reader.close(); + + final Map<String,String> macros; + try (VBAMacroReader reader = new VBAMacroReader(input)) { + macros = reader.readMacros(); + } final String divider = "---------------------------------------"; for (Entry<String, String> entry : macros.entrySet()) { @@ -94,11 +95,10 @@ public class VBAMacroExtractor { System.out.println(moduleCode); } else { File out = new File(outputDir, moduleName + extension); - FileOutputStream fout = new FileOutputStream(out); - OutputStreamWriter fwriter = new OutputStreamWriter(fout, StringUtil.UTF8); - fwriter.write(moduleCode); - fwriter.close(); - fout.close(); + try (FileOutputStream fout = new FileOutputStream(out); + OutputStreamWriter fwriter = new OutputStreamWriter(fout, StringUtil.UTF8)) { + fwriter.write(moduleCode); + } System.out.println("Extracted " + out); } } |