diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java index dde14ee23b2..8fb5a778715 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java @@ -31,7 +31,8 @@ import org.apache.commons.compress.PasswordRequiredException; -class AES256SHA256Decoder extends CoderBase { +class AES256SHA256Decoder extends AbstractCoder { + @Override InputStream decode(final String archiveName, final InputStream in, final long uncompressedLength, final Coder coder, final byte[] passwordBytes, final int maxMemoryLimitInKb) { diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/AbstractCoder.java similarity index 84% rename from src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java rename to src/main/java/org/apache/commons/compress/archivers/sevenz/AbstractCoder.java index ab05f6b3183..c8690de282b 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/CoderBase.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/AbstractCoder.java @@ -20,13 +20,15 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.util.Objects; +import java.util.stream.Stream; import org.apache.commons.compress.utils.ByteUtils; /** * Abstracts a base Codec class. */ -abstract class CoderBase { +abstract class AbstractCoder { /** * If the option represents a number, return its integer value, otherwise return the given default value. @@ -35,33 +37,19 @@ abstract class CoderBase { * @param defaultValue A default value if options is not a number. * @return The given number or default value. */ - protected static int numberOptionOrDefault(final Object options, final int defaultValue) { + protected static int toInt(final Object options, final int defaultValue) { return options instanceof Number ? ((Number) options).intValue() : defaultValue; } - private final Class[] acceptableOptions; + private final Class[] optionClasses; /** * Constructs a new instance. * - * @param acceptableOptions types that can be used as options for this codec. + * @param optionClasses types that can be used as options for this codec. */ - protected CoderBase(final Class... acceptableOptions) { - this.acceptableOptions = acceptableOptions; - } - - /** - * Tests whether this method can extract options from the given object. - * - * @return whether this method can extract options from the given object. - */ - boolean canAcceptOptions(final Object opts) { - for (final Class c : acceptableOptions) { - if (c.isInstance(opts)) { - return true; - } - } - return false; + protected AbstractCoder(final Class... optionClasses) { + this.optionClasses = Objects.requireNonNull(optionClasses, "optionClasses"); } /** @@ -101,4 +89,13 @@ byte[] getOptionsAsProperties(final Object options) throws IOException { Object getOptionsFromCoder(final Coder coder, final InputStream in) throws IOException { return null; } + + /** + * Tests whether this method can extract options from the given object. + * + * @return whether this method can extract options from the given object. + */ + boolean isOptionInstance(final Object opts) { + return Stream.of(optionClasses).anyMatch(c -> c.isInstance(opts)); + } } diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java index be6a6b0ed1e..4dbeeae9c24 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java @@ -44,7 +44,7 @@ import org.tukaani.xz.X86Options; class Coders { - private static final Map CODER_MAP = new HashMap() { + private static final Map CODER_MAP = new HashMap() { private static final long serialVersionUID = 1664829131806520867L; @@ -66,13 +66,13 @@ class Coders { } }; - static CoderBase findByMethod(final SevenZMethod method) { + static AbstractCoder findByMethod(final SevenZMethod method) { return CODER_MAP.get(method); } static InputStream addDecoder(final String archiveName, final InputStream is, final long uncompressedLength, final Coder coder, final byte[] password, final int maxMemoryLimitInKb) throws IOException { - final CoderBase cb = findByMethod(SevenZMethod.byId(coder.decompressionMethodId)); + final AbstractCoder cb = findByMethod(SevenZMethod.byId(coder.decompressionMethodId)); if (cb == null) { throw new IOException("Unsupported compression method " + Arrays.toString(coder.decompressionMethodId) @@ -83,14 +83,14 @@ static InputStream addDecoder(final String archiveName, final InputStream is, fi static OutputStream addEncoder(final OutputStream out, final SevenZMethod method, final Object options) throws IOException { - final CoderBase cb = findByMethod(method); + final AbstractCoder cb = findByMethod(method); if (cb == null) { throw new IOException("Unsupported compression method " + method); } return cb.encode(out, options); } - static class CopyDecoder extends CoderBase { + static class CopyDecoder extends AbstractCoder { @Override InputStream decode(final String archiveName, final InputStream in, final long uncompressedLength, final Coder coder, final byte[] password, final int maxMemoryLimitInKb) throws IOException { @@ -102,7 +102,7 @@ OutputStream encode(final OutputStream out, final Object options) { } } - static class BCJDecoder extends CoderBase { + static class BCJDecoder extends AbstractCoder { private final FilterOptions opts; BCJDecoder(final FilterOptions opts) { this.opts = opts; @@ -128,13 +128,12 @@ OutputStream encode(final OutputStream out, final Object options) { } } - static class DeflateDecoder extends CoderBase { + static class DeflateDecoder extends AbstractCoder { private static final byte[] ONE_ZERO_BYTE = new byte[1]; DeflateDecoder() { super(Number.class); } - @SuppressWarnings("resource") // caller must close the InputStream @Override InputStream decode(final String archiveName, final InputStream in, final long uncompressedLength, final Coder coder, final byte[] password, final int maxMemoryLimitInKb) @@ -152,7 +151,7 @@ InputStream decode(final String archiveName, final InputStream in, final long un @Override OutputStream encode(final OutputStream out, final Object options) { - final int level = numberOptionOrDefault(options, 9); + final int level = toInt(options, 9); final Deflater deflater = new Deflater(level, true); final DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out, deflater); return new DeflateDecoderOutputStream(deflaterOutputStream, deflater); @@ -231,12 +230,11 @@ public void close() throws IOException { } } - static class Deflate64Decoder extends CoderBase { + static class Deflate64Decoder extends AbstractCoder { Deflate64Decoder() { super(Number.class); } - @SuppressWarnings("resource") // caller must close the InputStream @Override InputStream decode(final String archiveName, final InputStream in, final long uncompressedLength, final Coder coder, final byte[] password, final int maxMemoryLimitInKb) @@ -245,7 +243,7 @@ InputStream decode(final String archiveName, final InputStream in, final long un } } - static class BZIP2Decoder extends CoderBase { + static class BZIP2Decoder extends AbstractCoder { BZIP2Decoder() { super(Number.class); } @@ -259,7 +257,7 @@ InputStream decode(final String archiveName, final InputStream in, final long un @Override OutputStream encode(final OutputStream out, final Object options) throws IOException { - final int blockSize = numberOptionOrDefault(options, BZip2CompressorOutputStream.MAX_BLOCKSIZE); + final int blockSize = toInt(options, BZip2CompressorOutputStream.MAX_BLOCKSIZE); return new BZip2CompressorOutputStream(out, blockSize); } } diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/DeltaDecoder.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/DeltaDecoder.java index c2747f8ea01..826a693912a 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/DeltaDecoder.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/DeltaDecoder.java @@ -25,7 +25,7 @@ import org.tukaani.xz.FinishableWrapperOutputStream; import org.tukaani.xz.UnsupportedOptionsException; -class DeltaDecoder extends CoderBase { +class DeltaDecoder extends AbstractCoder { DeltaDecoder() { super(Number.class); } @@ -39,7 +39,7 @@ InputStream decode(final String archiveName, final InputStream in, final long un @SuppressWarnings("resource") @Override OutputStream encode(final OutputStream out, final Object options) throws IOException { - final int distance = numberOptionOrDefault(options, 1); + final int distance = toInt(options, 1); try { return new DeltaOptions(distance).getOutputStream(new FinishableWrapperOutputStream(out)); } catch (final UnsupportedOptionsException ex) { // NOSONAR @@ -50,7 +50,7 @@ OutputStream encode(final OutputStream out, final Object options) throws IOExcep @Override byte[] getOptionsAsProperties(final Object options) { return new byte[] { - (byte) (numberOptionOrDefault(options, 1) - 1) + (byte) (toInt(options, 1) - 1) }; } diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java index 13e498f7462..6c0424e740e 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMA2Decoder.java @@ -27,7 +27,7 @@ import org.tukaani.xz.LZMA2InputStream; import org.tukaani.xz.LZMA2Options; -class LZMA2Decoder extends CoderBase { +class LZMA2Decoder extends AbstractCoder { LZMA2Decoder() { super(LZMA2Options.class, Number.class); } @@ -108,6 +108,6 @@ private LZMA2Options getOptions(final Object opts) throws IOException { } private int numberOptionOrDefault(final Object opts) { - return numberOptionOrDefault(opts, LZMA2Options.DICT_SIZE_DEFAULT); + return toInt(opts, LZMA2Options.DICT_SIZE_DEFAULT); } } diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMADecoder.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMADecoder.java index 68a283688da..7fc92af769f 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMADecoder.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/LZMADecoder.java @@ -28,7 +28,7 @@ import org.tukaani.xz.LZMAInputStream; import org.tukaani.xz.LZMAOutputStream; -class LZMADecoder extends CoderBase { +class LZMADecoder extends AbstractCoder { LZMADecoder() { super(LZMA2Options.class, Number.class); } @@ -110,6 +110,6 @@ private LZMA2Options getOptions(final Object opts) throws IOException { } private int numberOptionOrDefault(final Object opts) { - return numberOptionOrDefault(opts, LZMA2Options.DICT_SIZE_DEFAULT); + return toInt(opts, LZMA2Options.DICT_SIZE_DEFAULT); } } diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfiguration.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfiguration.java index a93bf7d571a..4cd83a9503e 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfiguration.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZMethodConfiguration.java @@ -59,7 +59,7 @@ public SevenZMethodConfiguration(final SevenZMethod method) { public SevenZMethodConfiguration(final SevenZMethod method, final Object options) { this.method = method; this.options = options; - if (options != null && !Coders.findByMethod(method).canAcceptOptions(options)) { + if (options != null && !Coders.findByMethod(method).isOptionInstance(options)) { throw new IllegalArgumentException("The " + method + " method doesn't support options of type " + options.getClass()); }