    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.lzma;
    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.LZMAOutputStream;
    027
    028/**
    029 * LZMA compressor.
    030 *
    031 * @since 1.13
    032 */
    033public class LZMACompressorOutputStream extends CompressorOutputStream {
    034    private final LZMAOutputStream out;
    035
    036    /**
    037     * Creates a LZMA compressor.
    038     *
    039     * @param outputStream the stream to wrap
    040     * @throws IOException on error
    041     */
    042    public LZMACompressorOutputStream(final OutputStream outputStream) throws IOException {
    043        out = new LZMAOutputStream(outputStream, new LZMA2Options(), -1);
    044    }
    045
    046    /** {@inheritDoc} */
    047    @Override
    048    public void close() throws IOException {
    049        out.close();
    050    }
    051
    052    /**
    053     * Finishes compression without closing the underlying stream. No more data can be written to this stream after finishing.
    054     *
    055     * @throws IOException on error
    056     */
    057    public void finish() throws IOException {
    058        out.finish();
    059    }
    060
    061    /**
    062     * Doesn't do anything as {@link LZMAOutputStream} doesn't support flushing.
    063     */
    064    @Override
    065    public void flush() throws IOException {
    066        // noop
    067    }
    068
    069    /** {@inheritDoc} */
    070    @Override
    071    public void write(final byte[] buf, final int off, final int len) throws IOException {
    072        out.write(buf, off, len);
    073    }
    074
    075    /** {@inheritDoc} */
    076    @Override
    077    public void write(final int b) throws IOException {
    078        out.write(b);
    079    }
    080}