diff options
author | PJ Fanning <fanningpj@apache.org> | 2017-11-23 20:03:25 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2017-11-23 20:03:25 +0000 |
commit | e0c8416726efa9ca12e0791aa89295c9e7964cde (patch) | |
tree | 4fb344e6046005d88413bab2c429fd221a82796a /src/ooxml | |
parent | 61cad2865eeb9dff5664ceb47a4e088fe8827b01 (diff) | |
download | poi-e0c8416726efa9ca12e0791aa89295c9e7964cde.tar.gz poi-e0c8416726efa9ca12e0791aa89295c9e7964cde.zip |
use try with resources in examples
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1816189 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml')
4 files changed, 6 insertions, 25 deletions
diff --git a/src/ooxml/java/org/apache/poi/poifs/crypt/temp/SXSSFWorkbookWithCustomZipEntrySource.java b/src/ooxml/java/org/apache/poi/poifs/crypt/temp/SXSSFWorkbookWithCustomZipEntrySource.java index 54600684ec..aeb35154a8 100644 --- a/src/ooxml/java/org/apache/poi/poifs/crypt/temp/SXSSFWorkbookWithCustomZipEntrySource.java +++ b/src/ooxml/java/org/apache/poi/poifs/crypt/temp/SXSSFWorkbookWithCustomZipEntrySource.java @@ -46,11 +46,8 @@ public class SXSSFWorkbookWithCustomZipEntrySource extends SXSSFWorkbook { EncryptedTempData tempData = new EncryptedTempData(); ZipEntrySource source = null; try { - OutputStream os = tempData.getOutputStream(); - try { + try (OutputStream os = tempData.getOutputStream()) { getXSSFWorkbook().write(os); - } finally { - IOUtils.closeQuietly(os); } // provide ZipEntrySource to poi which decrypts on the fly source = AesZipFileZipEntrySource.createZipEntrySource(tempData.getInputStream()); diff --git a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java index 5bbe155063..1a4c2cb1d5 100644 --- a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java +++ b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java @@ -250,15 +250,8 @@ public class WorkbookFactory { throw new FileNotFoundException(file.toString()); } - try { - NPOIFSFileSystem fs = new NPOIFSFileSystem(file, readOnly); - try { - return create(fs, password); - } catch (RuntimeException e) { - // ensure that the file-handle is closed again - IOUtils.closeQuietly(fs); - throw e; - } + try (NPOIFSFileSystem fs = new NPOIFSFileSystem(file, readOnly)) { + return create(fs, password); } catch(OfficeXmlFileException e) { // opening as .xls failed => try opening as .xlsx OPCPackage pkg = OPCPackage.open(file, readOnly ? PackageAccess.READ : PackageAccess.READ_WRITE); // NOSONAR diff --git a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java index e3ac6d22a1..b4f2024a24 100644 --- a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java +++ b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java @@ -87,14 +87,10 @@ public class XSSFBReader extends XSSFReader { * @throws IOException when there's a problem with the workbook part's stream */ public String getAbsPathMetadata() throws IOException { - InputStream is = null; - try { - is = workbookPart.getInputStream(); - PathExtractor p = new PathExtractor(workbookPart.getInputStream()); + try (InputStream is = workbookPart.getInputStream()) { + PathExtractor p = new PathExtractor(is); p.parse(); return p.getPath(); - } finally { - IOUtils.closeQuietly(is); } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFObjectData.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFObjectData.java index e54e25fd5f..c20d8d44cc 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFObjectData.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFObjectData.java @@ -171,14 +171,9 @@ public class XSSFObjectData extends XSSFSimpleShape implements ObjectData { } @Override - @SuppressWarnings("resource") public DirectoryEntry getDirectory() throws IOException { - InputStream is = null; - try { - is = getObjectPart().getInputStream(); + try (InputStream is = getObjectPart().getInputStream()) { return new POIFSFileSystem(is).getRoot(); - } finally { - IOUtils.closeQuietly(is); } } |