Debian Codegraph @

src/org/tukaani/xz/DeltaOptions.java

raw symbols SCIP
1 // SPDX-License-Identifier: 0BSD
2 // SPDX-FileCopyrightText: The XZ for Java authors and contributors
3 // SPDX-FileContributor: Lasse Collin <[email protected]>
4
5 package org.tukaani.xz;
6
7 import java.io.InputStream;
8
9 /**
10 * Delta filter options. The Delta filter can be used only as a non-last
11 * filter in the chain, for example Delta + LZMA2.
12 * <p>
13 * Currently only simple byte-wise delta is supported. The only option
14 * is the delta distance, which you should set to match your data.
15 * It's not possible to provide a generic default value for it.
16 * <p>
17 * For example, with distance = 2 and eight-byte input
18 * A1 B1 A2 B3 A3 B5 A4 B7, the output will be A1 B1 01 02 01 02 01 02.
19 * <p>
20 * The Delta filter can be good with uncompressed bitmap images. It can
21 * also help with PCM audio, although special-purpose compressors like
22 * FLAC will give much smaller result at much better compression speed.
23 */
24 public final class DeltaOptions extends FilterOptions {
25 /**
26 * Smallest supported delta calculation distance.
27 */
28 public static final int DISTANCE_MIN = 1;
29
30 /**
31 * Largest supported delta calculation distance.
32 */
33 public static final int DISTANCE_MAX = 256;
34
35 private int distance = DISTANCE_MIN;
36
37 /**
38 * Creates new Delta options and sets the delta distance to 1 byte.
39 */
40 public DeltaOptions() {}
41
42 /**
43 * Creates new Delta options and sets the distance to the given value.
44 */
45 public DeltaOptions(int distance) throws UnsupportedOptionsException {
46 setDistance(distance);
47 }
48
49 /**
50 * Sets the delta distance in bytes. The new distance must be in
51 * the range [DISTANCE_MIN, DISTANCE_MAX].
52 */
53 public void setDistance(int distance) throws UnsupportedOptionsException {
54 if (distance < DISTANCE_MIN || distance > DISTANCE_MAX)
55 throw new UnsupportedOptionsException(
56 "Delta distance must be in the range [" + DISTANCE_MIN
57 + ", " + DISTANCE_MAX + "]: " + distance);
58
59 this.distance = distance;
60 }
61
62 /**
63 * Gets the delta distance.
64 */
65 public int getDistance() {
66 return distance;
67 }
68
69 @Override
70 public int getEncoderMemoryUsage() {
71 return DeltaOutputStream.getMemoryUsage();
72 }
73
74 @Override
75 public FinishableOutputStream getOutputStream(FinishableOutputStream out,
76 ArrayCache arrayCache) {
77 return new DeltaOutputStream(out, this);
78 }
79
80 @Override
81 public int getDecoderMemoryUsage() {
82 return 1;
83 }
84
85 @Override
86 public InputStream getInputStream(InputStream in, ArrayCache arrayCache) {
87 return new DeltaInputStream(in, distance);
88 }
89
90 @Override
91 FilterEncoder getFilterEncoder() {
92 return new DeltaEncoder(this);
93 }
94
95 @Override
96 public Object clone() {
97 try {
98 return super.clone();
99 } catch (CloneNotSupportedException e) {
100 assert false;
101 throw new RuntimeException();
102 }
103 }
104 }