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