From 49b7fd53420269ae8c7cd87d8c19660dbf67ef23 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 5 Dec 2017 21:23:44 +0000 Subject: [PATCH] use try-with-resources in more places git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1817238 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/ss/extractor/EmbeddedExtractor.java | 13 ++++++------- .../poi/xssf/extractor/XSSFExcelExtractor.java | 8 +++----- .../org/apache/poi/xssf/usermodel/XSSFWorkbook.java | 10 ++++------ 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java b/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java index f598dc0066..3ae7453888 100644 --- a/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java +++ b/src/ooxml/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java @@ -204,17 +204,16 @@ public class EmbeddedExtractor implements Iterable { @Override public boolean canExtract(DirectoryNode dn) { ClassID clsId = dn.getStorageClsid(); - return (PdfClassID.equals(clsId) - || dn.hasEntry("CONTENTS")); + return (PdfClassID.equals(clsId) || dn.hasEntry("CONTENTS")); } @Override public EmbeddedData extract(DirectoryNode dn) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - InputStream is = dn.createDocumentInputStream("CONTENTS"); - IOUtils.copy(is, bos); - is.close(); - return new EmbeddedData(dn.getName() + ".pdf", bos.toByteArray(), CONTENT_TYPE_PDF); + try(ByteArrayOutputStream bos = new ByteArrayOutputStream(); + InputStream is = dn.createDocumentInputStream("CONTENTS")) { + IOUtils.copy(is, bos); + return new EmbeddedData(dn.getName() + ".pdf", bos.toByteArray(), CONTENT_TYPE_PDF); + } } @Override diff --git a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java index 81b93ab463..9ec618f90e 100644 --- a/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java +++ b/src/ooxml/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java @@ -74,12 +74,10 @@ public class XSSFExcelExtractor extends POIXMLTextExtractor System.err.println(" XSSFExcelExtractor "); System.exit(1); } - OPCPackage pkg = OPCPackage.create(args[0]); - POIXMLTextExtractor extractor = new XSSFExcelExtractor(pkg); - try { + + try (OPCPackage pkg = OPCPackage.create(args[0]); + POIXMLTextExtractor extractor = new XSSFExcelExtractor(pkg)) { System.out.println(extractor.getText()); - } finally { - extractor.close(); } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 01db019b82..1a027fea00 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -517,10 +517,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { public int addPicture(byte[] pictureData, int format) { int imageNumber = getAllPictures().size() + 1; XSSFPictureData img = createRelationship(XSSFPictureData.RELATIONS[format], XSSFFactory.getInstance(), imageNumber, true).getDocumentPart(); - try { - OutputStream out = img.getPackagePart().getOutputStream(); + try (OutputStream out = img.getPackagePart().getOutputStream()) { out.write(pictureData); - out.close(); } catch (IOException e){ throw new POIXMLException(e); } @@ -1745,9 +1743,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook { xmlOptions.setSaveSyntheticDocumentElement(new QName(CTWorkbook.type.getName().getNamespaceURI(), "workbook")); PackagePart part = getPackagePart(); - OutputStream out = part.getOutputStream(); - workbook.save(out, xmlOptions); - out.close(); + try (OutputStream out = part.getOutputStream()) { + workbook.save(out, xmlOptions); + } } /**