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 * @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)}.
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 close() throws IOException {
069        out.close();
070    }
071
072    /**
073     * Finishes compression without closing the underlying stream.
074     * No more data can be written to this stream after finishing.
075     * @throws IOException on error
076     */
077    public void finish() throws IOException {
078        out.finish();
079    }
080
081    /**
082     * Flushes the encoder and calls {@code outputStream.flush()}.
083     * All buffered pending data will then be decompressible from
084     * the output stream. Calling this function very often may increase
085     * the compressed file size a lot.
086     */
087    @Override
088    public void flush() throws IOException {
089        out.flush();
090    }
091
092    @Override
093    public void write(final byte[] buf, final int off, final int len) throws IOException {
094        out.write(buf, off, len);
095    }
096
097    @Override
098    public void write(final int b) throws IOException {
099        out.write(b);
100    }
101}