| 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.commons.compress.compressors.lzma; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.io.OutputStream; |
| 23 | |
| 24 | import org.apache.commons.compress.compressors.CompressorOutputStream; |
| 25 | import org.apache.commons.io.build.AbstractStreamBuilder; |
| 26 | import org.tukaani.xz.LZMA2Options; |
| 27 | import org.tukaani.xz.LZMAOutputStream; |
| 28 | |
| 29 | /** |
| 30 | * LZMA compressor. |
| 31 | * |
| 32 | * @since 1.13 |
| 33 | */ |
| 34 | public class LZMACompressorOutputStream extends CompressorOutputStream<LZMAOutputStream> { |
| 35 | |
| 36 | // @formatter:off |
| 37 | /** |
| 38 | * Builds a new {@link LZMACompressorOutputStream}. |
| 39 | * |
| 40 | * <p> |
| 41 | * For example: |
| 42 | * </p> |
| 43 | * <pre>{@code |
| 44 | * LZMACompressorOutputStream s = LZMACompressorOutputStream.builder() |
| 45 | * .setPath(path) |
| 46 | * .setLzma2Options(new LZMA2Options(...)) |
| 47 | * .get(); |
| 48 | * } |
| 49 | * </pre> |
| 50 | * |
| 51 | * @see #get() |
| 52 | * @see LZMA2Options |
| 53 | * @since 1.28.0 |
| 54 | */ |
| 55 | // @formatter:on |
| 56 | public static class Builder extends AbstractStreamBuilder<LZMACompressorOutputStream, Builder> { |
| 57 | |
| 58 | private LZMA2Options lzma2Options = new LZMA2Options(); |
| 59 | |
| 60 | /** |
| 61 | * Constructs a new builder of {@link LZMACompressorOutputStream}. |
| 62 | */ |
| 63 | public Builder() { |
| 64 | // empty |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public LZMACompressorOutputStream get() throws IOException { |
| 69 | return new LZMACompressorOutputStream(this); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Sets LZMA options. |
| 74 | * <p> |
| 75 | * Passing {@code null} resets to the default value {@link LZMA2Options#LZMA2Options()}. |
| 76 | * </p> |
| 77 | * |
| 78 | * @param lzma2Options LZMA options. |
| 79 | * @return this instance. |
| 80 | */ |
| 81 | public Builder setLzma2Options(final LZMA2Options lzma2Options) { |
| 82 | this.lzma2Options = lzma2Options != null ? lzma2Options : new LZMA2Options(); |
| 83 | return this; |
| 84 | } |
| 85 | |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Constructs a new builder of {@link LZMACompressorOutputStream}. |
| 90 | * |
| 91 | * @return a new builder of {@link LZMACompressorOutputStream}. |
| 92 | * @since 1.28.0 |
| 93 | */ |
| 94 | public static Builder builder() { |
| 95 | return new Builder(); |
| 96 | } |
| 97 | |
| 98 | @SuppressWarnings("resource") // Caller closes |
| 99 | 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 | } |