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;
}
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);
}
}
}
// Do the save
- try (FileOutputStream fos = new FileOutputStream(targetFile)) {
+ try (OutputStream fos = Files.newOutputStream(targetFile.toPath())) {
this.save(fos);
}
}
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;
}
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);
}
}
}
- 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 {
import org.apache.poi.util.TempFile;
import java.io.*;
+import java.nio.file.Files;
/**
* (Experimental) Temp File version of a package part.
@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
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;
}
// Peek at the first few bytes to sanity check
- try (FileInputStream input = new FileInputStream(file)) {
+ try (InputStream input = Files.newInputStream(file.toPath())) {
verifyZipHeader(input);
}
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;
}
} 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");
}
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;
*/
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;
}
*/
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);
}
/**
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;
@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);
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;
}
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);
}
}
* @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);
}
}
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;
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)
) {
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;
}
@SuppressWarnings("resource")
- FileInputStream biffStream = new FileInputStream(f); // NOSONAR
+ InputStream biffStream = Files.newInputStream(f.toPath());
try {
open(biffStream);
} catch (IOException | RuntimeException e) {
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;
}
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
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;
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);
}
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.
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;
}
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();
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){
}
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());
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;
* @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);
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.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;
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);
}
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);
}
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;
*/
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);
}
}
}
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")
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;
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);
}
}
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;
w.write(buf, 0, idx);
} catch (IOException e) {
- throw new RuntimeException(e);
+ throw new UncheckedIOException(e);
}
}
import java.io.IOException;
import java.io.InputStream;
+import java.io.UncheckedIOException;
import java.util.ArrayList;
import java.util.List;
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);
} 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();
import java.io.BufferedReader;
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.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;
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);
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;
}
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;
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) {
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) {
is.close();
os.close();
} catch (IOException e) {
- throw new RuntimeException(e);
+ throw new UncheckedIOException(e);
}
System.out.println("file downloaded ok");
return result;
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;
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();
os.write(fileContent);
} catch (IOException e) {
- throw new RuntimeException(e);
+ throw new UncheckedIOException(e);
}
System.out.println("Finished writing '" + outputFile.getAbsolutePath() + "'");