]> source.dussan.org Git - poi.git/commitdiff
use more nio file support
authorPJ Fanning <fanningpj@apache.org>
Thu, 14 Sep 2023 15:49:10 +0000 (15:49 +0000)
committerPJ Fanning <fanningpj@apache.org>
Thu, 14 Sep 2023 15:49:10 +0000 (15:49 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1912316 13f79535-47bb-0310-9956-ffa450edef68

22 files changed:
poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/OPCPackage.java
poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/ZipPackage.java
poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/TempFilePackagePart.java
poi-ooxml/src/main/java/org/apache/poi/openxml4j/opc/internal/ZipHelper.java
poi-ooxml/src/main/java/org/apache/poi/openxml4j/util/ZipArchiveFakeEntry.java
poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/EncryptedTempData.java
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java
poi-ooxml/src/test/java/org/apache/poi/xssf/XSSFTestDataSamples.java
poi/src/main/java/org/apache/poi/hssf/extractor/ExcelExtractor.java
poi/src/main/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
poi/src/main/java/org/apache/poi/hssf/usermodel/StaticFontMetrics.java
poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java
poi/src/main/java/org/apache/poi/poifs/dev/POIFSDump.java
poi/src/main/java/org/apache/poi/poifs/filesystem/FileMagic.java
poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
poi/src/main/java/org/apache/poi/util/HexRead.java
poi/src/test/java/org/apache/poi/POIDataSamples.java
poi/src/test/java/org/apache/poi/hssf/dev/BiffViewer.java
poi/src/test/java/org/apache/poi/hssf/usermodel/StreamUtility.java
poi/src/test/java/org/apache/poi/ss/formula/function/ExcelCetabFunctionExtractor.java
poi/src/test/java/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java
poi/src/test/java/org/apache/poi/ss/util/NumberRenderingSpreadsheetGenerator.java

index 35f3c10c019c17be055dccf940df4f6316e7da0a..2c79feea4e0e8e8db72f2c66ea7a07d719783354 100644 (file)
@@ -25,13 +25,13 @@ import static org.apache.poi.openxml4j.opc.PackagingURIHelper.RELATIONSHIP_PART_
 import java.io.ByteArrayOutputStream;
 import java.io.Closeable;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
@@ -506,7 +506,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
         }
         String name = path.substring(path.lastIndexOf(File.separatorChar) + 1);
 
-        try (FileInputStream is = new FileInputStream(path)) {
+        try (InputStream is = Files.newInputStream(Paths.get(path))) {
             addThumbnail(name, is);
         }
     }
@@ -1483,7 +1483,7 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
         }
 
         // Do the save
-        try (FileOutputStream fos = new FileOutputStream(targetFile)) {
+        try (OutputStream fos = Files.newOutputStream(targetFile.toPath())) {
             this.save(fos);
         }
     }
index cc714e94654b30988e729f9261327c4fcd58b109..21894df27059b6ddd3c0a445f26f9d51c5b665ab 100644 (file)
@@ -21,11 +21,10 @@ import static org.apache.poi.openxml4j.opc.ContentTypes.RELATIONSHIPS_PART;
 import static org.apache.poi.openxml4j.opc.internal.ContentTypeManager.CONTENT_TYPES_PART_NAME;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.nio.file.Files;
 import java.util.Collections;
 import java.util.Enumeration;
 import java.util.List;
@@ -179,12 +178,12 @@ public final class ZipPackage extends OPCPackage {
     }
 
     private static ZipEntrySource openZipEntrySourceStream(File file) throws InvalidOperationException {
-        final FileInputStream fis;
+        final InputStream fis;
         // Acquire a resource that is needed to read the next level of openZipEntrySourceStream
         try {
             // open the file input stream
-            fis = new FileInputStream(file); // NOSONAR
-        } catch (final FileNotFoundException e) {
+            fis = Files.newInputStream(file.toPath());
+        } catch (final IOException e) {
             // If the source cannot be acquired, abort (no resources to free at this level)
             throw new InvalidOperationException("Can't open the specified file input stream from file: '" + file + "'", e);
         }
@@ -204,7 +203,7 @@ public final class ZipPackage extends OPCPackage {
         }
     }
 
-    private static ZipEntrySource openZipEntrySourceStream(FileInputStream fis) throws InvalidOperationException {
+    private static ZipEntrySource openZipEntrySourceStream(InputStream fis) throws InvalidOperationException {
         final ZipArchiveThresholdInputStream zis;
         // Acquire a resource that is needed to read the next level of openZipEntrySourceStream
         try {
index d1a3e3fd807002819c7307654ef3b67ca5b5861a..600851722a62f8eafb6402de54390d24843376e9 100644 (file)
@@ -30,6 +30,7 @@ import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.TempFile;
 
 import java.io.*;
+import java.nio.file.Files;
 
 /**
  * (Experimental) Temp File version of a package part.
@@ -89,12 +90,12 @@ public final class TempFilePackagePart extends PackagePart {
 
     @Override
     protected InputStream getInputStreamImpl() throws IOException {
-        return new FileInputStream(tempFile);
+        return Files.newInputStream(tempFile.toPath());
     }
 
     @Override
     protected OutputStream getOutputStreamImpl() throws IOException {
-        return new FileOutputStream(tempFile);
+        return Files.newOutputStream(tempFile.toPath());
     }
 
     @Override
index 674850f6e1ed1c013df9b80a8319df2fe836c156..26f3cffae1c5cc710711f3a71df4b2cd361a1214 100644 (file)
@@ -24,6 +24,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.nio.file.Files;
 
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
 import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
@@ -197,7 +198,7 @@ public final class ZipHelper {
         }
         
         // Peek at the first few bytes to sanity check
-        try (FileInputStream input = new FileInputStream(file)) {
+        try (InputStream input = Files.newInputStream(file.toPath())) {
             verifyZipHeader(input);
         }
 
index 7a74f0747297247503458ab65a60def490aacf09..edb8817bed3da05c49a37f5f02d7a6bb82592560 100644 (file)
@@ -18,6 +18,7 @@
 package org.apache.poi.openxml4j.util;
 
 import java.io.*;
+import java.nio.file.Files;
 
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
 import org.apache.commons.io.input.UnsynchronizedByteArrayInputStream;
@@ -99,7 +100,7 @@ import org.apache.poi.util.TempFile;
             }
         } else if (tempFile != null) {
             try {
-                return new FileInputStream(tempFile);
+                return Files.newInputStream(tempFile.toPath());
             } catch (FileNotFoundException e) {
                 throw new IOException("temp file " + tempFile.getAbsolutePath() + " is missing");
             }
index 45b994798148bedc80c678b7d161f70e5dad979a..d5cc96585d483b29e5e951a60ba1eb282f02856d 100644 (file)
 package org.apache.poi.poifs.crypt.temp;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.nio.file.Files;
 
 import javax.crypto.Cipher;
 import javax.crypto.CipherInputStream;
@@ -73,7 +72,7 @@ public class EncryptedTempData {
      */
     public OutputStream getOutputStream() throws IOException {
         Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, PADDING);
-        outputStream = new CountingOutputStream(new CipherOutputStream(new FileOutputStream(tempFile), ciEnc));
+        outputStream = new CountingOutputStream(new CipherOutputStream(Files.newOutputStream(tempFile.toPath()), ciEnc));
         return outputStream;
     }
 
@@ -85,7 +84,7 @@ public class EncryptedTempData {
      */
     public InputStream getInputStream() throws IOException {
         Cipher ciDec = CryptoFunctions.getCipher(skeySpec, cipherAlgorithm, ChainingMode.cbc, ivBytes, Cipher.DECRYPT_MODE, PADDING);
-        return new CipherInputStream(new FileInputStream(tempFile), ciDec);
+        return new CipherInputStream(Files.newInputStream(tempFile.toPath()), ciDec);
     }
 
     /**
index 40441588a401653a12ca15f3084b2e7c206261e3..38562f5a5839f8a664fd86dcbe43e190c0d95635 100644 (file)
@@ -21,10 +21,10 @@ import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
 
 import java.awt.Dimension;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -607,7 +607,7 @@ public class XMLSlideShow extends POIXMLDocument
     @Override
     public XSLFPictureData addPicture(File pict, PictureType format) throws IOException {
         byte[] data = IOUtils.safelyAllocate(pict.length(), MAX_RECORD_LENGTH);
-        try (InputStream is = new FileInputStream(pict)) {
+        try (InputStream is = Files.newInputStream(pict.toPath())) {
             IOUtils.readFully(is, data);
         }
         return addPicture(data, format);
index 882ac230780e13b5c22201131c4bf074926c99f7..362b7039c84e8075aebb1b4eae1aa884c94f9637 100644 (file)
 package org.apache.poi.xssf;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.file.Files;
 
 import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
 import org.apache.poi.hssf.HSSFTestDataSamples;
@@ -76,7 +76,7 @@ public class XSSFTestDataSamples {
     }
 
     private static <R extends Workbook> void writeOut(R wb, File file) throws IOException {
-        try (FileOutputStream out = new FileOutputStream(file)) {
+        try (OutputStream out = Files.newOutputStream(file.toPath())) {
             wb.write(out);
         }
     }
@@ -197,7 +197,7 @@ public class XSSFTestDataSamples {
      * @throws IOException If reading the file fails
      */
     public static XSSFWorkbook readBack(File file) throws IOException {
-        try (InputStream in = new FileInputStream(file)) {
+        try (InputStream in = Files.newInputStream(file.toPath())) {
             return new XSSFWorkbook(in);
         }
     }
index 8b684f2d32dde7528b77911f587fd3bdbd5c6e6a..3f413b0643724c72e651f14db446925508840197 100644 (file)
 package org.apache.poi.hssf.extractor;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintStream;
+import java.nio.file.Files;
 import java.util.Locale;
 
 import org.apache.poi.extractor.POIOLE2TextExtractor;
@@ -225,7 +225,7 @@ public class ExcelExtractor implements POIOLE2TextExtractor, org.apache.poi.ss.e
             return;
         }
 
-        try (InputStream is = cmdArgs.getInputFile() == null ? System.in : new FileInputStream(cmdArgs.getInputFile());
+        try (InputStream is = cmdArgs.getInputFile() == null ? System.in : Files.newInputStream(cmdArgs.getInputFile().toPath());
              HSSFWorkbook wb = new HSSFWorkbook(is);
              ExcelExtractor extractor = new ExcelExtractor(wb)
         ) {
index f6deb15a10d40022c7e9bd472082680a9f508228..dc05c63ec5659052c34448c86ef4c940935746b5 100644 (file)
@@ -27,6 +27,7 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.file.Files;
 
 import org.apache.poi.EncryptedDocumentException;
 import org.apache.poi.extractor.POITextExtractor;
@@ -94,7 +95,7 @@ public class OldExcelExtractor implements POITextExtractor {
         }
 
         @SuppressWarnings("resource")
-        FileInputStream biffStream = new FileInputStream(f); // NOSONAR
+        InputStream biffStream = Files.newInputStream(f.toPath());
         try {
             open(biffStream);
         } catch (IOException | RuntimeException e)  {
index 2ccb1135ba4861e4fa1bb16cea705aa4b8b14123..43e684beb0b3a5c338928efc5e3f0e7749b61649 100644 (file)
@@ -19,9 +19,9 @@ package org.apache.poi.hssf.usermodel;
 
 import java.awt.Font;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.file.Files;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
@@ -118,7 +118,7 @@ final class StaticFontMetrics {
         }
 
         try (InputStream metricsIn = (propFile != null)
-                ? new FileInputStream(propFile)
+                ? Files.newInputStream(propFile.toPath())
                 : FontDetails.class.getResourceAsStream("/font_metrics.properties")
         )  {
             // Use the built-in font metrics file off the classpath
index d4ae39bf47fd2e8968df110808cb0c5007fb2793..793cb5000b9972946f211f3a678d44d0b8f09dca 100644 (file)
@@ -19,11 +19,11 @@ package org.apache.poi.poifs.crypt;
 import static org.apache.poi.poifs.crypt.Decryptor.DEFAULT_POIFS_ENTRY;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.FilterOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
+import java.nio.file.Files;
 import java.security.GeneralSecurityException;
 
 import javax.crypto.Cipher;
@@ -71,7 +71,7 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
         this.plainByteFlags = new SparseBitSet(cs);
         this.chunkBits = Integer.bitCount(cs-1);
         this.fileOut = TempFile.createTempFile("encrypted_package", "crypt");
-        this.out = new FileOutputStream(fileOut);
+        this.out = Files.newOutputStream(fileOut.toPath());
         this.dir = dir;
         this.cipher = initCipherForBlock(null, 0, false);
     }
@@ -303,7 +303,7 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
     private void processPOIFSWriterEvent(POIFSWriterEvent event) {
         try {
             try (OutputStream os = event.getStream();
-                 FileInputStream fis = new FileInputStream(fileOut)) {
+                 InputStream fis = Files.newInputStream(fileOut.toPath())) {
 
                 // StreamSize (8 bytes): An unsigned integer that specifies the number of bytes used by data
                 // encrypted within the EncryptedData field, not including the size of the StreamSize field.
index a0fc8a5214e9f020ac328be3c4ef29c5ae8545d5..d29745ea82e507061ca63223d55e7bc839fa7d91 100644 (file)
 package org.apache.poi.poifs.dev;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.nio.ByteBuffer;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.Iterator;
 
 import org.apache.poi.poifs.common.POIFSConstants;
@@ -65,7 +66,7 @@ public final class POIFSDump {
             }
 
             System.out.println("Dumping " + filename);
-            try (FileInputStream is = new FileInputStream(filename);
+            try (InputStream is = Files.newInputStream(Paths.get(filename));
                  POIFSFileSystem fs = new POIFSFileSystem(is)) {
                 DirectoryEntry root = fs.getRoot();
                 String filenameWithoutPath = new File(filename).getName();
@@ -98,12 +99,12 @@ public final class POIFSDump {
         for(Iterator<Entry> it = root.getEntries(); it.hasNext();){
             Entry entry = it.next();
             if(entry instanceof DocumentNode){
-                DocumentNode node = (DocumentNode)entry;
-                DocumentInputStream is = new DocumentInputStream(node);
-                byte[] bytes = IOUtils.toByteArray(is);
-                is.close();
-
-                try (OutputStream out = new FileOutputStream(new File(parent, node.getName().trim()))) {
+                final DocumentNode node = (DocumentNode) entry;
+                final byte[] bytes;
+                try (DocumentInputStream is = new DocumentInputStream(node)) {
+                   bytes = IOUtils.toByteArray(is);
+                }
+                try (OutputStream out = Files.newOutputStream(new File(parent, node.getName().trim()).toPath())) {
                     out.write(bytes);
                 }
             } else if (entry instanceof DirectoryEntry){
@@ -120,7 +121,7 @@ public final class POIFSDump {
     }
     public static void dump(POIFSFileSystem fs, int startBlock, String name, File parent) throws IOException {
         File file = new File(parent, name);
-        try (FileOutputStream out = new FileOutputStream(file)) {
+        try (OutputStream out = Files.newOutputStream(file.toPath())) {
             POIFSStream stream = new POIFSStream(fs, startBlock);
 
             byte[] b = IOUtils.safelyAllocate(fs.getBigBlockSize(), POIFSFileSystem.getMaxRecordLength());
index 81cd6187e24783515643ea35e42f070fa85c39b5..57c421f4b0535395813f0596f685e6c03e0ef076 100644 (file)
@@ -19,9 +19,9 @@ package org.apache.poi.poifs.filesystem;
 
 import java.io.BufferedInputStream;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.file.Files;
 import java.util.Arrays;
 
 import org.apache.poi.poifs.storage.HeaderBlockConstants;
@@ -171,7 +171,7 @@ public enum FileMagic {
      * @param inp a file to be identified
      */
     public static FileMagic valueOf(final File inp) throws IOException {
-        try (FileInputStream fis = new FileInputStream(inp)) {
+        try (InputStream fis = Files.newInputStream(inp.toPath())) {
             // read as many bytes as possible, up to the required number of bytes
             byte[] data = new byte[MAX_PATTERN_LENGTH];
             int read = IOUtils.readFully(fis, data, 0, MAX_PATTERN_LENGTH);
index f70edb9044b450d0afcd2df48b925b1dfa4b9036..97e703b7e294c5e083f4945edfabe8e7819c03f5 100644 (file)
@@ -18,8 +18,6 @@ package org.apache.poi.poifs.filesystem;
 
 import java.io.Closeable;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -27,6 +25,8 @@ import java.nio.ByteBuffer;
 import java.nio.channels.Channels;
 import java.nio.channels.FileChannel;
 import java.nio.channels.ReadableByteChannel;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
@@ -822,8 +822,8 @@ public class POIFSFileSystem extends BlockStore
             System.exit(1);
         }
 
-        try (FileInputStream istream = new FileInputStream(args[0])) {
-            try (FileOutputStream ostream = new FileOutputStream(args[1])) {
+        try (InputStream istream = Files.newInputStream(Paths.get(args[0]))) {
+            try (OutputStream ostream = Files.newOutputStream(Paths.get(args[1]))) {
                 try (POIFSFileSystem fs = new POIFSFileSystem(istream)) {
                     fs.writeFilesystem(ostream);
                 }
@@ -959,7 +959,7 @@ public class POIFSFileSystem extends BlockStore
     public static POIFSFileSystem create(File file) throws IOException {
         // Create a new empty POIFS in the file
         try (POIFSFileSystem tmp = new POIFSFileSystem();
-             OutputStream out = new FileOutputStream(file)) {
+             OutputStream out = Files.newOutputStream(file.toPath())) {
             tmp.writeFilesystem(out);
         }
 
index 0e7266c4b5e2fe22bf2e1841c8234027b39c1a7c..2da79c6c236e35bd61482f7d364aa87ba6fe95d2 100644 (file)
@@ -18,6 +18,8 @@
 package org.apache.poi.util;
 
 import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.List;
 import java.util.ArrayList;
 
@@ -36,7 +38,7 @@ public class HexRead {
      */
     public static byte[] readData( String filename ) throws IOException {
         File file = new File( filename );
-        try (InputStream stream = new FileInputStream(file)) {
+        try (InputStream stream = Files.newInputStream(file.toPath())) {
             return readData(stream, -1);
         }
     }
@@ -83,7 +85,7 @@ public class HexRead {
     }
 
     public static byte[] readData( String filename, String section ) throws IOException {
-        return readData(new FileInputStream( filename ), section);
+        return readData(Files.newInputStream(Paths.get(filename)), section);
     }
 
     @SuppressWarnings("fallthrough")
index d96e2f264ee9bc4c23e63f7d743aa0788a83c0e4..e15843f377f0734a5694863f61dab46e20b0bb7a 100644 (file)
 package org.apache.poi;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
 
 import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
@@ -150,9 +150,9 @@ public final class POIDataSamples {
 
         File f = getFile(sampleFileName);
         try {
-            return new FileInputStream(f);
-        } catch (FileNotFoundException e) {
-            throw new RuntimeException(e);
+            return Files.newInputStream(f.toPath());
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
         }
     }
 
index b9e97f7bfaeea51b071de10958e17ced46ac7cc9..5b355777f969b3c2fdea09f465d97a1c80e693ae 100644 (file)
@@ -25,6 +25,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
+import java.io.UncheckedIOException;
 import java.io.Writer;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
@@ -311,7 +312,7 @@ public final class BiffViewer {
 
             w.write(buf, 0, idx);
         } catch (IOException e) {
-            throw new RuntimeException(e);
+            throw new UncheckedIOException(e);
         }
     }
 
index 3e9dad3923f82ede3277e265767999db20a171fb..a8750e01b64032798c398e732100bc45c401f4e4 100644 (file)
@@ -19,6 +19,7 @@ package org.apache.poi.hssf.usermodel;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.UncheckedIOException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -53,7 +54,7 @@ public final class StreamUtility {
             result = diffInternal(isA, isB, allowableDifferenceRegions);
             success = true;
         } catch (IOException e) {
-            throw new RuntimeException(e);
+            throw new UncheckedIOException(e);
         } finally {
             close(isA, success);
             close(isB, success);
@@ -70,7 +71,7 @@ public final class StreamUtility {
         } catch (IOException e) {
             if(success) {
                 // this is a new error. ok to throw
-                throw new RuntimeException(e);
+                throw new UncheckedIOException(e);
             }
             // else don't subvert original exception. just print stack trace for this one
             e.printStackTrace();
index 8cee4a828c475b875244528e162f54446480a0bc..215f4e4cfd8426d1de1852d7808f53f59338057f 100644 (file)
@@ -19,7 +19,6 @@ package org.apache.poi.ss.formula.function;
 
 import java.io.BufferedReader;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -27,6 +26,8 @@ import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.PrintStream;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -369,7 +370,7 @@ public final class ExcelCetabFunctionExtractor {
             throw new IllegalStateException("Did not find file " + SOURCE_DOC_FILE_NAME + " in the resources");
         }
 
-        try (InputStream stream = new FileInputStream(SOURCE_DOC_FILE_NAME)) {
+        try (InputStream stream = Files.newInputStream(Paths.get(SOURCE_DOC_FILE_NAME))) {
             File outFile = new File("functionMetadataCetab.txt");
 
             processFile(stream, outFile);
index 56ac92f6412d06de63d0298f960a6086ad39ab0d..b131071abf5949898382c317d7394b533d56ef49 100644 (file)
 package org.apache.poi.ss.formula.function;
 
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PrintStream;
+import java.io.UncheckedIOException;
 import java.io.UnsupportedEncodingException;
 import java.math.BigInteger;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
 import java.security.MessageDigest;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -486,9 +485,9 @@ public final class ExcelFileFormatDocFunctionExtractor {
         }
         OutputStream os;
         try {
-            os = new FileOutputStream(outFile);
-        } catch (FileNotFoundException e) {
-            throw new RuntimeException(e);
+            os = Files.newOutputStream(outFile.toPath());
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
         }
         os = new SimpleAsciiOutputStream(os);
         PrintStream ps;
@@ -559,7 +558,7 @@ public final class ExcelFileFormatDocFunctionExtractor {
 
         byte[]buf = new byte[2048];
         try {
-            InputStream is = new FileInputStream(f);
+            InputStream is = Files.newInputStream(f.toPath());
             while(true) {
                 int bytesRead = is.read(buf);
                 if(bytesRead<1) {
@@ -590,7 +589,7 @@ public final class ExcelFileFormatDocFunctionExtractor {
             InputStream is = conn.getInputStream();
             System.out.println("downloading " + url.toExternalForm());
             result = TempFile.createTempFile("excelfileformat", ".odt");
-            OutputStream os = new FileOutputStream(result);
+            OutputStream os = Files.newOutputStream(result.toPath());
             while(true) {
                 int bytesRead = is.read(buf);
                 if(bytesRead<1) {
@@ -601,7 +600,7 @@ public final class ExcelFileFormatDocFunctionExtractor {
             is.close();
             os.close();
         } catch (IOException e) {
-            throw new RuntimeException(e);
+            throw new UncheckedIOException(e);
         }
         System.out.println("file downloaded ok");
         return result;
index d80d23990c5d84e9cf325a833e67bc86a2dd6e9f..cdabc06a40442e07cf1157c487ec9be418d8801d 100644 (file)
@@ -19,9 +19,11 @@ package org.apache.poi.ss.util;
 
 import java.io.DataInputStream;
 import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
@@ -151,7 +153,7 @@ public class NumberRenderingSpreadsheetGenerator {
         File outputFile = new File("ExcelNumberRendering.xls");
 
         try (UnsynchronizedByteArrayOutputStream baos = UnsynchronizedByteArrayOutputStream.builder().get();
-             FileOutputStream os = new FileOutputStream(outputFile)) {
+             OutputStream os = Files.newOutputStream(outputFile.toPath())) {
             wb.write(baos);
 
             byte[] fileContent = baos.toByteArray();
@@ -159,7 +161,7 @@ public class NumberRenderingSpreadsheetGenerator {
 
             os.write(fileContent);
         } catch (IOException e) {
-            throw new RuntimeException(e);
+            throw new UncheckedIOException(e);
         }
 
         System.out.println("Finished writing '" + outputFile.getAbsolutePath() + "'");