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 *   https://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.apache.commons.io.build.AbstractStreamBuilder;
026import org.tukaani.xz.LZMA2Options;
027import org.tukaani.xz.LZMAOutputStream;
028
029/**
030 * LZMA compressor.
031 *
032 * @since 1.13
033 */
034public class LZMACompressorOutputStream extends CompressorOutputStream<LZMAOutputStream> {
035
036    // @formatter:off
037    /**
038     * Builds a new {@link LZMACompressorOutputStream}.
039     *
040     * <p>
041     * For example:
042     * </p>
043     * <pre>{@code
044     * LZMACompressorOutputStream s = LZMACompressorOutputStream.builder()
045     *   .setPath(path)
046     *   .setLzma2Options(new LZMA2Options(...))
047     *   .get();
048     * }
049     * </pre>
050     *
051     * @see #get()
052     * @see LZMA2Options
053     * @since 1.28.0
054     */
055    // @formatter:on
056    public static class Builder extends AbstractStreamBuilder<LZMACompressorOutputStream, Builder> {
057
058        private LZMA2Options lzma2Options = new LZMA2Options();
059
060        /**
061         * Constructs a new builder of {@link LZMACompressorOutputStream}.
062         */
063        public Builder() {
064            // empty
065        }
066
067        @Override
068        public LZMACompressorOutputStream get() throws IOException {
069            return new LZMACompressorOutputStream(this);
070        }
071
072        /**
073         * Sets LZMA options.
074         * <p>
075         * Passing {@code null} resets to the default value {@link LZMA2Options#LZMA2Options()}.
076         * </p>
077         *
078         * @param lzma2Options LZMA options.
079         * @return this instance.
080         */
081        public Builder setLzma2Options(final LZMA2Options lzma2Options) {
082            this.lzma2Options = lzma2Options != null ? lzma2Options : new LZMA2Options();
083            return this;
084        }
085
086    }
087
088    /**
089     * Constructs a new builder of {@link LZMACompressorOutputStream}.
090     *
091     * @return a new builder of {@link LZMACompressorOutputStream}.
092     * @since 1.28.0
093     */
094    public static Builder builder() {
095        return new Builder();
096    }
097
098    @SuppressWarnings("resource") // Caller closes
099    private LZMACompressorOutputStream(final Builder builder) throws IOException {
100        super(new LZMAOutputStream(builder.getOutputStream(), builder.lzma2Options, -1));
101    }
102
103    /**
104     * Creates a LZMA compressor.
105     *
106     * @param outputStream the stream to wrap
107     * @throws IOException on error
108     */
109    public LZMACompressorOutputStream(final OutputStream outputStream) throws IOException {
110        this(builder().setOutputStream(outputStream));
111    }
112
113    /**
114     * Finishes compression without closing the underlying stream. No more data can be written to this stream after finishing.
115     *
116     * @throws IOException on error
117     */
118    @Override
119    @SuppressWarnings("resource") // instance variable access
120    public void finish() throws IOException {
121        out().finish();
122    }
123
124    /**
125     * Doesn't do anything as {@link LZMAOutputStream} doesn't support flushing.
126     */
127    @Override
128    public void flush() throws IOException {
129        // noop
130    }
131
132    /** {@inheritDoc} */
133    @Override
134    public void write(final byte[] buf, final int off, final int len) throws IOException {
135        out.write(buf, off, len);
136    }
137
138}