    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one
    003 * or more contributor license agreements.  See the NOTICE file
    004 * distributed with this work for additional information
    005 * regarding copyright ownership.  The ASF licenses this file
    006 * to you under the Apache License, Version 2.0 (the
    007 * "License"); you may not use this file except in compliance
    008 * with the License.  You may obtain a copy of the License at
    009 *
    010 * http://www.apache.org/licenses/LICENSE-2.0
    011 *
    012 * Unless required by applicable law or agreed to in writing,
    013 * software distributed under the License is distributed on an
    014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015 * KIND, either express or implied.  See the License for the
    016 * specific language governing permissions and limitations
    017 * under the License.
    018 */
    019package org.apache.commons.compress.compressors.xz;
    020
    021import java.io.IOException;
    022import java.io.OutputStream;
    023
    024import org.apache.commons.compress.compressors.CompressorOutputStream;
    025import org.tukaani.xz.LZMA2Options;
    026import org.tukaani.xz.XZOutputStream;
    027
    028/**
    029 * XZ compressor.
    030 *
    031 * @since 1.4
    032 */
    033public class XZCompressorOutputStream extends CompressorOutputStream {
    034    private final XZOutputStream out;
    035
    036    /**
    037     * Creates a new XZ compressor using the default LZMA2 options. This is equivalent to {@code XZCompressorOutputStream(outputStream, 6)}.
    038     *
    039     * @param outputStream the stream to wrap
    040     * @throws IOException on error
    041     */
    042    public XZCompressorOutputStream(final OutputStream outputStream) throws IOException {
    043        out = new XZOutputStream(outputStream, new LZMA2Options());
    044    }
    045
    046    /**
    047     * Creates a new XZ compressor using the specified LZMA2 preset level.
    048     * <p>
    049     * 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.
    050     * <p>
    051     * The presets 7-9 are like the preset 6 but use bigger dictionaries and have higher compressor and decompressor memory requirements. Unless the
    052     * 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.
    053     *
    054     * @param outputStream the stream to wrap
    055     * @param preset       the preset
    056     * @throws IOException on error
    057     */
    058    public XZCompressorOutputStream(final OutputStream outputStream, final int preset) throws IOException {
    059        out = new XZOutputStream(outputStream, new LZMA2Options(preset));
    060    }
    061
    062    @Override
    063    public void close() throws IOException {
    064        out.close();
    065    }
    066
    067    /**
    068     * Finishes compression without closing the underlying stream. No more data can be written to this stream after finishing.
    069     *
    070     * @throws IOException on error
    071     */
    072    public void finish() throws IOException {
    073        out.finish();
    074    }
    075
    076    /**
    077     * Flushes the encoder and calls {@code outputStream.flush()}. All buffered pending data will then be decompressible from the output stream. Calling this
    078     * function very often may increase the compressed file size a lot.
    079     */
    080    @Override
    081    public void flush() throws IOException {
    082        out.flush();
    083    }
    084
    085    @Override
    086    public void write(final byte[] buf, final int off, final int len) throws IOException {
    087        out.write(buf, off, len);
    088    }
    089
    090    @Override
    091    public void write(final int b) throws IOException {
    092        out.write(b);
    093    }
    094}