Debian Codegraph @

src/org/tukaani/xz/LZMA2OutputStream.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 // SPDX-FileContributor: Igor Pavlov <https://7-zip.org/>
5
6 package org.tukaani.xz;
7
8 import java.io.IOException;
9 import org.tukaani.xz.lz.LZEncoder;
10 import org.tukaani.xz.rangecoder.RangeEncoderToBuffer;
11 import org.tukaani.xz.lzma.LZMAEncoder;
12
13 class LZMA2OutputStream extends FinishableOutputStream {
14 static final int COMPRESSED_SIZE_MAX = 64 << 10;
15
16 private final ArrayCache arrayCache;
17
18 private FinishableOutputStream out;
19
20 private LZEncoder lz;
21 private RangeEncoderToBuffer rc;
22 private LZMAEncoder lzma;
23
24 private final int props; // Cannot change props on the fly for now.
25 private boolean dictResetNeeded = true;
26 private boolean stateResetNeeded = true;
27 private boolean propsNeeded = true;
28
29 private int pendingSize = 0;
30 private boolean finished = false;
31 private IOException exception = null;
32
33 private final byte[] chunkHeader = new byte[6];
34
35 private final byte[] tempBuf = new byte[1];
36
37 private static int getExtraSizeBefore(int dictSize) {
38 return COMPRESSED_SIZE_MAX > dictSize
39 ? COMPRESSED_SIZE_MAX - dictSize : 0;
40 }
41
42 static int getMemoryUsage(LZMA2Options options) {
43 // 64 KiB buffer for the range encoder + a little extra + LZMAEncoder
44 int dictSize = options.getDictSize();
45 int extraSizeBefore = getExtraSizeBefore(dictSize);
46 return 70 + LZMAEncoder.getMemoryUsage(options.getMode(),
47 dictSize, extraSizeBefore,
48 options.getMatchFinder());
49 }
50
51 LZMA2OutputStream(FinishableOutputStream out, LZMA2Options options,
52 ArrayCache arrayCache) {
53 if (out == null)
54 throw new NullPointerException();
55
56 this.arrayCache = arrayCache;
57 this.out = out;
58 rc = new RangeEncoderToBuffer(COMPRESSED_SIZE_MAX, arrayCache);
59
60 int dictSize = options.getDictSize();
61 int extraSizeBefore = getExtraSizeBefore(dictSize);
62 lzma = LZMAEncoder.getInstance(rc,
63 options.getLc(), options.getLp(), options.getPb(),
64 options.getMode(),
65 dictSize, extraSizeBefore, options.getNiceLen(),
66 options.getMatchFinder(), options.getDepthLimit(),
67 this.arrayCache);
68
69 lz = lzma.getLZEncoder();
70
71 byte[] presetDict = options.getPresetDict();
72 if (presetDict != null && presetDict.length > 0) {
73 lz.setPresetDict(dictSize, presetDict);
74 dictResetNeeded = false;
75 }
76
77 props = (options.getPb() * 5 + options.getLp()) * 9 + options.getLc();
78 }
79
80 @Override
81 public void write(int b) throws IOException {
82 tempBuf[0] = (byte)b;
83 write(tempBuf, 0, 1);
84 }
85
86 @Override
87 public void write(byte[] buf, int off, int len) throws IOException {
88 if (off < 0 || len < 0 || off + len < 0 || off + len > buf.length)
89 throw new IndexOutOfBoundsException();
90
91 if (exception != null)
92 throw exception;
93
94 if (finished)
95 throw new XZIOException("Stream finished or closed");
96
97 try {
98 while (len > 0) {
99 int used = lz.fillWindow(buf, off, len);
100 off += used;
101 len -= used;
102 pendingSize += used;
103
104 if (lzma.encodeForLZMA2())
105 writeChunk();
106 }
107 } catch (IOException e) {
108 exception = e;
109 throw e;
110 }
111 }
112
113 private void writeChunk() throws IOException {
114 int compressedSize = rc.finish();
115 int uncompressedSize = lzma.getUncompressedSize();
116
117 assert compressedSize > 0 : compressedSize;
118 assert uncompressedSize > 0 : uncompressedSize;
119
120 // +2 because the header of a compressed chunk is 2 bytes
121 // bigger than the header of an uncompressed chunk.
122 if (compressedSize + 2 < uncompressedSize) {
123 writeLZMA(uncompressedSize, compressedSize);
124 } else {
125 lzma.reset();
126 uncompressedSize = lzma.getUncompressedSize();
127 assert uncompressedSize > 0 : uncompressedSize;
128 writeUncompressed(uncompressedSize);
129 }
130
131 pendingSize -= uncompressedSize;
132 lzma.resetUncompressedSize();
133 rc.reset();
134 }
135
136 private void writeLZMA(int uncompressedSize, int compressedSize)
137 throws IOException {
138 int control;
139
140 if (propsNeeded) {
141 if (dictResetNeeded)
142 control = 0x80 + (3 << 5);
143 else
144 control = 0x80 + (2 << 5);
145 } else {
146 if (stateResetNeeded)
147 control = 0x80 + (1 << 5);
148 else
149 control = 0x80;
150 }
151
152 control |= (uncompressedSize - 1) >>> 16;
153 chunkHeader[0] = (byte)control;
154 chunkHeader[1] = (byte)((uncompressedSize - 1) >>> 8);
155 chunkHeader[2] = (byte)(uncompressedSize - 1);
156 chunkHeader[3] = (byte)((compressedSize - 1) >>> 8);
157 chunkHeader[4] = (byte)(compressedSize - 1);
158
159 if (propsNeeded) {
160 chunkHeader[5] = (byte)props;
161 out.write(chunkHeader, 0, 6);
162 } else {
163 out.write(chunkHeader, 0, 5);
164 }
165
166 rc.write(out);
167
168 propsNeeded = false;
169 stateResetNeeded = false;
170 dictResetNeeded = false;
171 }
172
173 private void writeUncompressed(int uncompressedSize) throws IOException {
174 while (uncompressedSize > 0) {
175 int chunkSize = Math.min(uncompressedSize, COMPRESSED_SIZE_MAX);
176 chunkHeader[0] = (byte)(dictResetNeeded ? 0x01 : 0x02);
177 chunkHeader[1] = (byte)((chunkSize - 1) >>> 8);
178 chunkHeader[2] = (byte)(chunkSize - 1);
179 out.write(chunkHeader, 0, 3);
180 lz.copyUncompressed(out, uncompressedSize, chunkSize);
181 uncompressedSize -= chunkSize;
182 dictResetNeeded = false;
183 }
184
185 stateResetNeeded = true;
186 }
187
188 private void writeEndMarker() throws IOException {
189 assert !finished;
190
191 if (exception != null)
192 throw exception;
193
194 lz.setFinishing();
195
196 try {
197 while (pendingSize > 0) {
198 lzma.encodeForLZMA2();
199 writeChunk();
200 }
201
202 out.write(0x00);
203 } catch (IOException e) {
204 exception = e;
205 throw e;
206 }
207
208 finished = true;
209
210 lzma.putArraysToCache(arrayCache);
211 lzma = null;
212 lz = null;
213 rc.putArraysToCache(arrayCache);
214 rc = null;
215 }
216
217 @Override
218 public void flush() throws IOException {
219 if (exception != null)
220 throw exception;
221
222 if (finished)
223 throw new XZIOException("Stream finished or closed");
224
225 try {
226 lz.setFlushing();
227
228 while (pendingSize > 0) {
229 lzma.encodeForLZMA2();
230 writeChunk();
231 }
232
233 out.flush();
234 } catch (IOException e) {
235 exception = e;
236 throw e;
237 }
238 }
239
240 @Override
241 public void finish() throws IOException {
242 if (!finished) {
243 writeEndMarker();
244
245 try {
246 out.finish();
247 } catch (IOException e) {
248 exception = e;
249 throw e;
250 }
251 }
252 }
253
254 @Override
255 public void close() throws IOException {
256 if (out != null) {
257 if (!finished) {
258 try {
259 writeEndMarker();
260 } catch (IOException e) {}
261 }
262
263 try {
264 out.close();
265 } catch (IOException e) {
266 if (exception == null)
267 exception = e;
268 }
269
270 out = null;
271 }
272
273 if (exception != null)
274 throw exception;
275 }
276 }