diff options
author | PJ Fanning <fanningpj@apache.org> | 2023-09-17 15:29:16 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2023-09-17 15:29:16 +0000 |
commit | 42996906392063b3423a68cf6fe99362e6ce9e9f (patch) | |
tree | f95c51a679faf9d8adc10c1ad6311b70ca1c502d /poi/src | |
parent | 8514b659aef47a210d38ccbe59978a6926af5075 (diff) | |
download | poi-42996906392063b3423a68cf6fe99362e6ce9e9f.tar.gz poi-42996906392063b3423a68cf6fe99362e6ce9e9f.zip |
use files nio APIs in more places
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1912367 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi/src')
12 files changed, 32 insertions, 27 deletions
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 dc05c63ec5..80b715a59b 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 @@ -23,7 +23,6 @@ import static org.apache.poi.hssf.model.InternalWorkbook.WORKBOOK_DIR_ENTRY_NAME import java.io.BufferedInputStream; import java.io.Closeable; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java b/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java index 73ed284721..17a03d8ebe 100644 --- a/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java +++ b/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptor.java @@ -31,10 +31,10 @@ import static org.apache.poi.poifs.crypt.agile.AgileDecryptor.kIntegrityValueBlo import static org.apache.poi.poifs.crypt.agile.AgileDecryptor.kVerifierInputBlock; 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.security.GeneralSecurityException; import java.security.MessageDigest; import java.security.SecureRandom; @@ -237,7 +237,7 @@ public class AgileEncryptor extends Encryptor { LittleEndian.putLong(buf, 0, oleStreamSize); integrityMD.update(buf, 0, LittleEndianConsts.LONG_SIZE); - try (InputStream fis = new FileInputStream(tmpFile)) { + try (InputStream fis = Files.newInputStream(tmpFile.toPath())) { int readBytes; while ((readBytes = fis.read(buf)) != -1) { integrityMD.update(buf, 0, readBytes); diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java b/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java index a5f2559965..8eb813ba61 100644 --- a/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java +++ b/poi/src/main/java/org/apache/poi/poifs/crypt/standard/StandardEncryptor.java @@ -21,11 +21,11 @@ import static org.apache.poi.poifs.crypt.DataSpaceMapUtils.createEncryptionEntry import static org.apache.poi.poifs.crypt.standard.StandardDecryptor.generateSecretKey; 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 java.security.MessageDigest; import java.security.SecureRandom; @@ -145,7 +145,7 @@ public class StandardEncryptor extends Encryptor { // field of the EncryptedPackage field specifies the number of bytes of // unencrypted data as specified in section 2.3.4.4. super( - new CipherOutputStream(new FileOutputStream(fileOut), getCipher(getSecretKey(), "PKCS5Padding")) + new CipherOutputStream(Files.newOutputStream(fileOut.toPath()), getCipher(getSecretKey(), "PKCS5Padding")) ); this.deleteFile = deleteFile; this.fileOut = fileOut; @@ -197,7 +197,7 @@ public class StandardEncryptor extends Encryptor { // value, depending on the block size of the chosen encryption algorithm leos.writeLong(countBytes); - try (FileInputStream fis = new FileInputStream(fileOut)) { + try (InputStream fis = Files.newInputStream(fileOut.toPath())) { IOUtils.copy(fis, leos); } if (!fileOut.delete()) { diff --git a/poi/src/main/java/org/apache/poi/poifs/dev/POIFSLister.java b/poi/src/main/java/org/apache/poi/poifs/dev/POIFSLister.java index f9696bccfb..8ae5d6e894 100644 --- a/poi/src/main/java/org/apache/poi/poifs/dev/POIFSLister.java +++ b/poi/src/main/java/org/apache/poi/poifs/dev/POIFSLister.java @@ -18,8 +18,10 @@ package org.apache.poi.poifs.dev; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Iterator; import org.apache.poi.poifs.filesystem.DirectoryNode; @@ -68,7 +70,7 @@ public class POIFSLister { } public static void viewFileOld(final String filename, boolean withSizes) throws IOException { - try (FileInputStream fis = new FileInputStream(filename); + try (InputStream fis = Files.newInputStream(Paths.get(filename)); POIFSFileSystem fs = new POIFSFileSystem(fis)) { displayDirectory(fs.getRoot(), "", withSizes); } diff --git a/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroExtractor.java b/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroExtractor.java index c4ff962a94..c0db47e5fb 100644 --- a/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroExtractor.java +++ b/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroExtractor.java @@ -19,9 +19,10 @@ package org.apache.poi.poifs.macros; import java.io.File; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.io.OutputStreamWriter; +import java.nio.file.Files; import java.util.Map; import java.util.Map.Entry; @@ -95,8 +96,8 @@ public class VBAMacroExtractor { System.out.println(moduleCode); } else { File out = new File(outputDir, moduleName + extension); - try (FileOutputStream fout = new FileOutputStream(out); - OutputStreamWriter fwriter = new OutputStreamWriter(fout, StringUtil.UTF8)) { + try (OutputStream fout = Files.newOutputStream(out.toPath()); + OutputStreamWriter fwriter = new OutputStreamWriter(fout, StringUtil.UTF8)) { fwriter.write(moduleCode); } System.out.println("Extracted " + out); diff --git a/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java b/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java index c89b650452..944c05bbd1 100644 --- a/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java +++ b/poi/src/main/java/org/apache/poi/poifs/macros/VBAMacroReader.java @@ -24,12 +24,12 @@ import static org.apache.poi.util.StringUtil.startsWithIgnoreCase; import java.io.Closeable; import java.io.EOFException; import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -93,7 +93,7 @@ public class VBAMacroReader implements Closeable { try { this.fs = new POIFSFileSystem(file); } catch (OfficeXmlFileException e) { - openOOXML(new FileInputStream(file)); + openOOXML(Files.newInputStream(file.toPath())); } } public VBAMacroReader(POIFSFileSystem fs) { diff --git a/poi/src/main/java/org/apache/poi/util/FontMetricsDumper.java b/poi/src/main/java/org/apache/poi/util/FontMetricsDumper.java index 321f0d5ea9..5078436bc2 100644 --- a/poi/src/main/java/org/apache/poi/util/FontMetricsDumper.java +++ b/poi/src/main/java/org/apache/poi/util/FontMetricsDumper.java @@ -22,9 +22,10 @@ import java.awt.Font; import java.awt.FontMetrics; import java.awt.GraphicsEnvironment; import java.awt.Toolkit; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Properties; @SuppressWarnings("deprecation") @@ -66,7 +67,7 @@ public class FontMetricsDumper { props.setProperty("font." + fontName + ".widths", widths.toString()); } - try (OutputStream fileOut = new FileOutputStream("font_metrics.properties")) { + try (OutputStream fileOut = Files.newOutputStream(Paths.get("font_metrics.properties"))) { props.store(fileOut, "Font Metrics"); } } diff --git a/poi/src/main/java/org/apache/poi/util/GenericRecordJsonWriter.java b/poi/src/main/java/org/apache/poi/util/GenericRecordJsonWriter.java index 7b24b774f6..4aa0bd56bb 100644 --- a/poi/src/main/java/org/apache/poi/util/GenericRecordJsonWriter.java +++ b/poi/src/main/java/org/apache/poi/util/GenericRecordJsonWriter.java @@ -34,7 +34,6 @@ import java.awt.image.IndexColorModel; import java.awt.image.PackedColorModel; import java.io.Closeable; import java.io.File; -import java.io.FileOutputStream; import java.io.Flushable; import java.io.IOException; import java.io.OutputStream; @@ -43,6 +42,7 @@ import java.io.PrintWriter; import java.io.Writer; import java.lang.reflect.Array; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.AbstractMap; import java.util.ArrayList; import java.util.Arrays; @@ -116,7 +116,8 @@ public class GenericRecordJsonWriter implements Closeable { protected int childIndex = 0; public GenericRecordJsonWriter(File fileName) throws IOException { - OutputStream os = ("null".equals(fileName.getName())) ? NullOutputStream.INSTANCE : new FileOutputStream(fileName); + OutputStream os = ("null".equals(fileName.getName())) ? + NullOutputStream.INSTANCE : Files.newOutputStream(fileName.toPath()); aw = new AppendableWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8)); fw = new PrintWriter(aw); } diff --git a/poi/src/main/java/org/apache/poi/util/GenericRecordXmlWriter.java b/poi/src/main/java/org/apache/poi/util/GenericRecordXmlWriter.java index ba098965d3..fe8bc58dc5 100644 --- a/poi/src/main/java/org/apache/poi/util/GenericRecordXmlWriter.java +++ b/poi/src/main/java/org/apache/poi/util/GenericRecordXmlWriter.java @@ -29,13 +29,13 @@ import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.Closeable; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.lang.reflect.Array; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.AbstractMap; import java.util.ArrayList; import java.util.Arrays; @@ -108,7 +108,8 @@ public class GenericRecordXmlWriter implements Closeable { private boolean attributePhase = true; public GenericRecordXmlWriter(File fileName) throws IOException { - OutputStream os = ("null".equals(fileName.getName())) ? NullOutputStream.INSTANCE : new FileOutputStream(fileName); + OutputStream os = ("null".equals(fileName.getName())) ? + NullOutputStream.INSTANCE : Files.newOutputStream(fileName.toPath()); fw = new PrintWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8)); } diff --git a/poi/src/main/java/org/apache/poi/util/IOUtils.java b/poi/src/main/java/org/apache/poi/util/IOUtils.java index 51281d4bba..4fa3484af8 100644 --- a/poi/src/main/java/org/apache/poi/util/IOUtils.java +++ b/poi/src/main/java/org/apache/poi/util/IOUtils.java @@ -20,13 +20,13 @@ package org.apache.poi.util; import java.io.Closeable; import java.io.EOFException; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PushbackInputStream; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; +import java.nio.file.Files; import java.util.Arrays; import java.util.Locale; import java.util.zip.CRC32; @@ -436,7 +436,7 @@ public final class IOUtils { if (!(destDirectory.exists() || destDirectory.mkdirs())) { throw new IllegalStateException("Can't create destination directory: "+destDirectory); } - try (OutputStream destStream = new FileOutputStream(destFile)) { + try (OutputStream destStream = Files.newOutputStream(destFile.toPath())) { return IOUtils.copy(srcStream, destStream); } } 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 215f4e4cfd..87c0dcd1f3 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.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -322,8 +321,8 @@ public final class ExcelCetabFunctionExtractor { } private static void processFile(InputStream input, File outFile) throws IOException { - try (OutputStream os = new SimpleAsciiOutputStream(new FileOutputStream(outFile)); - PrintStream ps = new PrintStream(os, true, StandardCharsets.UTF_8.name())) { + try (OutputStream os = new SimpleAsciiOutputStream(Files.newOutputStream(outFile.toPath())); + PrintStream ps = new PrintStream(os, true, StandardCharsets.UTF_8.name())) { outputLicenseHeader(ps); Class<?> genClass = ExcelCetabFunctionExtractor.class; diff --git a/poi/src/test/java/org/apache/poi/ss/util/NumberComparingSpreadsheetGenerator.java b/poi/src/test/java/org/apache/poi/ss/util/NumberComparingSpreadsheetGenerator.java index 8c241a6464..301a7e3019 100644 --- a/poi/src/test/java/org/apache/poi/ss/util/NumberComparingSpreadsheetGenerator.java +++ b/poi/src/test/java/org/apache/poi/ss/util/NumberComparingSpreadsheetGenerator.java @@ -18,8 +18,9 @@ package org.apache.poi.ss.util; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; @@ -135,7 +136,7 @@ public class NumberComparingSpreadsheetGenerator { } File outputFile = new File("ExcelNumberCompare.xls"); - try (FileOutputStream os = new FileOutputStream(outputFile)) { + try (OutputStream os = Files.newOutputStream(outputFile.toPath())) { wb.write(os); } System.out.println("Finished writing '" + outputFile.getAbsolutePath() + "'"); |