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