embrace Path
Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/1a377507 Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/1a377507 Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/1a377507 Branch: refs/heads/compress-2.0 Commit: 1a377507a839e627ddc2596264c486ff1ed54ba9 Parents: c6eed14 Author: Stefan Bodewig <bode...@apache.org> Authored: Sun Mar 27 15:59:37 2016 +0200 Committer: Stefan Bodewig <bode...@apache.org> Committed: Sun Mar 27 15:59:37 2016 +0200 ---------------------------------------------------------------------- .../archivers/ArchiveEntryParameters.java | 18 +++++++++--------- .../compress2/archivers/ArchiveFormat.java | 8 +++----- .../archivers/spi/AbstractArchiveFormat.java | 12 ++++++------ .../archivers/ArchiveEntryParametersTest.java | 6 +++--- .../compress2/formats/ar/RoundTripTest.java | 18 +++++++++--------- 5 files changed, 30 insertions(+), 32 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/1a377507/src/main/java/org/apache/commons/compress2/archivers/ArchiveEntryParameters.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress2/archivers/ArchiveEntryParameters.java b/src/main/java/org/apache/commons/compress2/archivers/ArchiveEntryParameters.java index 3147242..a91e7fd 100644 --- a/src/main/java/org/apache/commons/compress2/archivers/ArchiveEntryParameters.java +++ b/src/main/java/org/apache/commons/compress2/archivers/ArchiveEntryParameters.java @@ -18,9 +18,9 @@ */ package org.apache.commons.compress2.archivers; -import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.LinkOption; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileTime; @@ -63,19 +63,19 @@ public class ArchiveEntryParameters implements ArchiveEntry { } /** - * Populates parameters from a File instance. - * @param file the File to read information from + * Populates parameters from a Path instance. + * @param path the Path to read information from + * @param options options indicating how symbolic links are handled * @return parameters populated from the file instance */ - public static ArchiveEntryParameters fromFile(File file) throws IOException { - Path path = file.toPath(); + public static ArchiveEntryParameters fromPath(Path path, LinkOption... options) throws IOException { ArchiveEntryParameters params = new ArchiveEntryParameters() - .withName(file.getName()); - if (file.exists()) { + .withName(path.getFileName().toString()); + if (Files.exists(path, options)) { params = params - .withAttributes(Files.readAttributes(path, BasicFileAttributes.class)); + .withAttributes(Files.readAttributes(path, BasicFileAttributes.class, options)); try { - params = params.withPermissions(Files.readAttributes(path, PosixFileAttributes.class) + params = params.withPermissions(Files.readAttributes(path, PosixFileAttributes.class, options) .permissions()); } catch (UnsupportedOperationException ex) { // file system without support for POSIX attributes http://git-wip-us.apache.org/repos/asf/commons-compress/blob/1a377507/src/main/java/org/apache/commons/compress2/archivers/ArchiveFormat.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress2/archivers/ArchiveFormat.java b/src/main/java/org/apache/commons/compress2/archivers/ArchiveFormat.java index f527ed4..ca1ecfa 100644 --- a/src/main/java/org/apache/commons/compress2/archivers/ArchiveFormat.java +++ b/src/main/java/org/apache/commons/compress2/archivers/ArchiveFormat.java @@ -23,7 +23,7 @@ import java.nio.channels.ReadableByteChannel; import java.nio.channels.SeekableByteChannel; import java.nio.channels.WritableByteChannel; import java.nio.charset.Charset; -import java.io.File; +import java.nio.file.Path; import java.io.IOException; /** @@ -93,8 +93,7 @@ public interface ArchiveFormat<A extends ArchiveEntry> { * @param charset the charset used for encoding the entry names. * @throws IOException */ - // TODO use Path rather than File? - ArchiveInput<A> readFrom(File file, Charset charset) throws IOException; + ArchiveInput<A> readFrom(Path path, Charset charset) throws IOException; /** * Provides random access to an archive assuming the given charset for entry names. * @param channel the seekable channel to read from @@ -122,6 +121,5 @@ public interface ArchiveFormat<A extends ArchiveEntry> { * @throws IOException * @throws UnsupportedOperationException if this format doesn't support writing */ - // TODO use Path rather than File? - ArchiveOutput<A> writeTo(File file, Charset charset) throws IOException, UnsupportedOperationException; + ArchiveOutput<A> writeTo(Path path, Charset charset) throws IOException, UnsupportedOperationException; } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/1a377507/src/main/java/org/apache/commons/compress2/archivers/spi/AbstractArchiveFormat.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress2/archivers/spi/AbstractArchiveFormat.java b/src/main/java/org/apache/commons/compress2/archivers/spi/AbstractArchiveFormat.java index eef9b10..3ee071c 100644 --- a/src/main/java/org/apache/commons/compress2/archivers/spi/AbstractArchiveFormat.java +++ b/src/main/java/org/apache/commons/compress2/archivers/spi/AbstractArchiveFormat.java @@ -18,7 +18,6 @@ */ package org.apache.commons.compress2.archivers.spi; -import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; @@ -26,6 +25,7 @@ import java.nio.channels.SeekableByteChannel; import java.nio.channels.WritableByteChannel; import java.nio.channels.FileChannel; import java.nio.charset.Charset; +import java.nio.file.Path; import java.nio.file.StandardOpenOption; import org.apache.commons.compress2.archivers.ArchiveEntry; import org.apache.commons.compress2.archivers.ArchiveFormat; @@ -99,11 +99,11 @@ public abstract class AbstractArchiveFormat<A extends ArchiveEntry> implements A /** * {@inheritDoc} * <p>This implementation delegates to {@link #readWithRandomAccessFrom} if random access is supported or {@link - * #readFrom(File, Charset)} otherwise.</p> + * #readFrom(ReadableByteChannel, Charset)} otherwise.</p> */ @Override - public ArchiveInput<A> readFrom(File file, Charset charset) throws IOException { - SeekableByteChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.READ); + public ArchiveInput<A> readFrom(Path path, Charset charset) throws IOException { + SeekableByteChannel channel = FileChannel.open(path, StandardOpenOption.READ); if (supportsRandomAccessInput()) { return readWithRandomAccessFrom(channel, charset); } @@ -134,8 +134,8 @@ public abstract class AbstractArchiveFormat<A extends ArchiveEntry> implements A * <p>This implementation always delegates to {@link #writeTo(Channel, Charset)}.</p> */ @Override - public ArchiveOutput<A> writeTo(File file, Charset charset) throws IOException, UnsupportedOperationException { - return writeTo(FileChannel.open(file.toPath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE, + public ArchiveOutput<A> writeTo(Path path, Charset charset) throws IOException, UnsupportedOperationException { + return writeTo(FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING), charset); } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/1a377507/src/test/java/org/apache/commons/compress2/archivers/ArchiveEntryParametersTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress2/archivers/ArchiveEntryParametersTest.java b/src/test/java/org/apache/commons/compress2/archivers/ArchiveEntryParametersTest.java index c6f81b3..0e1f074 100644 --- a/src/test/java/org/apache/commons/compress2/archivers/ArchiveEntryParametersTest.java +++ b/src/test/java/org/apache/commons/compress2/archivers/ArchiveEntryParametersTest.java @@ -145,7 +145,7 @@ public class ArchiveEntryParametersTest { File f = File.createTempFile("pre", "suf"); f.deleteOnExit(); f.setLastModified(d.toEpochMilli()); - ArchiveEntryParameters p = ArchiveEntryParameters.fromFile(f); + ArchiveEntryParameters p = ArchiveEntryParameters.fromPath(f.toPath()); assert p.getName().endsWith("suf"); assert p.getName().startsWith("pre"); assertEquals(0, p.size()); @@ -162,7 +162,7 @@ public class ArchiveEntryParametersTest { f.mkdirs(); f.deleteOnExit(); f.setLastModified(d.toEpochMilli()); - ArchiveEntryParameters p = ArchiveEntryParameters.fromFile(f); + ArchiveEntryParameters p = ArchiveEntryParameters.fromPath(f.toPath()); assert p.getName().endsWith("suf/"); assert p.getName().startsWith("pre"); assertEquals(0, p.size()); @@ -175,7 +175,7 @@ public class ArchiveEntryParametersTest { public void fromNonExistingFileHasNoSize() throws IOException { File f = File.createTempFile("pre", "suf"); assert f.delete(); - ArchiveEntryParameters p = ArchiveEntryParameters.fromFile(f); + ArchiveEntryParameters p = ArchiveEntryParameters.fromPath(f.toPath()); assertEquals(-1, p.size()); } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/1a377507/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java b/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java index 491c964..77ad80f 100644 --- a/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java +++ b/src/test/java/org/apache/commons/compress2/formats/ar/RoundTripTest.java @@ -62,11 +62,11 @@ public class RoundTripTest { final WritableByteChannel out = new FileOutputStream(output).getChannel(); final ArArchiveOutput os = new ArArchiveOutput(out); IOUtils.copy(new FileInputStream(file1).getChannel(), - os.putEntry(os.createEntry(ArchiveEntryParameters.fromFile(file1)))); + os.putEntry(os.createEntry(ArchiveEntryParameters.fromPath(file1.toPath())))); os.closeEntry(); IOUtils.copy(new FileInputStream(file2).getChannel(), - os.putEntry(os.createEntry(ArchiveEntryParameters.fromFile(file2)))); + os.putEntry(os.createEntry(ArchiveEntryParameters.fromPath(file2.toPath())))); os.closeEntry(); os.close(); out.close(); @@ -103,11 +103,11 @@ public class RoundTripTest { final WritableByteChannel out = new FileOutputStream(output).getChannel(); final ArchiveOutput<ArArchiveEntry> os = format.writeTo(out, null); IOUtils.copy(new FileInputStream(file1).getChannel(), - os.putEntry(os.createEntry(ArchiveEntryParameters.fromFile(file1)))); + os.putEntry(os.createEntry(ArchiveEntryParameters.fromPath(file1.toPath())))); os.closeEntry(); IOUtils.copy(new FileInputStream(file2).getChannel(), - os.putEntry(os.createEntry(ArchiveEntryParameters.fromFile(file2)))); + os.putEntry(os.createEntry(ArchiveEntryParameters.fromPath(file2.toPath())))); os.closeEntry(); os.close(); out.close(); @@ -134,27 +134,27 @@ public class RoundTripTest { } @Test - public void testRoundtripUsingFormatInstanceAndFiles() throws Exception { + public void testRoundtripUsingFormatInstanceAndPaths() throws Exception { ArArchiveFormat format = new ArArchiveFormat(); final File output = new File(dir, "format-files.ar"); { final File file1 = getFile("test1.xml"); final File file2 = getFile("test2.xml"); - final ArchiveOutput<ArArchiveEntry> os = format.writeTo(output, null); + final ArchiveOutput<ArArchiveEntry> os = format.writeTo(output.toPath(), null); IOUtils.copy(new FileInputStream(file1).getChannel(), - os.putEntry(os.createEntry(ArchiveEntryParameters.fromFile(file1)))); + os.putEntry(os.createEntry(ArchiveEntryParameters.fromPath(file1.toPath())))); os.closeEntry(); IOUtils.copy(new FileInputStream(file2).getChannel(), - os.putEntry(os.createEntry(ArchiveEntryParameters.fromFile(file2)))); + os.putEntry(os.createEntry(ArchiveEntryParameters.fromPath(file2.toPath())))); os.closeEntry(); os.close(); } // UnArArchive Operation final File input = output; - final ArchiveInput<ArArchiveEntry> in = format.readFrom(input, null); + final ArchiveInput<ArArchiveEntry> in = format.readFrom(input.toPath(), null); ArArchiveEntry entry = in.next(); Assert.assertEquals("test1.xml", entry.getName());