    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.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, see {@link Builder}.
    032 * </p>
    033 *
    034 * @see FilterWriter
    035 * @see IOException
    036 * @see UncheckedIOException
    037 * @since 2.12.0
    038 */
    039public final class UncheckedFilterWriter extends FilterWriter {
    040
    041    /**
    042     * Builds a new {@link UncheckedFilterWriter} instance.
    043     * <p>
    044     * Using File IO:
    045     * </p>
    046     * <pre>{@code
    047     * UncheckedFilterWriter s = UncheckedFilterWriter.builder()
    048     *   .setFile(file)
    049     *   .get();}
    050     * </pre>
    051     * <p>
    052     * Using NIO Path:
    053     * </p>
    054     * <pre>{@code
    055     * UncheckedFilterWriter s = UncheckedFilterWriter.builder()
    056     *   .setPath(path)
    057     *   .get();}
    058     * </pre>
    059     */
    060    public static class Builder extends AbstractStreamBuilder<UncheckedFilterWriter, Builder> {
    061
    062        /**
    063         * Constructs a new instance.
    064         * <p>
    065         * This builder use the aspects Writer, OpenOption[], and Charset.
    066         * </p>
    067         * <p>
    068         * You must provide an origin that can be converted to a Writer by this builder, otherwise, this call will throw an
    069         * {@link UnsupportedOperationException}.
    070         * </p>
    071         *
    072         * @return a new instance.
    073         * @throws UnsupportedOperationException if the origin cannot provide a Writer.
    074         * @see #getWriter()
    075         */
    076        @SuppressWarnings("resource")
    077        @Override
    078        public UncheckedFilterWriter get() throws IOException {
    079            return new UncheckedFilterWriter(getWriter());
    080        }
    081
    082    }
    083
    084    /**
    085     * Constructs a new {@link Builder}.
    086     *
    087     * @return a new {@link Builder}.
    088     */
    089    public static Builder builder() {
    090        return new Builder();
    091    }
    092
    093    /**
    094     * Creates a new filtered writer.
    095     *
    096     * @param writer a Writer object providing the underlying stream.
    097     * @throws NullPointerException if {@code writer} is {@code null}.
    098     */
    099    private UncheckedFilterWriter(final Writer writer) {
    100        super(writer);
    101    }
    102
    103    /**
    104     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    105     */
    106    @Override
    107    public Writer append(final char c) throws UncheckedIOException {
    108        return Uncheck.apply(super::append, c);
    109    }
    110
    111    /**
    112     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    113     */
    114    @Override
    115    public Writer append(final CharSequence csq) throws UncheckedIOException {
    116        return Uncheck.apply(super::append, csq);
    117    }
    118
    119    /**
    120     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    121     */
    122    @Override
    123    public Writer append(final CharSequence csq, final int start, final int end) throws UncheckedIOException {
    124        return Uncheck.apply(super::append, csq, start, end);
    125    }
    126
    127    /**
    128     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    129     */
    130    @Override
    131    public void close() throws UncheckedIOException {
    132        Uncheck.run(super::close);
    133    }
    134
    135    /**
    136     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    137     */
    138    @Override
    139    public void flush() throws UncheckedIOException {
    140        Uncheck.run(super::flush);
    141    }
    142
    143    /**
    144     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    145     */
    146    @Override
    147    public void write(final char[] cbuf) throws UncheckedIOException {
    148        Uncheck.accept(super::write, cbuf);
    149    }
    150
    151    /**
    152     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    153     */
    154    @Override
    155    public void write(final char[] cbuf, final int off, final int len) throws UncheckedIOException {
    156        Uncheck.accept(super::write, cbuf, off, len);
    157    }
    158
    159    /**
    160     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    161     */
    162    @Override
    163    public void write(final int c) throws UncheckedIOException {
    164        Uncheck.accept(super::write, c);
    165    }
    166
    167    /**
    168     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    169     */
    170    @Override
    171    public void write(final String str) throws UncheckedIOException {
    172        Uncheck.accept(super::write, str);
    173    }
    174
    175    /**
    176     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    177     */
    178    @Override
    179    public void write(final String str, final int off, final int len) throws UncheckedIOException {
    180        Uncheck.accept(super::write, str, off, len);
    181    }
    182
    183}