Skip to content

Commit

Permalink
Better internal names and streams
Browse files Browse the repository at this point in the history
  • Loading branch information
garydgregory committed Dec 10, 2022
1 parent 3d70f41 commit 346e8a4
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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");
}

/**
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import org.tukaani.xz.X86Options;

class Coders {
private static final Map<SevenZMethod, CoderBase> CODER_MAP = new HashMap<SevenZMethod, CoderBase>() {
private static final Map<SevenZMethod, AbstractCoder> CODER_MAP = new HashMap<SevenZMethod, AbstractCoder>() {

private static final long serialVersionUID = 1664829131806520867L;

Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
Expand All @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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
Expand All @@ -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)
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 346e8a4

Please sign in to comment.