View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.commons.compress.archivers.sevenz;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  
25  import org.apache.commons.lang3.ArrayUtils;
26  import org.tukaani.xz.DeltaOptions;
27  import org.tukaani.xz.FinishableWrapperOutputStream;
28  import org.tukaani.xz.UnsupportedOptionsException;
29  
30  final class DeltaDecoder extends AbstractCoder {
31      DeltaDecoder() {
32          super(Number.class);
33      }
34  
35      @Override
36      InputStream decode(final String archiveName, final InputStream in, final long uncompressedLength, final Coder coder, final byte[] password,
37              final int maxMemoryLimitKiB) throws IOException {
38          return new DeltaOptions(getOptionsFromCoder(coder)).getInputStream(in);
39      }
40  
41      @SuppressWarnings("resource")
42      @Override
43      OutputStream encode(final OutputStream out, final Object options) throws IOException {
44          final int distance = toInt(options, 1);
45          try {
46              return new DeltaOptions(distance).getOutputStream(new FinishableWrapperOutputStream(out));
47          } catch (final UnsupportedOptionsException ex) { // NOSONAR
48              throw new IOException(ex.getMessage());
49          }
50      }
51  
52      @Override
53      byte[] getOptionsAsProperties(final Object options) {
54          return new byte[] { (byte) (toInt(options, 1) - 1) };
55      }
56  
57      private int getOptionsFromCoder(final Coder coder) {
58          if (coder == null || ArrayUtils.isEmpty(coder.properties)) {
59              return 1;
60          }
61          return (0xff & coder.properties[0]) + 1;
62      }
63  
64      @Override
65      Object getOptionsFromCoder(final Coder coder, final InputStream in) {
66          return getOptionsFromCoder(coder);
67      }
68  }