Debian Codegraph @

src/org/tukaani/xz/LZMAOutputStream.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.OutputStream;
9 import java.io.IOException;
10 import org.tukaani.xz.lz.LZEncoder;
11 import org.tukaani.xz.rangecoder.RangeEncoderToStream;
12 import org.tukaani.xz.lzma.LZMAEncoder;
13
14 /**
15 * Compresses into the legacy .lzma file format or into a raw LZMA stream.
16 *
17 * @since 1.6
18 */
19 public class LZMAOutputStream extends FinishableOutputStream {
20 private OutputStream out;
21
22 private final ArrayCache arrayCache;
23
24 private LZEncoder lz;
25 private final RangeEncoderToStream rc;
26 private LZMAEncoder lzma;
27
28 private final int props;
29 private final boolean useEndMarker;
30 private final long expectedUncompressedSize;
31 private long currentUncompressedSize = 0;
32
33 private boolean finished = false;
34 private IOException exception = null;
35
36 private final byte[] tempBuf = new byte[1];
37
38 private LZMAOutputStream(OutputStream out, LZMA2Options options,
39 boolean useHeader, boolean useEndMarker,
40 long expectedUncompressedSize,
41 ArrayCache arrayCache)
42 throws IOException {
43 if (out == null)
44 throw new NullPointerException();
45
46 // -1 indicates unknown and >= 0 are for known sizes.
47 if (expectedUncompressedSize < -1)
48 throw new IllegalArgumentException(
49 "Invalid expected input size (less than -1)");
50
51 this.useEndMarker = useEndMarker;
52 this.expectedUncompressedSize = expectedUncompressedSize;
53
54 this.arrayCache = arrayCache;
55
56 this.out = out;
57 rc = new RangeEncoderToStream(out);
58
59 int dictSize = options.getDictSize();
60 lzma = LZMAEncoder.getInstance(rc,
61 options.getLc(), options.getLp(), options.getPb(),
62 options.getMode(),
63 dictSize, 0, options.getNiceLen(),
64 options.getMatchFinder(), options.getDepthLimit(),
65 arrayCache);
66
67 lz = lzma.getLZEncoder();
68
69 byte[] presetDict = options.getPresetDict();
70 if (presetDict != null && presetDict.length > 0) {
71 if (useHeader)
72 throw new UnsupportedOptionsException(
73 "Preset dictionary cannot be used in .lzma files "
74 + "(try a raw LZMA stream instead)");
75
76 lz.setPresetDict(dictSize, presetDict);
77 }
78
79 props = (options.getPb() * 5 + options.getLp()) * 9 + options.getLc();
80
81 if (useHeader) {
82 // Props byte stores lc, lp, and pb.
83 out.write(props);
84
85 // Dictionary size is stored as a 32-bit unsigned little endian
86 // integer.
87 for (int i = 0; i < 4; ++i) {
88 out.write(dictSize & 0xFF);
89 dictSize >>>= 8;
90 }
91
92 // Uncompressed size is stored as a 64-bit unsigned little endian
93 // integer. The max value (-1 in two's complement) indicates
94 // unknown size.
95 for (int i = 0; i < 8; ++i)
96 out.write((int)(expectedUncompressedSize >>> (8 * i)) & 0xFF);
97 }
98 }
99
100 /**
101 * Creates a new compressor for the legacy .lzma file format.
102 * <p>
103 * If the uncompressed size of the input data is known, it will be stored
104 * in the .lzma header and no end of stream marker will be used. Otherwise
105 * the header will indicate unknown uncompressed size and the end of stream
106 * marker will be used.
107 * <p>
108 * Note that a preset dictionary cannot be used in .lzma files but
109 * it can be used for raw LZMA streams.
110 *
111 * @param out output stream to which the compressed data
112 * will be written
113 *
114 * @param options LZMA compression options; the same class
115 * is used here as is for LZMA2
116 *
117 * @param inputSize uncompressed size of the data to be compressed;
118 * use {@code -1} when unknown
119 *
120 * @throws IOException may be thrown from {@code out}
121 */
122 public LZMAOutputStream(OutputStream out, LZMA2Options options,
123 long inputSize)
124 throws IOException {
125 this(out, options, inputSize, ArrayCache.getDefaultCache());
126 }
127
128 /**
129 * Creates a new compressor for the legacy .lzma file format.
130 * <p>
131 * This is identical to
132 * {@code LZMAOutputStream(OutputStream, LZMA2Options, long)}
133 * except that this also takes the {@code arrayCache} argument.
134 *
135 * @param out output stream to which the compressed data
136 * will be written
137 *
138 * @param options LZMA compression options; the same class
139 * is used here as is for LZMA2
140 *
141 * @param inputSize uncompressed size of the data to be compressed;
142 * use {@code -1} when unknown
143 *
144 * @param arrayCache cache to be used for allocating large arrays
145 *
146 * @throws IOException may be thrown from {@code out}
147 *
148 * @since 1.7
149 */
150 public LZMAOutputStream(OutputStream out, LZMA2Options options,
151 long inputSize, ArrayCache arrayCache)
152 throws IOException {
153 this(out, options, true, inputSize == -1, inputSize, arrayCache);
154 }
155
156 /**
157 * Creates a new compressor for raw LZMA (also known as LZMA1) stream.
158 * <p>
159 * Raw LZMA streams can be encoded with or without end of stream marker.
160 * When decompressing the stream, one must know if the end marker was used
161 * and tell it to the decompressor. If the end marker wasn't used, the
162 * decompressor will also need to know the uncompressed size.
163 *
164 * @param out output stream to which the compressed data
165 * will be written
166 *
167 * @param options LZMA compression options; the same class
168 * is used here as is for LZMA2
169 *
170 * @param useEndMarker
171 * if end of stream marker should be written
172 *
173 * @throws IOException may be thrown from {@code out}
174 */
175 public LZMAOutputStream(OutputStream out, LZMA2Options options,
176 boolean useEndMarker) throws IOException {
177 this(out, options, useEndMarker, ArrayCache.getDefaultCache());
178 }
179
180 /**
181 * Creates a new compressor for raw LZMA (also known as LZMA1) stream.
182 * <p>
183 * This is identical to
184 * {@code LZMAOutputStream(OutputStream, LZMA2Options, boolean)}
185 * except that this also takes the {@code arrayCache} argument.
186 *
187 * @param out output stream to which the compressed data
188 * will be written
189 *
190 * @param options LZMA compression options; the same class
191 * is used here as is for LZMA2
192 *
193 * @param useEndMarker
194 * if end of stream marker should be written
195 *
196 * @param arrayCache cache to be used for allocating large arrays
197 *
198 * @throws IOException may be thrown from {@code out}
199 *
200 * @since 1.7
201 */
202 public LZMAOutputStream(OutputStream out, LZMA2Options options,
203 boolean useEndMarker, ArrayCache arrayCache)
204 throws IOException {
205 this(out, options, false, useEndMarker, -1, arrayCache);
206 }
207
208 /**
209 * Returns the LZMA lc/lp/pb properties encoded into a single byte.
210 * This might be useful when handling file formats other than .lzma
211 * that use the same encoding for the LZMA properties as .lzma does.
212 */
213 public int getProps() {
214 return props;
215 }
216
217 /**
218 * Gets the amount of uncompressed data written to the stream.
219 * This is useful when creating raw LZMA streams without
220 * the end of stream marker.
221 */
222 public long getUncompressedSize() {
223 return currentUncompressedSize;
224 }
225
226 @Override
227 public void write(int b) throws IOException {
228 tempBuf[0] = (byte)b;
229 write(tempBuf, 0, 1);
230 }
231
232 @Override
233 public void write(byte[] buf, int off, int len) throws IOException {
234 if (off < 0 || len < 0 || off + len < 0 || off + len > buf.length)
235 throw new IndexOutOfBoundsException();
236
237 if (exception != null)
238 throw exception;
239
240 if (finished)
241 throw new XZIOException("Stream finished or closed");
242
243 if (expectedUncompressedSize != -1
244 && expectedUncompressedSize - currentUncompressedSize < len)
245 throw new XZIOException("Expected uncompressed input size ("
246 + expectedUncompressedSize + " bytes) was exceeded");
247
248 currentUncompressedSize += len;
249
250 try {
251 while (len > 0) {
252 int used = lz.fillWindow(buf, off, len);
253 off += used;
254 len -= used;
255 lzma.encodeForLZMA1();
256 }
257 } catch (IOException e) {
258 exception = e;
259 throw e;
260 }
261 }
262
263 /**
264 * Flushing isn't supported and will throw XZIOException.
265 */
266 @Override
267 public void flush() throws IOException {
268 throw new XZIOException("LZMAOutputStream does not support flushing");
269 }
270
271 /**
272 * Finishes the stream without closing the underlying OutputStream.
273 */
274 @Override
275 public void finish() throws IOException {
276 if (!finished) {
277 if (exception != null)
278 throw exception;
279
280 try {
281 if (expectedUncompressedSize != -1
282 && expectedUncompressedSize != currentUncompressedSize)
283 throw new XZIOException("Expected uncompressed size ("
284 + expectedUncompressedSize + ") doesn't equal "
285 + "the number of bytes written to the stream ("
286 + currentUncompressedSize + ")");
287
288 lz.setFinishing();
289 lzma.encodeForLZMA1();
290
291 if (useEndMarker)
292 lzma.encodeLZMA1EndMarker();
293
294 rc.finish();
295 } catch (IOException e) {
296 exception = e;
297 throw e;
298 }
299
300 finished = true;
301
302 lzma.putArraysToCache(arrayCache);
303 lzma = null;
304 lz = null;
305 }
306 }
307
308 /**
309 * Finishes the stream and closes the underlying OutputStream.
310 */
311 @Override
312 public void close() throws IOException {
313 if (out != null) {
314 try {
315 finish();
316 } catch (IOException e) {}
317
318 try {
319 out.close();
320 } catch (IOException e) {
321 if (exception == null)
322 exception = e;
323 }
324
325 out = null;
326 }
327
328 if (exception != null)
329 throw exception;
330 }
331 }