    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;
    023import org.tukaani.xz.LZMA2Options;
    024import org.tukaani.xz.XZOutputStream;
    025
    026import org.apache.commons.compress.compressors.CompressorOutputStream;
    027
    028/**
    029 * XZ compressor.
    030 * @since 1.4
    031 */
    032public class XZCompressorOutputStream extends CompressorOutputStream {
    033    private final XZOutputStream out;
    034
    035    /**
    036     * Creates a new XZ compressor using the default LZMA2 options.
    037     * This is equivalent to <code>XZCompressorOutputStream(outputStream, 6)</code>.
    038     * @param outputStream the stream to wrap
    039     * @throws IOException on error
    040     */
    041    public XZCompressorOutputStream(final OutputStream outputStream)
    042            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.
    050     * The presets 4-6 are fairly slow presets with high compression.
    051     * The default preset is 6.
    052     * <p>
    053     * The presets 7-9 are like the preset 6 but use bigger dictionaries
    054     * and have higher compressor and decompressor memory requirements.
    055     * Unless the uncompressed size of the file exceeds 8&nbsp;MiB,
    056     * 16&nbsp;MiB, or 32&nbsp;MiB, it is waste of memory to use the
    057     * presets 7, 8, or 9, respectively.
    058     * @param outputStream the stream to wrap
    059     * @param preset the preset
    060     * @throws IOException on error
    061     */
    062    public XZCompressorOutputStream(final OutputStream outputStream, final int preset)
    063            throws IOException {
    064        out = new XZOutputStream(outputStream, new LZMA2Options(preset));
    065    }
    066
    067    @Override
    068    public void write(final int b) throws IOException {
    069        out.write(b);
    070    }
    071
    072    @Override
    073    public void write(final byte[] buf, final int off, final int len) throws IOException {
    074        out.write(buf, off, len);
    075    }
    076
    077    /**
    078     * Flushes the encoder and calls <code>outputStream.flush()</code>.
    079     * All buffered pending data will then be decompressible from
    080     * the output stream. Calling this function very often may increase
    081     * the compressed file size a lot.
    082     */
    083    @Override
    084    public void flush() throws IOException {
    085        out.flush();
    086    }
    087
    088    /**
    089     * Finishes compression without closing the underlying stream.
    090     * No more data can be written to this stream after finishing.
    091     * @throws IOException on error
    092     */
    093    public void finish() throws IOException {
    094        out.finish();
    095    }
    096
    097    @Override
    098    public void close() throws IOException {
    099        out.close();
    100    }
    101}