Repository: commons-compress Updated Branches: refs/heads/compress-2.0 8d714b44d -> 66a824fb5
extract common meta-data of archivers and compressors Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/66a824fb Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/66a824fb Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/66a824fb Branch: refs/heads/compress-2.0 Commit: 66a824fb5793e022ef92e2f899f528f078bfe01b Parents: 8d714b4 Author: Stefan Bodewig <bode...@apache.org> Authored: Sat Apr 29 17:28:12 2017 +0200 Committer: Stefan Bodewig <bode...@apache.org> Committed: Sat Apr 29 17:28:12 2017 +0200 ---------------------------------------------------------------------- .../org/apache/commons/compress2/Format.java | 81 ++++++++++++++++++++ .../compress2/archivers/ArchiveFormat.java | 34 +------- .../commons/compress2/archivers/Archivers.java | 16 +--- 3 files changed, 85 insertions(+), 46 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/66a824fb/src/main/java/org/apache/commons/compress2/Format.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress2/Format.java b/src/main/java/org/apache/commons/compress2/Format.java new file mode 100644 index 0000000..62b18c1 --- /dev/null +++ b/src/main/java/org/apache/commons/compress2/Format.java @@ -0,0 +1,81 @@ +/* + * 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.compress2; + +import java.nio.ByteBuffer; +import java.util.Comparator; + +/** + * Common meta-data for archive and compression formats. + */ +public interface Format { + /** + * The name by which this format is known. + * @return the name by which this format is known + */ + String getName(); + + /** + * Does the format support writing? + * @return whether writing is supported + */ + boolean supportsWriting(); + + /** + * Does the format support content-based detection? + * @return whether the format supports content-based detection. + */ + boolean supportsAutoDetection(); + /** + * If this format supports content-based detection, how many bytes does it need to read to know a channel is + * readable by this format? + * @return the minimal number of bytes needed + * @throws UnsupportedOperationException if this format doesn't support content based detection. + */ + int getNumberOfBytesRequiredForAutodetection() throws UnsupportedOperationException; + /** + * Verifies the given input is readable by this format. + * @param probe a buffer holding at least {@link #getNumberOfBytesRequiredForAutodetection} bytes + * @return whether the input is readable by this format + * @throws UnsupportedOperationException if this format doesn't support content based detection. + */ + boolean matches(ByteBuffer probe) throws UnsupportedOperationException; + + /** + * Comparator that sorts {@link Format}s in ascending order of number of bytes required for aut-detection. + * + * <p>Formats that don't support auto-detection at all are sorted last.</p> + */ + public static final Comparator<Format> AUTO_DETECTION_ORDER = new Comparator<Format>() { + @Override + public int compare(Format f1, Format f2) { + if (f1.supportsAutoDetection() && f2.supportsAutoDetection()) { + return f1.getNumberOfBytesRequiredForAutodetection() - f2.getNumberOfBytesRequiredForAutodetection(); + } + if (!f1.supportsAutoDetection() && !f2.supportsAutoDetection()) { + return 0; + } + if (f1.supportsAutoDetection()) { + return -1; + } + return 1; + } + }; + +} http://git-wip-us.apache.org/repos/asf/commons-compress/blob/66a824fb/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 5dd2a6b..fe4bf72 100644 --- a/src/main/java/org/apache/commons/compress2/archivers/ArchiveFormat.java +++ b/src/main/java/org/apache/commons/compress2/archivers/ArchiveFormat.java @@ -27,23 +27,13 @@ import java.nio.charset.Charset; import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.io.IOException; +import org.apache.commons.compress2.Format; /** * Describes a given archive format and works as factory and content-probe at the same time. * @Immutable */ -public interface ArchiveFormat<A extends ArchiveEntry> { - /** - * The name by which this format is known. - * @return the name by which this format is known - */ - String getName(); - - /** - * Does the format support writing? - * @return whether writing is supported - */ - boolean supportsWriting(); +public interface ArchiveFormat<A extends ArchiveEntry> extends Format { /** * Does the format support random access reading? * @return whether random access reading is supported @@ -61,26 +51,6 @@ public interface ArchiveFormat<A extends ArchiveEntry> { boolean supportsReadingFromNonSeekableChannels(); /** - * Does the format support content-based detection? - * @return whether the format supports content-based detection. - */ - boolean supportsAutoDetection(); - /** - * If this format supports content-based detection, how many bytes does it need to read to know a channel is - * readable by this format? - * @return the minimal number of bytes needed - * @throws UnsupportedOperationException if this format doesn't support content based detection. - */ - int getNumberOfBytesRequiredForAutodetection() throws UnsupportedOperationException; - /** - * Verifies the given input is readable by this format. - * @param probe a buffer holding at least {@link #getNumberOfBytesRequiredForAutodetection} bytes - * @return whether the input is readable by this format - * @throws UnsupportedOperationException if this format doesn't support content based detection. - */ - boolean matches(ByteBuffer probe) throws UnsupportedOperationException; - - /** * Reads an archive assuming the given charset for entry names. * @param channel the channel to read from * @param charset the charset used for encoding the entry names. http://git-wip-us.apache.org/repos/asf/commons-compress/blob/66a824fb/src/main/java/org/apache/commons/compress2/archivers/Archivers.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress2/archivers/Archivers.java b/src/main/java/org/apache/commons/compress2/archivers/Archivers.java index a9e910e..26f527c 100644 --- a/src/main/java/org/apache/commons/compress2/archivers/Archivers.java +++ b/src/main/java/org/apache/commons/compress2/archivers/Archivers.java @@ -33,6 +33,7 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.StreamSupport; +import org.apache.commons.compress2.Format; /** * Loads ArchiveFormats defined as "services" from {@code @@ -122,7 +123,7 @@ public class Archivers implements Iterable<ArchiveFormat<? extends ArchiveEntry> private void fillMap() throws ServiceConfigurationError { Set<ArchiveFormat<? extends ArchiveEntry>> ts = - new TreeSet<ArchiveFormat<? extends ArchiveEntry>>(Archivers::sortForAutoDetection); + new TreeSet<ArchiveFormat<? extends ArchiveEntry>>(Format.AUTO_DETECTION_ORDER); ts.addAll(asList(formatLoader)); archivers = Collections.unmodifiableMap(ts.stream() .collect(Collectors.toMap(ArchiveFormat::getName, Function.identity()))); @@ -144,17 +145,4 @@ public class Archivers implements Iterable<ArchiveFormat<? extends ArchiveEntry> } return l; } - - private static int sortForAutoDetection(ArchiveFormat a1, ArchiveFormat a2) { - if (a1.supportsAutoDetection() && a2.supportsAutoDetection()) { - return a1.getNumberOfBytesRequiredForAutodetection() - a2.getNumberOfBytesRequiredForAutodetection(); - } - if (!a1.supportsAutoDetection() && !a2.supportsAutoDetection()) { - return 0; - } - if (a1.supportsAutoDetection()) { - return -1; - } - return 1; - } }