View Javadoc
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  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  
23  import org.tukaani.xz.DeltaOptions;
24  import org.tukaani.xz.FinishableWrapperOutputStream;
25  import org.tukaani.xz.UnsupportedOptionsException;
26  
27  final class DeltaDecoder extends AbstractCoder {
28      DeltaDecoder() {
29          super(Number.class);
30      }
31  
32      @Override
33      InputStream decode(final String archiveName, final InputStream in, final long uncompressedLength, final Coder coder, final byte[] password,
34              final int maxMemoryLimitInKb) throws IOException {
35          return new DeltaOptions(getOptionsFromCoder(coder)).getInputStream(in);
36      }
37  
38      @SuppressWarnings("resource")
39      @Override
40      OutputStream encode(final OutputStream out, final Object options) throws IOException {
41          final int distance = toInt(options, 1);
42          try {
43              return new DeltaOptions(distance).getOutputStream(new FinishableWrapperOutputStream(out));
44          } catch (final UnsupportedOptionsException ex) { // NOSONAR
45              throw new IOException(ex.getMessage());
46          }
47      }
48  
49      @Override
50      byte[] getOptionsAsProperties(final Object options) {
51          return new byte[] { (byte) (toInt(options, 1) - 1) };
52      }
53  
54      private int getOptionsFromCoder(final Coder coder) {
55          if (coder.properties == null || coder.properties.length == 0) {
56              return 1;
57          }
58          return (0xff & coder.properties[0]) + 1;
59      }
60  
61      @Override
62      Object getOptionsFromCoder(final Coder coder, final InputStream in) {
63          return getOptionsFromCoder(coder);
64      }
65  }