From 42996906392063b3423a68cf6fe99362e6ce9e9f Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Sun, 17 Sep 2023 15:29:16 +0000 Subject: [PATCH] use files nio APIs in more places git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1912367 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/examples/ss/ConditionalFormats.java | 6 ++++-- .../org/apache/poi/ooxml/dev/OOXMLPrettyPrint.java | 4 +++- .../poifs/crypt/temp/AesZipFileZipEntrySource.java | 7 ++++--- .../org/apache/poi/xdgf/util/HierarchyPrinter.java | 9 +++++---- .../java/org/apache/poi/xdgf/util/VsdxToPng.java | 10 ++++++---- .../java/org/apache/poi/xslf/util/DummyFormat.java | 5 +++-- .../main/java/org/apache/poi/xslf/util/PPTX2PNG.java | 5 +++-- .../java/org/apache/poi/xslf/util/SVGFormat.java | 4 ++-- .../org/apache/poi/xssf/streaming/SXSSFWorkbook.java | 4 ++-- .../java/org/apache/poi/hmef/dev/HMEFDumper.java | 5 +++-- .../poi/hmef/extractor/HMEFContentsExtractor.java | 12 ++++++------ .../main/java/org/apache/poi/hpbf/dev/PLCDumper.java | 5 +++-- .../java/org/apache/poi/hslf/dev/PPTXMLDump.java | 6 ++++-- .../apache/poi/hslf/extractor/ImageExtractor.java | 7 +++++-- .../org/apache/poi/hslf/usermodel/HSLFSlideShow.java | 4 ++-- .../poi/hssf/converter/AbstractExcelUtils.java | 5 +++-- .../apache/poi/hwpf/converter/AbstractWordUtils.java | 4 ++-- .../java/org/apache/poi/hwpf/dev/HWPFLister.java | 4 ++-- .../apache/poi/hssf/extractor/OldExcelExtractor.java | 1 - .../apache/poi/poifs/crypt/agile/AgileEncryptor.java | 4 ++-- .../poi/poifs/crypt/standard/StandardEncryptor.java | 8 ++++---- .../java/org/apache/poi/poifs/dev/POIFSLister.java | 6 ++++-- .../apache/poi/poifs/macros/VBAMacroExtractor.java | 7 ++++--- .../org/apache/poi/poifs/macros/VBAMacroReader.java | 4 ++-- .../java/org/apache/poi/util/FontMetricsDumper.java | 5 +++-- .../org/apache/poi/util/GenericRecordJsonWriter.java | 5 +++-- .../org/apache/poi/util/GenericRecordXmlWriter.java | 5 +++-- poi/src/main/java/org/apache/poi/util/IOUtils.java | 4 ++-- .../function/ExcelCetabFunctionExtractor.java | 5 ++--- .../ss/util/NumberComparingSpreadsheetGenerator.java | 5 +++-- 30 files changed, 94 insertions(+), 71 deletions(-) diff --git a/poi-examples/src/main/java/org/apache/poi/examples/ss/ConditionalFormats.java b/poi-examples/src/main/java/org/apache/poi/examples/ss/ConditionalFormats.java index db65f76787..a0a8177e87 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/ss/ConditionalFormats.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/ss/ConditionalFormats.java @@ -19,8 +19,10 @@ package org.apache.poi.examples.ss; -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.List; import org.apache.poi.hssf.usermodel.HSSFWorkbook; @@ -93,7 +95,7 @@ public final class ConditionalFormats { if (wb instanceof XSSFWorkbook) { file += "x"; } - try (FileOutputStream out = new FileOutputStream(file)) { + try (OutputStream out = Files.newOutputStream(Paths.get(file))) { wb.write(out); } System.out.println("Generated: " + file); diff --git a/poi-ooxml/src/main/java/org/apache/poi/ooxml/dev/OOXMLPrettyPrint.java b/poi-ooxml/src/main/java/org/apache/poi/ooxml/dev/OOXMLPrettyPrint.java index 7a5fb93bb3..00ded3fb2a 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/ooxml/dev/OOXMLPrettyPrint.java +++ b/poi-ooxml/src/main/java/org/apache/poi/ooxml/dev/OOXMLPrettyPrint.java @@ -17,6 +17,7 @@ package org.apache.poi.ooxml.dev; import java.io.*; +import java.nio.file.Files; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -82,7 +83,8 @@ public class OOXMLPrettyPrint { System.out.println("Reading zip-file " + file + " and writing pretty-printed XML to " + outFile); try (ZipSecureFile zipFile = ZipHelper.openZipFile(file)) { - try (ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFile)))) { + try (ZipOutputStream out = new ZipOutputStream( + new BufferedOutputStream(Files.newOutputStream(outFile.toPath())))) { new OOXMLPrettyPrint().handle(zipFile, out); } } finally { diff --git a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java index a6e3286b51..b8e3bcba9d 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java +++ b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/temp/AesZipFileZipEntrySource.java @@ -20,9 +20,10 @@ package org.apache.poi.poifs.crypt.temp; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; import java.util.Enumeration; import javax.crypto.Cipher; @@ -130,8 +131,8 @@ public final class AesZipFileZipEntrySource implements ZipEntrySource { Cipher ciEnc = CryptoFunctions.getCipher(skeySpec, CipherAlgorithm.aes128, ChainingMode.cbc, ivBytes, Cipher.ENCRYPT_MODE, PADDING); try (ZipArchiveInputStream zis = new ZipArchiveInputStream(is); - FileOutputStream fos = new FileOutputStream(tmpFile); - ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos)) { + OutputStream fos = Files.newOutputStream(tmpFile.toPath()); + ZipArchiveOutputStream zos = new ZipArchiveOutputStream(fos)) { ZipArchiveEntry ze; while ((ze = zis.getNextZipEntry()) != null) { diff --git a/poi-ooxml/src/main/java/org/apache/poi/xdgf/util/HierarchyPrinter.java b/poi-ooxml/src/main/java/org/apache/poi/xdgf/util/HierarchyPrinter.java index bba3433850..b3a8a47b8e 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xdgf/util/HierarchyPrinter.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xdgf/util/HierarchyPrinter.java @@ -19,14 +19,15 @@ package org.apache.poi.xdgf.util; import java.awt.geom.AffineTransform; 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.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.poi.xdgf.usermodel.XDGFPage; import org.apache.poi.xdgf.usermodel.XDGFShape; @@ -46,7 +47,7 @@ public class HierarchyPrinter { + Util.sanitizeFilename(page.getName()) + ".txt"); try ( - OutputStream os = new FileOutputStream(pageFile); + OutputStream os = Files.newOutputStream(pageFile.toPath()); PrintStream pos = new PrintStream(os, false, StandardCharsets.UTF_8.name()) ) { printHierarchy(page, pos); @@ -91,7 +92,7 @@ public class HierarchyPrinter { String inFilename = args[0]; String outDir = args[1]; - try (FileInputStream is = new FileInputStream(inFilename)) { + try (InputStream is = Files.newInputStream(Paths.get(inFilename))) { XmlVisioDocument doc = new XmlVisioDocument(is); printHierarchy(doc, outDir); } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xdgf/util/VsdxToPng.java b/poi-ooxml/src/main/java/org/apache/poi/xdgf/util/VsdxToPng.java index e8c96c3355..8d2aa31555 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xdgf/util/VsdxToPng.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xdgf/util/VsdxToPng.java @@ -22,9 +22,11 @@ import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; 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 java.nio.file.Paths; import javax.imageio.ImageIO; @@ -94,7 +96,7 @@ public class VsdxToPng { graphics.dispose(); - try (FileOutputStream out = new FileOutputStream(outFile)) { + try (OutputStream out = Files.newOutputStream(outFile.toPath())) { ImageIO.write(img, "png", out); } } @@ -127,7 +129,7 @@ public class VsdxToPng { renderer = new ShapeDebuggerRenderer(); } - try (FileInputStream is = new FileInputStream(inFilename)) { + try (InputStream is = Files.newInputStream(Paths.get(inFilename))) { XmlVisioDocument doc = new XmlVisioDocument(is); renderToPng(doc, pngDir, 2000 / 11.0, renderer); } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/util/DummyFormat.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/util/DummyFormat.java index 0a995ed498..63166ef855 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xslf/util/DummyFormat.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/util/DummyFormat.java @@ -19,11 +19,12 @@ package org.apache.poi.xslf.util; import java.awt.Graphics2D; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.io.PrintStream; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; @@ -49,7 +50,7 @@ public class DummyFormat implements OutputFormat { @Override public void writeSlide(MFProxy proxy, File outFile) throws IOException { - try (FileOutputStream fos = new FileOutputStream(outFile)) { + try (OutputStream fos = Files.newOutputStream(outFile.toPath())) { bos.writeTo(fos); bos.reset(); } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/util/PPTX2PNG.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/util/PPTX2PNG.java index a589855795..0dcbf813f5 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xslf/util/PPTX2PNG.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/util/PPTX2PNG.java @@ -24,10 +24,11 @@ import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.Dimension2D; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.nio.charset.Charset; +import java.nio.file.Files; import java.util.Arrays; import java.util.Locale; import java.util.Map; @@ -426,7 +427,7 @@ public final class PPTX2PNG { // do some sanitizing for creative filenames ... filename = new File(filename == null ? "dummy.dat" : filename).getName(); filename = calcOutFile(proxy, slideNo).replaceFirst("\\.\\w+$", "")+"_"+filename; - try (FileOutputStream fos = new FileOutputStream(new File(outdir, filename))) { + try (OutputStream fos = Files.newOutputStream(new File(outdir, filename).toPath())) { fos.write(ep.getData().get()); } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/util/SVGFormat.java b/poi-ooxml/src/main/java/org/apache/poi/xslf/util/SVGFormat.java index 3122855bde..3d8d119810 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xslf/util/SVGFormat.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/util/SVGFormat.java @@ -22,10 +22,10 @@ package org.apache.poi.xslf.util; import java.awt.Dimension; import java.awt.Graphics2D; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import org.apache.batik.dom.GenericDOMImplementation; import org.apache.batik.svggen.SVGGraphics2D; @@ -62,7 +62,7 @@ public class SVGFormat implements OutputFormat { public void writeSlide(MFProxy proxy, File outFile) throws IOException { // Batik DEFAULT_XML_ENCODING is ISO-8859-1 ... srsly?! // Unicode entities aren't encoded, so use UTF-8 - try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(outFile.getCanonicalPath()), StandardCharsets.UTF_8)) { + try (OutputStreamWriter writer = new OutputStreamWriter(Files.newOutputStream(outFile.toPath()), StandardCharsets.UTF_8)) { svgGenerator.stream(writer, true); } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java index d8fe3c16f7..4483995cfb 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java @@ -18,7 +18,6 @@ package org.apache.poi.xssf.streaming; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -27,6 +26,7 @@ import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.Enumeration; import java.util.HashMap; import java.util.Iterator; @@ -941,7 +941,7 @@ public class SXSSFWorkbook implements Workbook { File tmplFile = TempFile.createTempFile("poi-sxssf-template", ".xlsx"); boolean deleted; try { - try (FileOutputStream os = new FileOutputStream(tmplFile)) { + try (OutputStream os = Files.newOutputStream(tmplFile.toPath())) { _wb.write(os); } diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hmef/dev/HMEFDumper.java b/poi-scratchpad/src/main/java/org/apache/poi/hmef/dev/HMEFDumper.java index 72f40b65f9..5e2517f193 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hmef/dev/HMEFDumper.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hmef/dev/HMEFDumper.java @@ -17,9 +17,10 @@ package org.apache.poi.hmef.dev; -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.List; import org.apache.poi.hmef.HMEFMessage; @@ -67,7 +68,7 @@ public final class HMEFDumper { continue; } - try (InputStream stream = new FileInputStream(arg)) { + try (InputStream stream = Files.newInputStream(Paths.get(arg))) { HMEFDumper dumper = new HMEFDumper(stream); dumper.setTruncatePropertyData(truncatePropData); dumper.dump(); diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java b/poi-scratchpad/src/main/java/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java index aacfb1a053..59cbe79e91 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hmef/extractor/HMEFContentsExtractor.java @@ -18,11 +18,10 @@ package org.apache.poi.hmef.extractor; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.nio.file.Files; import org.apache.poi.hmef.Attachment; import org.apache.poi.hmef.HMEFMessage; @@ -70,8 +69,9 @@ public final class HMEFContentsExtractor { } private final HMEFMessage message; - public HMEFContentsExtractor(File filename) throws IOException { - this(new HMEFMessage(new FileInputStream(filename))); + + public HMEFContentsExtractor(File file) throws IOException { + this(new HMEFMessage(Files.newInputStream(file.toPath()))); } public HMEFContentsExtractor(HMEFMessage message) { this.message = message; @@ -94,7 +94,7 @@ public final class HMEFContentsExtractor { dest = new File(name + ".txt"); } - try (OutputStream fout = new FileOutputStream(dest)) { + try (OutputStream fout = Files.newOutputStream(dest.toPath())) { if (body instanceof MAPIStringAttribute) { // Save in a predictable encoding, not raw bytes String text = ((MAPIStringAttribute) body).getDataString(); @@ -153,7 +153,7 @@ public final class HMEFContentsExtractor { // Save it File file = new File(dir, filename); - try (OutputStream fout = new FileOutputStream(file)) { + try (OutputStream fout = Files.newOutputStream(file.toPath())) { fout.write(att.getContents()); } } diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hpbf/dev/PLCDumper.java b/poi-scratchpad/src/main/java/org/apache/poi/hpbf/dev/PLCDumper.java index 3c222d51b2..74dc8edbe3 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hpbf/dev/PLCDumper.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hpbf/dev/PLCDumper.java @@ -17,9 +17,10 @@ package org.apache.poi.hpbf.dev; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.poi.hpbf.HPBFDocument; import org.apache.poi.hpbf.model.QuillContents; @@ -54,7 +55,7 @@ public final class PLCDumper { System.exit(1); } - try (FileInputStream fis = new FileInputStream(args[0])) { + try (InputStream fis = Files.newInputStream(Paths.get(args[0]))) { PLCDumper dump = new PLCDumper(fis); System.out.println("Dumping " + args[0]); diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hslf/dev/PPTXMLDump.java b/poi-scratchpad/src/main/java/org/apache/poi/hslf/dev/PPTXMLDump.java index e27f5e3b93..f6b523fcc3 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/dev/PPTXMLDump.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/dev/PPTXMLDump.java @@ -19,12 +19,14 @@ package org.apache.poi.hslf.dev; import java.io.BufferedWriter; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.Arrays; import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; @@ -214,7 +216,7 @@ public final class PPTXMLDump { System.out.println("Dumping " + arg); if (outFile) { - FileOutputStream fos = new FileOutputStream(ppt.getName() + ".xml"); + OutputStream fos = Files.newOutputStream(Paths.get(ppt.getName() + ".xml")); OutputStreamWriter out = new OutputStreamWriter(fos, StandardCharsets.UTF_8); dump.dump(out); out.close(); diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hslf/extractor/ImageExtractor.java b/poi-scratchpad/src/main/java/org/apache/poi/hslf/extractor/ImageExtractor.java index 926aada0ec..298d349674 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/extractor/ImageExtractor.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/extractor/ImageExtractor.java @@ -17,8 +17,10 @@ package org.apache.poi.hslf.extractor; -import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import org.apache.poi.hslf.usermodel.HSLFPictureData; import org.apache.poi.hslf.usermodel.HSLFSlideShow; @@ -44,7 +46,8 @@ public final class ImageExtractor { byte[] data = pict.getData(); PictureType type = pict.getType(); - try (FileOutputStream out = new FileOutputStream("pict_" + i++ + type.extension)) { + try (OutputStream out = Files.newOutputStream( + Paths.get("pict_" + i++ + type.extension))) { out.write(data); } } diff --git a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSlideShow.java b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSlideShow.java index 15476e48f3..8fd7a1d40a 100644 --- a/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSlideShow.java +++ b/poi-scratchpad/src/main/java/org/apache/poi/hslf/usermodel/HSLFSlideShow.java @@ -22,10 +22,10 @@ import static org.apache.logging.log4j.util.Unbox.box; import java.awt.Dimension; import java.io.Closeable; 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; @@ -877,7 +877,7 @@ public final class HSLFSlideShow extends POIDocument implements SlideShow 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() + "'");