1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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) {
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 }