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   * <em>Calling flush()</em>
32   * <p>
33   * Calling {@link #flush()} flushes the encoder and calls {@code outputStream.flush()}. All buffered pending data will then be decompressible from the output
34   * stream. Calling this function very often may increase the compressed file size a lot.
35   * </p>
36   *
37   * @since 1.4
38   */
39  public class XZCompressorOutputStream extends CompressorOutputStream<XZOutputStream> {
40  
41      /**
42       * Creates a new XZ compressor using the default LZMA2 options. This is equivalent to {@code XZCompressorOutputStream(outputStream, 6)}.
43       *
44       * @param outputStream the stream to wrap
45       * @throws IOException on error
46       */
47      @SuppressWarnings("resource") // Caller closes
48      public XZCompressorOutputStream(final OutputStream outputStream) throws IOException {
49          super(new XZOutputStream(outputStream, new LZMA2Options()));
50      }
51  
52      /**
53       * Creates a new XZ compressor using the specified LZMA2 preset level.
54       * <p>
55       * 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.
56       * <p>
57       * The presets 7-9 are like the preset 6 but use bigger dictionaries and have higher compressor and decompressor memory requirements. Unless the
58       * 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.
59       *
60       * @param outputStream the stream to wrap
61       * @param preset       the preset
62       * @throws IOException on error
63       */
64      @SuppressWarnings("resource") // Caller closes
65      public XZCompressorOutputStream(final OutputStream outputStream, final int preset) throws IOException {
66          super(new XZOutputStream(outputStream, new LZMA2Options(preset)));
67      }
68  
69      /**
70       * Finishes compression without closing the underlying stream. No more data can be written to this stream after finishing.
71       *
72       * @throws IOException on error
73       */
74      @SuppressWarnings("resource") // instance variable access
75      public void finish() throws IOException {
76          out().finish();
77      }
78  
79      @Override
80      public void write(final byte[] buf, final int off, final int len) throws IOException {
81          out.write(buf, off, len);
82      }
83  
84  }