blob: e512b6605ebdf6de833479d3241bdc7b17870d72 [file] [log] [blame]
Stefan Bodewigaa739262011-11-02 16:13:41 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.commons.compress.compressors.xz;
20
21import java.io.IOException;
22import java.io.OutputStream;
23import org.tukaani.xz.LZMA2Options;
24import org.tukaani.xz.XZOutputStream;
25
26import org.apache.commons.compress.compressors.CompressorOutputStream;
27
28/**
29 * XZ compressor.
Sebastian Bazleyddbca722013-03-11 14:14:47 +000030 * @since 1.4
Stefan Bodewigaa739262011-11-02 16:13:41 +000031 */
32public class XZCompressorOutputStream extends CompressorOutputStream {
33 private final XZOutputStream out;
34
35 /**
36 * Creates a new XZ compressor using the default LZMA2 options.
37 * This is equivalent to <code>XZCompressorOutputStream(6)</code>.
38 */
39 public XZCompressorOutputStream(OutputStream outputStream)
40 throws IOException {
41 out = new XZOutputStream(outputStream, new LZMA2Options());
42 }
43
44 /**
45 * Creates a new XZ compressor using the specified LZMA2 preset level.
46 * <p>
47 * The presets 0-3 are fast presets with medium compression.
48 * The presets 4-6 are fairly slow presets with high compression.
49 * The default preset is 6.
50 * <p>
51 * The presets 7-9 are like the preset 6 but use bigger dictionaries
52 * and have higher compressor and decompressor memory requirements.
53 * Unless the uncompressed size of the file exceeds 8&nbsp;MiB,
54 * 16&nbsp;MiB, or 32&nbsp;MiB, it is waste of memory to use the
55 * presets 7, 8, or 9, respectively.
56 */
57 public XZCompressorOutputStream(OutputStream outputStream, int preset)
58 throws IOException {
59 out = new XZOutputStream(outputStream, new LZMA2Options(preset));
60 }
61
Sebastian Bazley8862a482011-11-09 18:26:00 +000062 @Override
Stefan Bodewigaa739262011-11-02 16:13:41 +000063 public void write(int b) throws IOException {
64 out.write(b);
65 }
66
Sebastian Bazley8862a482011-11-09 18:26:00 +000067 @Override
Stefan Bodewigaa739262011-11-02 16:13:41 +000068 public void write(byte[] buf, int off, int len) throws IOException {
69 out.write(buf, off, len);
70 }
71
72 /**
73 * Flushes the encoder and calls <code>outputStream.flush()</code>.
74 * All buffered pending data will then be decompressible from
75 * the output stream. Calling this function very often may increase
76 * the compressed file size a lot.
77 */
Sebastian Bazley8862a482011-11-09 18:26:00 +000078 @Override
Stefan Bodewigaa739262011-11-02 16:13:41 +000079 public void flush() throws IOException {
80 out.flush();
81 }
82
83 /**
84 * Finishes compression without closing the underlying stream.
85 * No more data can be written to this stream after finishing.
86 */
87 public void finish() throws IOException {
88 out.finish();
89 }
90
Sebastian Bazley8862a482011-11-09 18:26:00 +000091 @Override
Stefan Bodewigaa739262011-11-02 16:13:41 +000092 public void close() throws IOException {
93 out.close();
94 }
95}