Coders.java

  1. /*
  2.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.commons.compress.archivers.sevenz;

  18. import java.io.ByteArrayInputStream;
  19. import java.io.FilterInputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.io.SequenceInputStream;
  24. import java.util.Arrays;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import java.util.zip.Deflater;
  28. import java.util.zip.DeflaterOutputStream;
  29. import java.util.zip.Inflater;
  30. import java.util.zip.InflaterInputStream;

  31. import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
  32. import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
  33. import org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream;
  34. import org.apache.commons.compress.utils.FlushShieldFilterOutputStream;
  35. import org.tukaani.xz.ARMOptions;
  36. import org.tukaani.xz.ARMThumbOptions;
  37. import org.tukaani.xz.FilterOptions;
  38. import org.tukaani.xz.FinishableWrapperOutputStream;
  39. import org.tukaani.xz.IA64Options;
  40. import org.tukaani.xz.PowerPCOptions;
  41. import org.tukaani.xz.SPARCOptions;
  42. import org.tukaani.xz.X86Options;

  43. final class Coders {
  44.     static class BCJDecoder extends AbstractCoder {
  45.         private final FilterOptions opts;

  46.         BCJDecoder(final FilterOptions opts) {
  47.             this.opts = opts;
  48.         }

  49.         @Override
  50.         InputStream decode(final String archiveName, final InputStream in, final long uncompressedLength, final Coder coder, final byte[] password,
  51.                 final int maxMemoryLimitInKb) throws IOException {
  52.             try {
  53.                 return opts.getInputStream(in);
  54.             } catch (final AssertionError e) {
  55.                 throw new IOException("BCJ filter used in " + archiveName + " needs XZ for Java > 1.4 - see "
  56.                         + "https://commons.apache.org/proper/commons-compress/limitations.html#7Z", e);
  57.             }
  58.         }

  59.         @SuppressWarnings("resource")
  60.         @Override
  61.         OutputStream encode(final OutputStream out, final Object options) {
  62.             return new FlushShieldFilterOutputStream(opts.getOutputStream(new FinishableWrapperOutputStream(out)));
  63.         }
  64.     }

  65.     static class BZIP2Decoder extends AbstractCoder {
  66.         BZIP2Decoder() {
  67.             super(Number.class);
  68.         }

  69.         @Override
  70.         InputStream decode(final String archiveName, final InputStream in, final long uncompressedLength, final Coder coder, final byte[] password,
  71.                 final int maxMemoryLimitInKb) throws IOException {
  72.             return new BZip2CompressorInputStream(in);
  73.         }

  74.         @Override
  75.         OutputStream encode(final OutputStream out, final Object options) throws IOException {
  76.             final int blockSize = toInt(options, BZip2CompressorOutputStream.MAX_BLOCKSIZE);
  77.             return new BZip2CompressorOutputStream(out, blockSize);
  78.         }
  79.     }

  80.     static class CopyDecoder extends AbstractCoder {
  81.         @Override
  82.         InputStream decode(final String archiveName, final InputStream in, final long uncompressedLength, final Coder coder, final byte[] password,
  83.                 final int maxMemoryLimitInKb) throws IOException {
  84.             return in;
  85.         }

  86.         @Override
  87.         OutputStream encode(final OutputStream out, final Object options) {
  88.             return out;
  89.         }
  90.     }

  91.     static class Deflate64Decoder extends AbstractCoder {
  92.         Deflate64Decoder() {
  93.             super(Number.class);
  94.         }

  95.         @Override
  96.         InputStream decode(final String archiveName, final InputStream in, final long uncompressedLength, final Coder coder, final byte[] password,
  97.                 final int maxMemoryLimitInKb) throws IOException {
  98.             return new Deflate64CompressorInputStream(in);
  99.         }
  100.     }

  101.     static class DeflateDecoder extends AbstractCoder {
  102.         static class DeflateDecoderInputStream extends FilterInputStream {

  103.             Inflater inflater;

  104.             DeflateDecoderInputStream(final InflaterInputStream inflaterInputStream, final Inflater inflater) {
  105.                 super(inflaterInputStream);
  106.                 this.inflater = inflater;
  107.             }

  108.             @Override
  109.             public void close() throws IOException {
  110.                 try {
  111.                     super.close();
  112.                 } finally {
  113.                     inflater.end();
  114.                 }
  115.             }

  116.         }

  117.         static class DeflateDecoderOutputStream extends OutputStream {

  118.             final DeflaterOutputStream deflaterOutputStream;
  119.             Deflater deflater;

  120.             DeflateDecoderOutputStream(final DeflaterOutputStream deflaterOutputStream, final Deflater deflater) {
  121.                 this.deflaterOutputStream = deflaterOutputStream;
  122.                 this.deflater = deflater;
  123.             }

  124.             @Override
  125.             public void close() throws IOException {
  126.                 try {
  127.                     deflaterOutputStream.close();
  128.                 } finally {
  129.                     deflater.end();
  130.                 }
  131.             }

  132.             @Override
  133.             public void write(final byte[] b) throws IOException {
  134.                 deflaterOutputStream.write(b);
  135.             }

  136.             @Override
  137.             public void write(final byte[] b, final int off, final int len) throws IOException {
  138.                 deflaterOutputStream.write(b, off, len);
  139.             }

  140.             @Override
  141.             public void write(final int b) throws IOException {
  142.                 deflaterOutputStream.write(b);
  143.             }
  144.         }

  145.         private static final byte[] ONE_ZERO_BYTE = new byte[1];

  146.         DeflateDecoder() {
  147.             super(Number.class);
  148.         }

  149.         @Override
  150.         InputStream decode(final String archiveName, final InputStream in, final long uncompressedLength, final Coder coder, final byte[] password,
  151.                 final int maxMemoryLimitInKb) throws IOException {
  152.             final Inflater inflater = new Inflater(true);
  153.             // Inflater with nowrap=true has this odd contract for a zero padding
  154.             // byte following the data stream; this used to be zlib's requirement
  155.             // and has been fixed a long time ago, but the contract persists so
  156.             // we comply.
  157.             // https://docs.oracle.com/javase/8/docs/api/java/util/zip/Inflater.html#Inflater(boolean)
  158.             final InflaterInputStream inflaterInputStream = new InflaterInputStream(new SequenceInputStream(in, new ByteArrayInputStream(ONE_ZERO_BYTE)),
  159.                     inflater);
  160.             return new DeflateDecoderInputStream(inflaterInputStream, inflater);
  161.         }

  162.         @Override
  163.         OutputStream encode(final OutputStream out, final Object options) {
  164.             final int level = toInt(options, 9);
  165.             final Deflater deflater = new Deflater(level, true);
  166.             final DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(out, deflater);
  167.             return new DeflateDecoderOutputStream(deflaterOutputStream, deflater);
  168.         }
  169.     }

  170.     private static final Map<SevenZMethod, AbstractCoder> CODER_MAP = new HashMap<SevenZMethod, AbstractCoder>() {

  171.         private static final long serialVersionUID = 1664829131806520867L;

  172.         {
  173.             put(SevenZMethod.COPY, new CopyDecoder());
  174.             put(SevenZMethod.LZMA, new LZMADecoder());
  175.             put(SevenZMethod.LZMA2, new LZMA2Decoder());
  176.             put(SevenZMethod.DEFLATE, new DeflateDecoder());
  177.             put(SevenZMethod.DEFLATE64, new Deflate64Decoder());
  178.             put(SevenZMethod.BZIP2, new BZIP2Decoder());
  179.             put(SevenZMethod.AES256SHA256, new AES256SHA256Decoder());
  180.             put(SevenZMethod.BCJ_X86_FILTER, new BCJDecoder(new X86Options()));
  181.             put(SevenZMethod.BCJ_PPC_FILTER, new BCJDecoder(new PowerPCOptions()));
  182.             put(SevenZMethod.BCJ_IA64_FILTER, new BCJDecoder(new IA64Options()));
  183.             put(SevenZMethod.BCJ_ARM_FILTER, new BCJDecoder(new ARMOptions()));
  184.             put(SevenZMethod.BCJ_ARM_THUMB_FILTER, new BCJDecoder(new ARMThumbOptions()));
  185.             put(SevenZMethod.BCJ_SPARC_FILTER, new BCJDecoder(new SPARCOptions()));
  186.             put(SevenZMethod.DELTA_FILTER, new DeltaDecoder());
  187.         }
  188.     };

  189.     static InputStream addDecoder(final String archiveName, final InputStream is, final long uncompressedLength, final Coder coder, final byte[] password,
  190.             final int maxMemoryLimitInKb) throws IOException {
  191.         final AbstractCoder cb = findByMethod(SevenZMethod.byId(coder.decompressionMethodId));
  192.         if (cb == null) {
  193.             throw new IOException("Unsupported compression method " + Arrays.toString(coder.decompressionMethodId) + " used in " + archiveName);
  194.         }
  195.         return cb.decode(archiveName, is, uncompressedLength, coder, password, maxMemoryLimitInKb);
  196.     }

  197.     static OutputStream addEncoder(final OutputStream out, final SevenZMethod method, final Object options) throws IOException {
  198.         final AbstractCoder cb = findByMethod(method);
  199.         if (cb == null) {
  200.             throw new IOException("Unsupported compression method " + method);
  201.         }
  202.         return cb.encode(out, options);
  203.     }

  204.     static AbstractCoder findByMethod(final SevenZMethod method) {
  205.         return CODER_MAP.get(method);
  206.     }

  207. }