Skip to content

Commit

Permalink
Merge pull request #186 from arturobernalg/feature/filesNioApi
Browse files Browse the repository at this point in the history
Replace construction of FileInputStream and FileOutputStream objects with Files NIO APIs.
  • Loading branch information
bodewig committed Apr 11, 2021
2 parents 33789f6 + 735e99a commit 84b8b7a
Show file tree
Hide file tree
Showing 74 changed files with 621 additions and 633 deletions.
Expand Up @@ -20,9 +20,9 @@
import org.apache.commons.compress.utils.FileNameUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;

/**
* Used internally by {@link ZipArchiveOutputStream} when creating a split archive.
Expand Down Expand Up @@ -65,7 +65,7 @@ public ZipSplitOutputStream(final File zipFile, final long splitSize) throws Ill
this.zipFile = zipFile;
this.splitSize = splitSize;

this.outputStream = new FileOutputStream(zipFile);
this.outputStream = Files.newOutputStream(zipFile.toPath());
// write the zip split signature 0x08074B50 to the zip file
writeZipSplitSignature();
}
Expand Down Expand Up @@ -176,7 +176,7 @@ private void openNewSplitSegment() throws IOException {
newFile = createNewSplitSegmentFile(null);

outputStream.close();
outputStream = new FileOutputStream(newFile);
outputStream = Files.newOutputStream(newFile.toPath());
currentSplitSegmentBytesWritten = 0;
zipFile = newFile;
currentSplitSegmentIndex++;
Expand Down
Expand Up @@ -20,8 +20,9 @@
package org.apache.commons.compress.compressors.pack200;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -132,14 +133,14 @@ public static void normalize(final File from, final File to, Map<String, String>
props.put(Pack200.Packer.SEGMENT_LIMIT, "-1");
final File tempFile = File.createTempFile("commons-compress", "pack200normalize");
try {
try (FileOutputStream fos = new FileOutputStream(tempFile);
JarFile jarFile = new JarFile(from)) {
try (OutputStream fos = Files.newOutputStream(tempFile.toPath());
JarFile jarFile = new JarFile(from)) {
final Pack200.Packer packer = Pack200.newPacker();
packer.properties().putAll(props);
packer.pack(jarFile, fos);
}
final Pack200.Unpacker unpacker = Pack200.newUnpacker();
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(to))) {
try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(to.toPath()))) {
unpacker.unpack(tempFile, jos);
}
} finally {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/apache/commons/compress/utils/IOUtils.java
Expand Up @@ -22,7 +22,6 @@
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.OutputStream;
Expand Down Expand Up @@ -151,7 +150,7 @@ public static long skip(final InputStream input, long numToSkip) throws IOExcept
* @since 1.20
*/
public static int read(final File file, final byte[] array) throws IOException {
try (FileInputStream inputStream = new FileInputStream(file)) {
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
return readFully(inputStream, array, 0, array.length);
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/test/java/org/apache/commons/compress/AbstractTestCase.java
Expand Up @@ -24,9 +24,7 @@
import java.io.BufferedInputStream;
import java.io.Closeable;
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;
Expand Down Expand Up @@ -175,7 +173,7 @@ protected File createArchive(final String archivename) throws Exception {
archive.deleteOnExit();
archiveList = new ArrayList<>();

stream = new FileOutputStream(archive);
stream = Files.newOutputStream(archive.toPath());
out = factory.createArchiveOutputStream(archivename, stream);

final File file1 = getFile("test1.xml");
Expand Down Expand Up @@ -237,7 +235,7 @@ protected File createEmptyArchive(final String archivename) throws Exception {
try {
archive = File.createTempFile("empty", "." + archivename);
archive.deleteOnExit();
stream = new FileOutputStream(archive);
stream = Files.newOutputStream(archive.toPath());
out = factory.createArchiveOutputStream(archivename, stream);
out.finish();
} finally {
Expand All @@ -264,7 +262,7 @@ protected File createSingleEntryArchive(final String archivename) throws Excepti
try {
archive = File.createTempFile("empty", "." + archivename);
archive.deleteOnExit();
stream = new FileOutputStream(archive);
stream = Files.newOutputStream(archive.toPath());
out = factory.createArchiveOutputStream(archivename, stream);
// Use short file name so does not cause problems for ar
addArchiveEntry(out, "test1.xml", getFile("test1.xml"));
Expand All @@ -290,7 +288,7 @@ protected File createSingleEntryArchive(final String archivename) throws Excepti
*/
protected void checkArchiveContent(final File archive, final List<String> expected)
throws Exception {
try (InputStream is = new FileInputStream(archive)) {
try (InputStream is = Files.newInputStream(archive.toPath())) {
final BufferedInputStream buf = new BufferedInputStream(is);
final ArchiveInputStream in = factory.createArchiveInputStream(buf);
this.checkArchiveContent(in, expected);
Expand Down Expand Up @@ -333,7 +331,7 @@ protected File checkArchiveContent(final ArchiveInputStream in, final List<Strin
outfile.mkdirs();
} else {
outfile.getParentFile().mkdirs();
try (OutputStream out = new FileOutputStream(outfile)) {
try (OutputStream out = Files.newOutputStream(outfile.toPath())) {
copied = IOUtils.copy(in, out);
}
}
Expand Down Expand Up @@ -384,7 +382,7 @@ protected File[] createTempDirAndFile() throws IOException {
final File tmpDir = createTempDir();
final File tmpFile = File.createTempFile("testfile", "", tmpDir);
tmpFile.deleteOnExit();
try (FileOutputStream fos = new FileOutputStream(tmpFile)) {
try (OutputStream fos = Files.newOutputStream(tmpFile.toPath())) {
fos.write(new byte[] { 'f', 'o', 'o' });
return new File[] { tmpDir, tmpFile };
}
Expand Down
Expand Up @@ -23,8 +23,8 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;

Expand Down Expand Up @@ -70,7 +70,7 @@ public static void setUpFileList() throws Exception {
assertTrue(ARCDIR.exists());
final File listing = new File(ARCDIR, "files.txt");
assertTrue("files.txt is readable", listing.canRead());
try (final BufferedReader br = new BufferedReader(new FileReader(listing))) {
try (final BufferedReader br = new BufferedReader(Files.newBufferedReader(listing.toPath()))) {
String line;
while ((line = br.readLine()) != null) {
if (!line.startsWith("#")) {
Expand Down
Expand Up @@ -22,7 +22,7 @@
import static org.junit.Assert.assertNotNull;

import java.io.File;
import java.io.FileInputStream;
import java.nio.file.Files;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
Expand All @@ -37,7 +37,7 @@ public class ChainingTestCase extends AbstractTestCase {
public void testTarGzip() throws Exception {
final File file = getFile("bla.tgz");
try (final TarArchiveInputStream is = new TarArchiveInputStream(
new GzipCompressorInputStream(new FileInputStream(file)))) {
new GzipCompressorInputStream(Files.newInputStream(file.toPath())))) {
final TarArchiveEntry entry = (TarArchiveEntry) is.getNextEntry();
assertNotNull(entry);
assertEquals("test1.xml", entry.getName());
Expand All @@ -48,7 +48,7 @@ public void testTarGzip() throws Exception {
public void testTarBzip2() throws Exception {
final File file = getFile("bla.tar.bz2");
try (final TarArchiveInputStream is = new TarArchiveInputStream(
new BZip2CompressorInputStream(new FileInputStream(file)))) {
new BZip2CompressorInputStream(Files.newInputStream(file.toPath())))) {
final TarArchiveEntry entry = (TarArchiveEntry) is.getNextEntry();
assertNotNull(entry);
assertEquals("test1.xml", entry.getName());
Expand Down
Expand Up @@ -24,8 +24,8 @@

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;

import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveInputStream;
Expand Down Expand Up @@ -108,8 +108,7 @@ public void testDetection() throws Exception {
private ArchiveInputStream getStreamFor(final String resource)
throws ArchiveException, IOException {
return factory.createArchiveInputStream(
new BufferedInputStream(new FileInputStream(
getFile(resource))));
new BufferedInputStream(Files.newInputStream(getFile(resource).toPath())));
}

// Check that the empty archives created by the code are readable
Expand Down Expand Up @@ -144,7 +143,7 @@ private void checkEmptyArchive(final String type) throws Exception{
ArchiveInputStream ais = null;
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(ar));
in = new BufferedInputStream(Files.newInputStream(ar.toPath()));
ais = factory.createArchiveInputStream(in);
} catch (final ArchiveException ae) {
fail("Should have recognized empty archive for "+type);
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/apache/commons/compress/IOMethodsTest.java
Expand Up @@ -24,9 +24,9 @@

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;

import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
Expand Down Expand Up @@ -148,7 +148,7 @@ private void compareReads(final String archiverName) throws Exception {
final File file = createSingleEntryArchive(archiverName);
file.deleteOnExit();

final InputStream is1 = new FileInputStream(file);
final InputStream is1 = Files.newInputStream(file.toPath());
final ArchiveInputStream ais1 = factory.createArchiveInputStream(archiverName, is1);
final ArchiveEntry nextEntry = ais1.getNextEntry();
assertNotNull(nextEntry);
Expand All @@ -159,13 +159,13 @@ private void compareReads(final String archiverName) throws Exception {
assertTrue("Size should be > 0, found: "+size, size > 0);
}

final InputStream is2 = new FileInputStream(file);
final InputStream is2 = Files.newInputStream(file.toPath());
final ArchiveInputStream ais2 = factory.createArchiveInputStream(archiverName, is2);
final ArchiveEntry nextEntry2 = ais2.getNextEntry();
assertNotNull(nextEntry2);
assertEquals("Expected same entry size", size, nextEntry2.getSize());

final InputStream is3 = new FileInputStream(file);
final InputStream is3 = Files.newInputStream(file.toPath());
final ArchiveInputStream ais3 = factory.createArchiveInputStream(archiverName, is3);
final ArchiveEntry nextEntry3 = ais3.getNextEntry();
assertNotNull(nextEntry3);
Expand Down

0 comments on commit 84b8b7a

Please sign in to comment.