Repository: commons-compress Updated Branches: refs/heads/master b15221d6f -> fe2ba8d32
COMPRESS-118 make source a first-class abstraction Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/fe2ba8d3 Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/fe2ba8d3 Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/fe2ba8d3 Branch: refs/heads/master Commit: fe2ba8d320a10a988086378be2ae8621c55a6dab Parents: b15221d Author: Stefan Bodewig <bode...@apache.org> Authored: Sun Apr 29 22:36:36 2018 +0200 Committer: Stefan Bodewig <bode...@apache.org> Committed: Sun Apr 29 22:36:36 2018 +0200 ---------------------------------------------------------------------- .../compress/archivers/examples/Archive.java | 25 +++-- .../examples/DirectoryBasedSource.java | 101 +++++++++++++++++++ .../examples/DirectoryBasedSupplier.java | 98 ------------------ .../compress/archivers/examples/Source.java | 28 +++++ 4 files changed, 141 insertions(+), 111 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/fe2ba8d3/src/main/java/org/apache/commons/compress/archivers/examples/Archive.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/Archive.java b/src/main/java/org/apache/commons/compress/archivers/examples/Archive.java index 2247fcc..5ce777a 100644 --- a/src/main/java/org/apache/commons/compress/archivers/examples/Archive.java +++ b/src/main/java/org/apache/commons/compress/archivers/examples/Archive.java @@ -29,7 +29,7 @@ import org.apache.commons.compress.archivers.ArchiveException; */ public class Archive { /** - * Sets up a chain of operations and consumes the files from a supplier of files. + * Sets up a chain of operations and consumes the files from a source of files. * @since 1.17 */ public interface ChainBuilder { @@ -63,22 +63,22 @@ public class Archive { * Sets the source of files to be a directory. */ public static ChainBuilder directory(File f) { - return source(new DirectoryBasedSupplier(f)); + return source(new DirectoryBasedSource(f)); } /** * Sets the source of files to process. */ - public static ChainBuilder source(Supplier<ThrowingIterator<ChainPayload<File>>> supplier) { - return new Builder(supplier); + public static ChainBuilder source(Source<File> source) { + return new Builder(source); } private static class Builder implements ChainBuilder { - private final Supplier<ThrowingIterator<ChainPayload<File>>> supplier; + private final Source<File> source; private ChainDefinition<File> chainDef = new ChainDefinition<>(); - Builder(Supplier<ThrowingIterator<ChainPayload<File>>> supplier) { - this.supplier = supplier; + Builder(Source<File> source) { + this.source = source; } public ChainBuilder filter(Filter<File> filter) { @@ -111,23 +111,22 @@ public class Archive { public void to(Sink<File> sink) throws IOException, ArchiveException { chainDef.add(sink); chainDef.freeze(); - new Archive(supplier, chainDef, sink).run(); + new Archive(source, chainDef, sink).run(); } } - private final Supplier<ThrowingIterator<ChainPayload<File>>> supplier; + private final Source<File> source; private final ChainDefinition<File> chainDef; private final Sink<File> sink; - private Archive(Supplier<ThrowingIterator<ChainPayload<File>>> supplier, ChainDefinition<File> chainDef, - Sink<File> sink) { - this.supplier = supplier; + private Archive(Source<File> source, ChainDefinition<File> chainDef, Sink<File> sink) { + this.source = source; this.chainDef = chainDef; this.sink = sink; } private void run() throws IOException, ArchiveException { - ThrowingIterator<ChainPayload<File>> iter = supplier.get(); + ThrowingIterator<ChainPayload<File>> iter = source.get(); while (iter.hasNext()) { chainDef.chain().next(iter.next()); } http://git-wip-us.apache.org/repos/asf/commons-compress/blob/fe2ba8d3/src/main/java/org/apache/commons/compress/archivers/examples/DirectoryBasedSource.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/DirectoryBasedSource.java b/src/main/java/org/apache/commons/compress/archivers/examples/DirectoryBasedSource.java new file mode 100644 index 0000000..84d1a53 --- /dev/null +++ b/src/main/java/org/apache/commons/compress/archivers/examples/DirectoryBasedSource.java @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.compress.archivers.examples; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.NoSuchElementException; + +/** + * Recursively returns all files and directories contained inside of a base directory. + * @since 1.17 + */ +public class DirectoryBasedSource implements Source<File> { + + private final File dir; + + /** + * @param dir the directory to provide entries from. + */ + public DirectoryBasedSource(File dir) { + if (!dir.isDirectory()) { + throw new IllegalArgumentException("dir is not a readable directory"); + } + this.dir = dir; + } + + @Override + public ThrowingIterator<ChainPayload<File>> get() throws IOException { + return new DirectoryIterator("", dir); + } + + @Override + public void close() { + } + + private static class DirectoryIterator implements ThrowingIterator<ChainPayload<File>> { + private final Iterator<File> files; + private final String namePrefix; + private DirectoryIterator nestedIterator; + DirectoryIterator(String namePrefix, File dir) throws IOException { + this.namePrefix = namePrefix; + File[] fs = dir.listFiles(); + files = fs == null ? Collections.<File>emptyIterator() : Arrays.asList(fs).iterator(); + } + + @Override + public boolean hasNext() throws IOException { + if (nestedIterator != null && nestedIterator.hasNext()) { + return true; + } + if (nestedIterator != null) { + nestedIterator = null; + } + return files.hasNext(); + } + + @Override + public ChainPayload<File> next() throws IOException { + if (!hasNext()) { + throw new NoSuchElementException(); + } + if (nestedIterator != null) { + return nestedIterator.next(); + } + final File f = files.next(); + String entryName = namePrefix + f.getName(); + if (f.isDirectory()) { + entryName += "/"; + nestedIterator = new DirectoryIterator(entryName, f); + } + return new ChainPayload(f, entryName, new Supplier<InputStream>() { + @Override + public InputStream get() throws IOException { + return new FileInputStream(f); + } + }); + } + + } +} http://git-wip-us.apache.org/repos/asf/commons-compress/blob/fe2ba8d3/src/main/java/org/apache/commons/compress/archivers/examples/DirectoryBasedSupplier.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/DirectoryBasedSupplier.java b/src/main/java/org/apache/commons/compress/archivers/examples/DirectoryBasedSupplier.java deleted file mode 100644 index 1b8cc91..0000000 --- a/src/main/java/org/apache/commons/compress/archivers/examples/DirectoryBasedSupplier.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.commons.compress.archivers.examples; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.Arrays; -import java.util.Collections; -import java.util.Iterator; -import java.util.NoSuchElementException; - -/** - * Recursively returns all files and directories contained inside of a base directory. - * @since 1.17 - */ -public class DirectoryBasedSupplier - implements Supplier<ThrowingIterator<ChainPayload<File>>> { - - private final File dir; - - /** - * @param dir the directory to provide entries from. - */ - public DirectoryBasedSupplier(File dir) { - if (!dir.isDirectory()) { - throw new IllegalArgumentException("dir is not a readable directory"); - } - this.dir = dir; - } - - @Override - public ThrowingIterator<ChainPayload<File>> get() throws IOException { - return new DirectoryIterator("", dir); - } - - private static class DirectoryIterator implements ThrowingIterator<ChainPayload<File>> { - private final Iterator<File> files; - private final String namePrefix; - private DirectoryIterator nestedIterator; - DirectoryIterator(String namePrefix, File dir) throws IOException { - this.namePrefix = namePrefix; - File[] fs = dir.listFiles(); - files = fs == null ? Collections.<File>emptyIterator() : Arrays.asList(fs).iterator(); - } - - @Override - public boolean hasNext() throws IOException { - if (nestedIterator != null && nestedIterator.hasNext()) { - return true; - } - if (nestedIterator != null) { - nestedIterator = null; - } - return files.hasNext(); - } - - @Override - public ChainPayload<File> next() throws IOException { - if (!hasNext()) { - throw new NoSuchElementException(); - } - if (nestedIterator != null) { - return nestedIterator.next(); - } - final File f = files.next(); - String entryName = namePrefix + f.getName(); - if (f.isDirectory()) { - entryName += "/"; - nestedIterator = new DirectoryIterator(entryName, f); - } - return new ChainPayload(f, entryName, new Supplier<InputStream>() { - @Override - public InputStream get() throws IOException { - return new FileInputStream(f); - } - }); - } - - } -} http://git-wip-us.apache.org/repos/asf/commons-compress/blob/fe2ba8d3/src/main/java/org/apache/commons/compress/archivers/examples/Source.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/archivers/examples/Source.java b/src/main/java/org/apache/commons/compress/archivers/examples/Source.java new file mode 100644 index 0000000..4a51efe --- /dev/null +++ b/src/main/java/org/apache/commons/compress/archivers/examples/Source.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.compress.archivers.examples; + +import java.io.Closeable; + +/** + * Describes the contract of a source for {@link Archive} or {@link Expand}. + * @since 1.17 + */ +public interface Source<T> extends Supplier<ThrowingIterator<ChainPayload<T>>>, Closeable { +}