DeltaDecoder.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.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;

  21. import org.tukaani.xz.DeltaOptions;
  22. import org.tukaani.xz.FinishableWrapperOutputStream;
  23. import org.tukaani.xz.UnsupportedOptionsException;

  24. final class DeltaDecoder extends AbstractCoder {
  25.     DeltaDecoder() {
  26.         super(Number.class);
  27.     }

  28.     @Override
  29.     InputStream decode(final String archiveName, final InputStream in, final long uncompressedLength, final Coder coder, final byte[] password,
  30.             final int maxMemoryLimitInKb) throws IOException {
  31.         return new DeltaOptions(getOptionsFromCoder(coder)).getInputStream(in);
  32.     }

  33.     @SuppressWarnings("resource")
  34.     @Override
  35.     OutputStream encode(final OutputStream out, final Object options) throws IOException {
  36.         final int distance = toInt(options, 1);
  37.         try {
  38.             return new DeltaOptions(distance).getOutputStream(new FinishableWrapperOutputStream(out));
  39.         } catch (final UnsupportedOptionsException ex) { // NOSONAR
  40.             throw new IOException(ex.getMessage());
  41.         }
  42.     }

  43.     @Override
  44.     byte[] getOptionsAsProperties(final Object options) {
  45.         return new byte[] { (byte) (toInt(options, 1) - 1) };
  46.     }

  47.     private int getOptionsFromCoder(final Coder coder) {
  48.         if (coder.properties == null || coder.properties.length == 0) {
  49.             return 1;
  50.         }
  51.         return (0xff & coder.properties[0]) + 1;
  52.     }

  53.     @Override
  54.     Object getOptionsFromCoder(final Coder coder, final InputStream in) {
  55.         return getOptionsFromCoder(coder);
  56.     }
  57. }