Skip to content

Commit

Permalink
use files nio APIs in more places
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1912367 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
pjfanning committed Sep 17, 2023
1 parent 8514b65 commit 4299690
Show file tree
Hide file tree
Showing 30 changed files with 94 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -93,7 +95,7 @@ public static void main(String[] args) throws IOException {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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;
Expand Down Expand Up @@ -82,7 +83,8 @@ private static void handleFile(File file, File outFile) throws IOException {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -130,8 +131,8 @@ private static void copyToFile(InputStream is, File tmpFile, byte[] keyBytes, by
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ Licensed to the Apache Software Foundation (ASF) under one or more

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;
Expand All @@ -46,7 +47,7 @@ public static void printHierarchy(XDGFPage page, File outDir)
+ 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);
Expand Down Expand Up @@ -91,7 +92,7 @@ public static void main(String[] args) throws Exception {
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);
}
Expand Down
10 changes: 6 additions & 4 deletions poi-ooxml/src/main/java/org/apache/poi/xdgf/util/VsdxToPng.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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;

Expand Down Expand Up @@ -94,7 +96,7 @@ public static void renderToPng(XDGFPage page, File outFile, double scale,

graphics.dispose();

try (FileOutputStream out = new FileOutputStream(outFile)) {
try (OutputStream out = Files.newOutputStream(outFile.toPath())) {
ImageIO.write(img, "png", out);
}
}
Expand Down Expand Up @@ -127,7 +129,7 @@ public static void main(String[] args) throws Exception {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ Licensed to the Apache Software Foundation (ASF) under one or more

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;

Expand All @@ -49,7 +50,7 @@ public Graphics2D addSlide(double width, double height) {

@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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
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;
Expand Down Expand Up @@ -426,7 +427,7 @@ private void extractEmbedded(MFProxy proxy, int slideNo) throws IOException {
// 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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
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;
Expand Down Expand Up @@ -62,7 +62,7 @@ public Graphics2D addSlide(double width, double height) {
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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;
Expand All @@ -27,6 +26,7 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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;
Expand Down Expand Up @@ -941,7 +941,7 @@ public void write(OutputStream stream) throws IOException {
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ Licensed to the Apache Software Foundation (ASF) under one or more

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;
Expand Down Expand Up @@ -67,7 +68,7 @@ public static void main(String[] args) throws Exception {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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;
Expand Down Expand Up @@ -70,8 +69,9 @@ public static void main(String[] args) throws IOException {
}

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;
Expand All @@ -94,7 +94,7 @@ public void extractMessageBody(File dest) throws IOException {
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();
Expand Down Expand Up @@ -153,7 +153,7 @@ public void extractAttachments(File dir) throws IOException {

// 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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ Licensed to the Apache Software Foundation (ASF) under one or more

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;
Expand Down Expand Up @@ -54,7 +55,7 @@ public static void main(String[] args) throws Exception {
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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ Licensed to the Apache Software Foundation (ASF) under one or more

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;
Expand Down Expand Up @@ -214,7 +216,7 @@ public static void main(String[] args) throws Exception {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ Licensed to the Apache Software Foundation (ASF) under one or more

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;
Expand All @@ -44,7 +46,8 @@ public static void main(String[] args) throws IOException {
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ Licensed to the Apache Software Foundation (ASF) under one or more
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;
Expand Down Expand Up @@ -877,7 +877,7 @@ public HSLFPictureData addPicture(File pict, PictureType format) throws IOExcept
throw new IllegalArgumentException("Unsupported picture format: " + format);
}
byte[] data = IOUtils.safelyAllocate(pict.length(), MAX_RECORD_LENGTH);
try (FileInputStream is = new FileInputStream(pict)) {
try (InputStream is = Files.newInputStream(pict.toPath())) {
IOUtils.readFully(is, data);
}
return addPicture(data, format);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ Licensed to the Apache Software Foundation (ASF) under one or more
package org.apache.poi.hssf.converter;

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.hssf.usermodel.HSSFWorkbook;
Expand Down Expand Up @@ -181,7 +182,7 @@ static boolean isNotEmpty(String str) {
}

public static HSSFWorkbook loadXls(File xlsFile) throws IOException {
try (final FileInputStream inputStream = new FileInputStream(xlsFile)) {
try (final InputStream inputStream = Files.newInputStream(xlsFile.toPath())) {
return new HSSFWorkbook(inputStream);
}
}
Expand Down
Loading

0 comments on commit 4299690

Please sign in to comment.