]> source.dussan.org Git - poi.git/commitdiff
use try with resources in examples
authorPJ Fanning <fanningpj@apache.org>
Thu, 23 Nov 2017 20:03:25 +0000 (20:03 +0000)
committerPJ Fanning <fanningpj@apache.org>
Thu, 23 Nov 2017 20:03:25 +0000 (20:03 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1816189 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/poifs/crypt/temp/SXSSFWorkbookWithCustomZipEntrySource.java
src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFBReader.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFObjectData.java
src/scratchpad/src/org/apache/poi/hemf/record/HemfText.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlideShowEncrypted.java

index 54600684ecdd7eb2f2dad49dacd094713336ea9b..aeb35154a8b3a90800df0be5f780f2a1f04cad39 100644 (file)
@@ -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());
index 5bbe155063a3c04f8df688bab30b6318a5adba8f..1a4c2cb1d53ab5d38b203ce443899475315faeda 100644 (file)
@@ -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
index e3ac6d22a1396b89ecb89caf9ddc59c24fb78894..b4f2024a2409f72474e1aaad725258f565fc28a8 100644 (file)
@@ -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);
         }
     }
 
index e54e25fd5fdce4b0a8c2c0dc2f5ca2a36cce6b96..c20d8d44cce2762fb9e53de5e13a876b3230a210 100644 (file)
@@ -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);
         }
     }
 
index 87a7b4698850e176e4ff5206bd432a670a9ba898..6d3a1b297d4de8b4c5db6d6bfd2b738683223029 100644 (file)
@@ -230,14 +230,10 @@ public class HemfText {
 
         String getText(Charset charset) throws IOException {
             StringBuilder sb = new StringBuilder();
-            Reader r = null;
-            try {
-                r = new InputStreamReader(new ByteArrayInputStream(rawTextBytes), charset);
+            try (Reader r = new InputStreamReader(new ByteArrayInputStream(rawTextBytes), charset)) {
                 for (int i = 0; i < numChars; i++) {
                     sb.appendCodePoint(readCodePoint(r));
                 }
-            } finally {
-                IOUtils.closeQuietly(r);
             }
             return sb.toString();
         }
index f3495407b3b2d4e1cd6ff32ddc366980b94c2abf..eb9526469415863ff9e670569fc0bcd48e82bdf3 100644 (file)
@@ -179,10 +179,8 @@ public class HSLFSlideShowEncrypted implements Closeable {
 
         Decryptor dec = getEncryptionInfo().getDecryptor();
         dec.setChunkSize(-1);
-        LittleEndianByteArrayInputStream lei = new LittleEndianByteArrayInputStream(docstream, offset); // NOSONAR
-        ChunkedCipherInputStream ccis = null;
-        try {
-            ccis = (ChunkedCipherInputStream)dec.getDataStream(lei, docstream.length-offset, 0);
+        try (LittleEndianByteArrayInputStream lei = new LittleEndianByteArrayInputStream(docstream, offset);
+                ChunkedCipherInputStream ccis = (ChunkedCipherInputStream)dec.getDataStream(lei, docstream.length-offset, 0)) {
             ccis.initCipherForBlock(persistId);
 
             // decrypt header and read length to be decrypted
@@ -193,9 +191,6 @@ public class HSLFSlideShowEncrypted implements Closeable {
 
         } catch (Exception e) {
             throw new EncryptedPowerPointFileException(e);
-        } finally {
-            IOUtils.closeQuietly(ccis);
-            IOUtils.closeQuietly(lei);
         }
     }
 
@@ -288,10 +283,9 @@ public class HSLFSlideShowEncrypted implements Closeable {
             return;
         }
 
-        LittleEndianByteArrayOutputStream los = new LittleEndianByteArrayOutputStream(pictstream, offset); // NOSONAR
         ChunkedCipherOutputStream ccos = null;
 
-        try {
+        try (LittleEndianByteArrayOutputStream los = new LittleEndianByteArrayOutputStream(pictstream, offset)) {
             Encryptor enc = getEncryptionInfo().getEncryptor();
             enc.setChunkSize(-1);
             ccos = enc.getDataStream(los, 0);
@@ -362,7 +356,6 @@ public class HSLFSlideShowEncrypted implements Closeable {
             throw new EncryptedPowerPointFileException(e);
         } finally {
             IOUtils.closeQuietly(ccos);
-            IOUtils.closeQuietly(los);
         }
     }