001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.io.output;
019
020import java.io.FilterOutputStream;
021import java.io.IOException;
022import java.io.OutputStream;
023import java.io.UncheckedIOException;
024
025import org.apache.commons.io.build.AbstractStreamBuilder;
026import org.apache.commons.io.function.Uncheck;
027
028/**
029 * A {@link FilterOutputStream} that throws {@link UncheckedIOException} instead of {@link UncheckedIOException}.
030 * <p>
031 * To build an instance, use {@link Builder}.
032 * </p>
033 *
034 * @see Builder
035 * @see FilterOutputStream
036 * @see UncheckedIOException
037 * @since 2.12.0
038 */
039public final class UncheckedFilterOutputStream extends FilterOutputStream {
040
041    // @formatter:off
042    /**
043     * Builds a new {@link UncheckedFilterOutputStream}.
044     *
045     * <p>
046     * Using File IO:
047     * </p>
048     * <pre>{@code
049     * UncheckedFilterOutputStream s = UncheckedFilterOutputStream.builder()
050     *   .setFile(file)
051     *   .get();}
052     * </pre>
053     * <p>
054     * Using NIO Path:
055     * </p>
056     * <pre>{@code
057     * UncheckedFilterOutputStream s = UncheckedFilterOutputStream.builder()
058     *   .setPath(path)
059     *   .get();}
060     * </pre>
061     *
062     * @see #get()
063     */
064    // @formatter:on
065    public static class Builder extends AbstractStreamBuilder<UncheckedFilterOutputStream, Builder> {
066
067        /**
068         * Builds a new {@link UncheckedFilterOutputStream}.
069         * <p>
070         * You must set input that supports {@link #getOutputStream()} on this builder, otherwise, this method throws an exception.
071         * </p>
072         * <p>
073         * This builder use the following aspects:
074         * </p>
075         * <ul>
076         * <li>{@link #getOutputStream()}</li>
077         * </ul>
078         *
079         * @return a new instance.
080         * @throws IllegalStateException         if the {@code origin} is {@code null}.
081         * @throws UnsupportedOperationException if the origin cannot be converted to an {@link OutputStream}.
082         * @throws IOException                   if an I/O error occurs.
083         * @see #getOutputStream()
084         */
085        @SuppressWarnings("resource")
086        @Override
087        public UncheckedFilterOutputStream get() throws IOException {
088            return new UncheckedFilterOutputStream(getOutputStream());
089        }
090
091    }
092
093    /**
094     * Constructs a new {@link Builder}.
095     *
096     * @return a new {@link Builder}.
097     */
098    public static Builder builder() {
099        return new Builder();
100    }
101
102    /**
103     * Constructs an output stream filter built on top of the specified underlying output stream.
104     *
105     * @param outputStream the underlying output stream, or {@code null} if this instance is to be created without an
106     *        underlying stream.
107     */
108    private UncheckedFilterOutputStream(final OutputStream outputStream) {
109        super(outputStream);
110    }
111
112    /**
113     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
114     */
115    @Override
116    public void close() throws UncheckedIOException {
117        Uncheck.run(super::close);
118    }
119
120    /**
121     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
122     */
123    @Override
124    public void flush() throws UncheckedIOException {
125        Uncheck.run(super::flush);
126    }
127
128    /**
129     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
130     */
131    @Override
132    public void write(final byte[] b) throws UncheckedIOException {
133        Uncheck.accept(super::write, b);
134    }
135
136    /**
137     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
138     */
139    @Override
140    public void write(final byte[] b, final int off, final int len) throws UncheckedIOException {
141        Uncheck.accept(super::write, b, off, len);
142    }
143
144    /**
145     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
146     */
147    @Override
148    public void write(final int b) throws UncheckedIOException {
149        Uncheck.accept(super::write, b);
150    }
151
152}