summaryrefslogtreecommitdiffstats
path: root/poi
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2023-09-14 15:49:10 +0000
committerPJ Fanning <fanningpj@apache.org>2023-09-14 15:49:10 +0000
commit4b520ff7c5859cf18ea7cc9a74524f381d4624c3 (patch)
tree24db8759d40a2935c35699cebabe87d9137aff5b /poi
parent4afcb281cef11d7943e5c4ca4324098f7eac9b42 (diff)
downloadpoi-4b520ff7c5859cf18ea7cc9a74524f381d4624c3.tar.gz
poi-4b520ff7c5859cf18ea7cc9a74524f381d4624c3.zip
use more nio file support
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1912316 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi')
-rw-r--r--poi/src/main/java/org/apache/poi/hssf/extractor/ExcelExtractor.java4
-rw-r--r--poi/src/main/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java3
-rw-r--r--poi/src/main/java/org/apache/poi/hssf/usermodel/StaticFontMetrics.java4
-rw-r--r--poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java8
-rw-r--r--poi/src/main/java/org/apache/poi/poifs/dev/POIFSDump.java21
-rw-r--r--poi/src/main/java/org/apache/poi/poifs/filesystem/FileMagic.java4
-rw-r--r--poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java10
-rw-r--r--poi/src/main/java/org/apache/poi/util/HexRead.java6
-rw-r--r--poi/src/test/java/org/apache/poi/POIDataSamples.java10
-rw-r--r--poi/src/test/java/org/apache/poi/hssf/dev/BiffViewer.java3
-rw-r--r--poi/src/test/java/org/apache/poi/hssf/usermodel/StreamUtility.java5
-rw-r--r--poi/src/test/java/org/apache/poi/ss/formula/function/ExcelCetabFunctionExtractor.java5
-rw-r--r--poi/src/test/java/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java17
-rw-r--r--poi/src/test/java/org/apache/poi/ss/util/NumberRenderingSpreadsheetGenerator.java8
14 files changed, 58 insertions, 50 deletions
diff --git a/poi/src/main/java/org/apache/poi/hssf/extractor/ExcelExtractor.java b/poi/src/main/java/org/apache/poi/hssf/extractor/ExcelExtractor.java
index 8b684f2d32..3f413b0643 100644
--- a/poi/src/main/java/org/apache/poi/hssf/extractor/ExcelExtractor.java
+++ b/poi/src/main/java/org/apache/poi/hssf/extractor/ExcelExtractor.java
@@ -18,10 +18,10 @@
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)
) {
diff --git a/poi/src/main/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java b/poi/src/main/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
index f6deb15a10..dc05c63ec5 100644
--- a/poi/src/main/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
+++ b/poi/src/main/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
@@ -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) {
diff --git a/poi/src/main/java/org/apache/poi/hssf/usermodel/StaticFontMetrics.java b/poi/src/main/java/org/apache/poi/hssf/usermodel/StaticFontMetrics.java
index 2ccb1135ba..43e684beb0 100644
--- a/poi/src/main/java/org/apache/poi/hssf/usermodel/StaticFontMetrics.java
+++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/StaticFontMetrics.java
@@ -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
diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java b/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java
index d4ae39bf47..793cb5000b 100644
--- a/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java
+++ b/poi/src/main/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java
@@ -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.
diff --git a/poi/src/main/java/org/apache/poi/poifs/dev/POIFSDump.java b/poi/src/main/java/org/apache/poi/poifs/dev/POIFSDump.java
index a0fc8a5214..d29745ea82 100644
--- a/poi/src/main/java/org/apache/poi/poifs/dev/POIFSDump.java
+++ b/poi/src/main/java/org/apache/poi/poifs/dev/POIFSDump.java
@@ -17,11 +17,12 @@
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());
diff --git a/poi/src/main/java/org/apache/poi/poifs/filesystem/FileMagic.java b/poi/src/main/java/org/apache/poi/poifs/filesystem/FileMagic.java
index 81cd6187e2..57c421f4b0 100644
--- a/poi/src/main/java/org/apache/poi/poifs/filesystem/FileMagic.java
+++ b/poi/src/main/java/org/apache/poi/poifs/filesystem/FileMagic.java
@@ -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);
diff --git a/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java b/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
index f70edb9044..97e703b7e2 100644
--- a/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
+++ b/poi/src/main/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java
@@ -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);
}
diff --git a/poi/src/main/java/org/apache/poi/util/HexRead.java b/poi/src/main/java/org/apache/poi/util/HexRead.java
index 0e7266c4b5..2da79c6c23 100644
--- a/poi/src/main/java/org/apache/poi/util/HexRead.java
+++ b/poi/src/main/java/org/apache/poi/util/HexRead.java
@@ -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")
diff --git a/poi/src/test/java/org/apache/poi/POIDataSamples.java b/poi/src/test/java/org/apache/poi/POIDataSamples.java
index d96e2f264e..e15843f377 100644
--- a/poi/src/test/java/org/apache/poi/POIDataSamples.java
+++ b/poi/src/test/java/org/apache/poi/POIDataSamples.java
@@ -17,10 +17,10 @@
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);
}
}
diff --git a/poi/src/test/java/org/apache/poi/hssf/dev/BiffViewer.java b/poi/src/test/java/org/apache/poi/hssf/dev/BiffViewer.java
index b9e97f7bfa..5b355777f9 100644
--- a/poi/src/test/java/org/apache/poi/hssf/dev/BiffViewer.java
+++ b/poi/src/test/java/org/apache/poi/hssf/dev/BiffViewer.java
@@ -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);
}
}
diff --git a/poi/src/test/java/org/apache/poi/hssf/usermodel/StreamUtility.java b/poi/src/test/java/org/apache/poi/hssf/usermodel/StreamUtility.java
index 3e9dad3923..a8750e01b6 100644
--- a/poi/src/test/java/org/apache/poi/hssf/usermodel/StreamUtility.java
+++ b/poi/src/test/java/org/apache/poi/hssf/usermodel/StreamUtility.java
@@ -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();
diff --git a/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelCetabFunctionExtractor.java b/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelCetabFunctionExtractor.java
index 8cee4a828c..215f4e4cfd 100644
--- a/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelCetabFunctionExtractor.java
+++ b/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelCetabFunctionExtractor.java
@@ -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);
diff --git a/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java b/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java
index 56ac92f641..b131071abf 100644
--- a/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java
+++ b/poi/src/test/java/org/apache/poi/ss/formula/function/ExcelFileFormatDocFunctionExtractor.java
@@ -18,19 +18,18 @@
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;
diff --git a/poi/src/test/java/org/apache/poi/ss/util/NumberRenderingSpreadsheetGenerator.java b/poi/src/test/java/org/apache/poi/ss/util/NumberRenderingSpreadsheetGenerator.java
index d80d23990c..cdabc06a40 100644
--- a/poi/src/test/java/org/apache/poi/ss/util/NumberRenderingSpreadsheetGenerator.java
+++ b/poi/src/test/java/org/apache/poi/ss/util/NumberRenderingSpreadsheetGenerator.java
@@ -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() + "'");