    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 *      https://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.FilterWriter;
    021import java.io.IOException;
    022import java.io.UncheckedIOException;
    023import java.io.Writer;
    024
    025import org.apache.commons.io.build.AbstractStreamBuilder;
    026import org.apache.commons.io.function.Uncheck;
    027
    028/**
    029 * A {@link FilterWriter} that throws {@link UncheckedIOException} instead of {@link IOException}.
    030 * <p>
    031 * To build an instance, use {@link Builder}.
    032 * </p>
    033 *
    034 * @see Builder
    035 * @see FilterWriter
    036 * @see IOException
    037 * @see UncheckedIOException
    038 * @since 2.12.0
    039 */
    040public final class UncheckedFilterWriter extends FilterWriter {
    041
    042    // @formatter:off
    043    /**
    044     * Builds a new {@link UncheckedFilterWriter}.
    045     *
    046     * <p>
    047     * Using File IO:
    048     * </p>
    049     * <pre>{@code
    050     * UncheckedFilterWriter s = UncheckedFilterWriter.builder()
    051     *   .setFile(file)
    052     *   .get();}
    053     * </pre>
    054     * <p>
    055     * Using NIO Path:
    056     * </p>
    057     * <pre>{@code
    058     * UncheckedFilterWriter s = UncheckedFilterWriter.builder()
    059     *   .setPath(path)
    060     *   .get();}
    061     * </pre>
    062     *
    063     * @see #get()
    064     */
    065    // @formatter:on
    066    public static class Builder extends AbstractStreamBuilder<UncheckedFilterWriter, Builder> {
    067
    068        /**
    069         * Constructs a builder of {@link UncheckedFilterWriter}.
    070         */
    071        public Builder() {
    072            // empty
    073        }
    074
    075        /**
    076         * Builds a new {@link UncheckedFilterWriter}.
    077         * <p>
    078         * You must set an aspect that supports {@link #getWriter()} on this builder, otherwise, this method throws an exception.
    079         * </p>
    080         * <p>
    081         * This builder uses the following aspects:
    082         * </p>
    083         * <ul>
    084         * <li>{@link #getWriter()}</li>
    085         * </ul>
    086         *
    087         * @return a new instance.
    088         * @throws UnsupportedOperationException if the origin cannot provide a {@link Writer}.
    089         * @throws IOException                   if an I/O error occurs converting to an {@link Writer} using {@link #getWriter()}.
    090         * @see #getWriter()
    091         * @see #getUnchecked()
    092         */
    093        @Override
    094        public UncheckedFilterWriter get() throws IOException {
    095            return new UncheckedFilterWriter(this);
    096        }
    097
    098    }
    099
    100    /**
    101     * Constructs a new {@link Builder}.
    102     *
    103     * @return a new {@link Builder}.
    104     */
    105    public static Builder builder() {
    106        return new Builder();
    107    }
    108
    109    /**
    110     * Constructs a new filtered writer.
    111     *
    112     * @param builder a Writer object providing the underlying stream.
    113     * @throws NullPointerException if {@code builder} the its {@code Writer} is {@code null}.
    114     * @throws IOException          if an I/O error occurs converting to an {@link Writer} using {@link #getWriter()}.
    115     */
    116    @SuppressWarnings("resource") // Caller closes.
    117    private UncheckedFilterWriter(final Builder builder) throws IOException {
    118        super(builder.getWriter());
    119    }
    120
    121    /**
    122     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    123     */
    124    @Override
    125    public Writer append(final char c) throws UncheckedIOException {
    126        return Uncheck.apply(super::append, c);
    127    }
    128
    129    /**
    130     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    131     */
    132    @Override
    133    public Writer append(final CharSequence csq) throws UncheckedIOException {
    134        return Uncheck.apply(super::append, csq);
    135    }
    136
    137    /**
    138     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    139     */
    140    @Override
    141    public Writer append(final CharSequence csq, final int start, final int end) throws UncheckedIOException {
    142        return Uncheck.apply(super::append, csq, start, end);
    143    }
    144
    145    /**
    146     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    147     */
    148    @Override
    149    public void close() throws UncheckedIOException {
    150        Uncheck.run(super::close);
    151    }
    152
    153    /**
    154     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    155     */
    156    @Override
    157    public void flush() throws UncheckedIOException {
    158        Uncheck.run(super::flush);
    159    }
    160
    161    /**
    162     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    163     */
    164    @Override
    165    public void write(final char[] cbuf) throws UncheckedIOException {
    166        Uncheck.accept(super::write, cbuf);
    167    }
    168
    169    /**
    170     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    171     */
    172    @Override
    173    public void write(final char[] cbuf, final int off, final int len) throws UncheckedIOException {
    174        Uncheck.accept(super::write, cbuf, off, len);
    175    }
    176
    177    /**
    178     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    179     */
    180    @Override
    181    public void write(final int c) throws UncheckedIOException {
    182        Uncheck.accept(super::write, c);
    183    }
    184
    185    /**
    186     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    187     */
    188    @Override
    189    public void write(final String str) throws UncheckedIOException {
    190        Uncheck.accept(super::write, str);
    191    }
    192
    193    /**
    194     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    195     */
    196    @Override
    197    public void write(final String str, final int off, final int len) throws UncheckedIOException {
    198        Uncheck.accept(super::write, str, off, len);
    199    }
    200
    201}