XZCompressorOutputStream.java

  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. import java.io.IOException;
  21. import java.io.OutputStream;

  22. import org.apache.commons.compress.compressors.CompressorOutputStream;
  23. import org.tukaani.xz.LZMA2Options;
  24. import org.tukaani.xz.XZOutputStream;

  25. /**
  26.  * XZ compressor.
  27.  *
  28.  * <em>Calling flush()</em>
  29.  * <p>
  30.  * Calling {@link #flush()} flushes the encoder and calls {@code outputStream.flush()}. All buffered pending data will then be decompressible from the output
  31.  * stream. Calling this function very often may increase the compressed file size a lot.
  32.  * </p>
  33.  *
  34.  * @since 1.4
  35.  */
  36. public class XZCompressorOutputStream extends CompressorOutputStream<XZOutputStream> {

  37.     /**
  38.      * Creates a new XZ compressor using the default LZMA2 options. This is equivalent to {@code XZCompressorOutputStream(outputStream, 6)}.
  39.      *
  40.      * @param outputStream the stream to wrap
  41.      * @throws IOException on error
  42.      */
  43.     @SuppressWarnings("resource") // Caller closes
  44.     public XZCompressorOutputStream(final OutputStream outputStream) throws IOException {
  45.         super(new XZOutputStream(outputStream, new LZMA2Options()));
  46.     }

  47.     /**
  48.      * Creates a new XZ compressor using the specified LZMA2 preset level.
  49.      * <p>
  50.      * 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.
  51.      * <p>
  52.      * The presets 7-9 are like the preset 6 but use bigger dictionaries and have higher compressor and decompressor memory requirements. Unless the
  53.      * 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.
  54.      *
  55.      * @param outputStream the stream to wrap
  56.      * @param preset       the preset
  57.      * @throws IOException on error
  58.      */
  59.     @SuppressWarnings("resource") // Caller closes
  60.     public XZCompressorOutputStream(final OutputStream outputStream, final int preset) throws IOException {
  61.         super(new XZOutputStream(outputStream, new LZMA2Options(preset)));
  62.     }

  63.     /**
  64.      * Finishes compression without closing the underlying stream. No more data can be written to this stream after finishing.
  65.      *
  66.      * @throws IOException on error
  67.      */
  68.     @SuppressWarnings("resource") // instance variable access
  69.     public void finish() throws IOException {
  70.         out().finish();
  71.     }

  72.     @Override
  73.     public void write(final byte[] buf, final int off, final int len) throws IOException {
  74.         out.write(buf, off, len);
  75.     }

  76. }