    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.input;
    019
    020import java.io.FilterReader;
    021import java.io.IOException;
    022import java.io.Reader;
    023import java.io.UncheckedIOException;
    024import java.nio.CharBuffer;
    025
    026import org.apache.commons.io.build.AbstractStreamBuilder;
    027import org.apache.commons.io.function.Uncheck;
    028
    029/**
    030 * A {@link FilterReader} that throws {@link UncheckedIOException} instead of {@link IOException}.
    031 * <p>
    032 * To build an instance, use {@link Builder}.
    033 * </p>
    034 *
    035 * @see Builder
    036 * @see FilterReader
    037 * @see IOException
    038 * @see UncheckedIOException
    039 * @since 2.12.0
    040 */
    041public final class UncheckedFilterReader extends FilterReader {
    042
    043    // @formatter:off
    044    /**
    045     * Builds a new {@link UncheckedFilterReader}.
    046     *
    047     * <p>
    048     * Using File IO:
    049     * </p>
    050     * <pre>{@code
    051     * UncheckedFilterReader s = UncheckedFilterReader.builder()
    052     *   .setFile(file)
    053     *   .get();}
    054     * </pre>
    055     * <p>
    056     * Using NIO Path:
    057     * </p>
    058     * <pre>{@code
    059     * UncheckedFilterReader s = UncheckedFilterReader.builder()
    060     *   .setPath(path)
    061     *   .get();}
    062     * </pre>
    063     *
    064     * @see #get()
    065     */
    066    // @formatter:on
    067    public static class Builder extends AbstractStreamBuilder<UncheckedFilterReader, Builder> {
    068
    069        /**
    070         * Builds a new {@link UncheckedFilterReader}.
    071         * <p>
    072         * You must set input that supports {@link #getReader()} on this builder, otherwise, this method throws an exception.
    073         * </p>
    074         * <p>
    075         * This builder use the following aspects:
    076         * </p>
    077         * <ul>
    078         * <li>{@link #getReader()}</li>
    079         * </ul>
    080         *
    081         * @return a new instance.
    082         * @throws UnsupportedOperationException if the origin cannot provide a Reader.
    083         * @throws IllegalStateException if the {@code origin} is {@code null}.
    084         * @see #getReader()
    085         */
    086        @Override
    087        public UncheckedFilterReader get() {
    088            // This an unchecked class, so this method is as well.
    089            return Uncheck.get(() -> new UncheckedFilterReader(getReader()));
    090        }
    091
    092    }
    093
    094    /**
    095     * Constructs a new {@link Builder}.
    096     *
    097     * @return a new {@link Builder}.
    098     */
    099    public static Builder builder() {
    100        return new Builder();
    101    }
    102
    103    /**
    104     * Constructs a new filtered reader.
    105     *
    106     * @param reader a Reader object providing the underlying stream.
    107     * @throws NullPointerException if {@code reader} is {@code null}.
    108     */
    109    private UncheckedFilterReader(final Reader reader) {
    110        super(reader);
    111    }
    112
    113    /**
    114     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    115     */
    116    @Override
    117    public void close() throws UncheckedIOException {
    118        Uncheck.run(super::close);
    119    }
    120
    121    /**
    122     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    123     */
    124    @Override
    125    public void mark(final int readAheadLimit) throws UncheckedIOException {
    126        Uncheck.accept(super::mark, readAheadLimit);
    127    }
    128
    129    /**
    130     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    131     */
    132    @Override
    133    public int read() throws UncheckedIOException {
    134        return Uncheck.get(super::read);
    135    }
    136
    137    /**
    138     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    139     */
    140    @Override
    141    public int read(final char[] cbuf) throws UncheckedIOException {
    142        return Uncheck.apply(super::read, cbuf);
    143    }
    144
    145    /**
    146     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    147     */
    148    @Override
    149    public int read(final char[] cbuf, final int off, final int len) throws UncheckedIOException {
    150        return Uncheck.apply(super::read, cbuf, off, len);
    151    }
    152
    153    /**
    154     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    155     */
    156    @Override
    157    public int read(final CharBuffer target) throws UncheckedIOException {
    158        return Uncheck.apply(super::read, target);
    159    }
    160
    161    /**
    162     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    163     */
    164    @Override
    165    public boolean ready() throws UncheckedIOException {
    166        return Uncheck.get(super::ready);
    167    }
    168
    169    /**
    170     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    171     */
    172    @Override
    173    public void reset() throws UncheckedIOException {
    174        Uncheck.run(super::reset);
    175    }
    176
    177    /**
    178     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    179     */
    180    @Override
    181    public long skip(final long n) throws UncheckedIOException {
    182        return Uncheck.apply(super::skip, n);
    183    }
    184
    185}