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