View Javadoc
1   /*
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   */
19  package org.apache.commons.compress.compressors.xz;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  
24  import org.apache.commons.compress.compressors.CompressorOutputStream;
25  import org.tukaani.xz.LZMA2Options;
26  import org.tukaani.xz.XZOutputStream;
27  
28  /**
29   * XZ compressor.
30   *
31   * @since 1.4
32   */
33  public class XZCompressorOutputStream extends CompressorOutputStream {
34      private final XZOutputStream out;
35  
36      /**
37       * Creates a new XZ compressor using the default LZMA2 options. This is equivalent to {@code XZCompressorOutputStream(outputStream, 6)}.
38       *
39       * @param outputStream the stream to wrap
40       * @throws IOException on error
41       */
42      public XZCompressorOutputStream(final OutputStream outputStream) throws IOException {
43          out = new XZOutputStream(outputStream, new LZMA2Options());
44      }
45  
46      /**
47       * Creates a new XZ compressor using the specified LZMA2 preset level.
48       * <p>
49       * The presets 0-3 are fast presets with medium compression. The presets 4-6 are fairly slow presets with high compression. The default preset is 6.
50       * <p>
51       * The presets 7-9 are like the preset 6 but use bigger dictionaries and have higher compressor and decompressor memory requirements. Unless the
52       * uncompressed size of the file exceeds 8&nbsp;MiB, 16&nbsp;MiB, or 32&nbsp;MiB, it is waste of memory to use the presets 7, 8, or 9, respectively.
53       *
54       * @param outputStream the stream to wrap
55       * @param preset       the preset
56       * @throws IOException on error
57       */
58      public XZCompressorOutputStream(final OutputStream outputStream, final int preset) throws IOException {
59          out = new XZOutputStream(outputStream, new LZMA2Options(preset));
60      }
61  
62      @Override
63      public void close() throws IOException {
64          out.close();
65      }
66  
67      /**
68       * Finishes compression without closing the underlying stream. No more data can be written to this stream after finishing.
69       *
70       * @throws IOException on error
71       */
72      public void finish() throws IOException {
73          out.finish();
74      }
75  
76      /**
77       * Flushes the encoder and calls {@code outputStream.flush()}. All buffered pending data will then be decompressible from the output stream. Calling this
78       * function very often may increase the compressed file size a lot.
79       */
80      @Override
81      public void flush() throws IOException {
82          out.flush();
83      }
84  
85      @Override
86      public void write(final byte[] buf, final int off, final int len) throws IOException {
87          out.write(buf, off, len);
88      }
89  
90      @Override
91      public void write(final int b) throws IOException {
92          out.write(b);
93      }
94  }