    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.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         * Constructs a new builder of {@link UncheckedFilterReader}.
    071         */
    072        public Builder() {
    073            // empty
    074        }
    075
    076        /**
    077         * Builds a new {@link UncheckedFilterReader}.
    078         * <p>
    079         * You must set an aspect that supports {@link #getReader()} on this builder, otherwise, this method throws an exception.
    080         * </p>
    081         * <p>
    082         * This builder uses the following aspects:
    083         * </p>
    084         * <ul>
    085         * <li>{@link #getReader()}</li>
    086         * </ul>
    087         *
    088         * @return a new instance.
    089         * @throws UnsupportedOperationException if the origin cannot provide a {@link Reader}.
    090         * @throws IllegalStateException if the {@code origin} is {@code null}.
    091         * @see #getReader()
    092         * @see #getUnchecked()
    093         */
    094        @Override
    095        public UncheckedFilterReader get() {
    096            // This an unchecked class, so this method is as well.
    097            return Uncheck.get(() -> new UncheckedFilterReader(this));
    098        }
    099
    100    }
    101
    102    /**
    103     * Constructs a new {@link Builder}.
    104     *
    105     * @return a new {@link Builder}.
    106     */
    107    public static Builder builder() {
    108        return new Builder();
    109    }
    110
    111    /**
    112     * Constructs a new filtered reader.
    113     *
    114     * @param builder a Builder object providing the underlying stream.
    115     * @throws IOException          if an I/O error occurs.
    116     * @throws NullPointerException if {@code reader} is {@code null}.
    117     */
    118    @SuppressWarnings("resource")
    119    private UncheckedFilterReader(final Builder builder) throws IOException {
    120        super(builder.getReader());
    121    }
    122
    123    /**
    124     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    125     */
    126    @Override
    127    public void close() throws UncheckedIOException {
    128        Uncheck.run(super::close);
    129    }
    130
    131    /**
    132     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    133     */
    134    @Override
    135    public void mark(final int readAheadLimit) throws UncheckedIOException {
    136        Uncheck.accept(super::mark, readAheadLimit);
    137    }
    138
    139    /**
    140     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    141     */
    142    @Override
    143    public int read() throws UncheckedIOException {
    144        return Uncheck.getAsInt(super::read);
    145    }
    146
    147    /**
    148     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    149     */
    150    @Override
    151    public int read(final char[] cbuf) throws UncheckedIOException {
    152        return Uncheck.apply(super::read, cbuf);
    153    }
    154
    155    /**
    156     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    157     */
    158    @Override
    159    public int read(final char[] cbuf, final int off, final int len) throws UncheckedIOException {
    160        return Uncheck.apply(super::read, cbuf, off, len);
    161    }
    162
    163    /**
    164     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    165     */
    166    @Override
    167    public int read(final CharBuffer target) throws UncheckedIOException {
    168        return Uncheck.apply(super::read, target);
    169    }
    170
    171    /**
    172     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    173     */
    174    @Override
    175    public boolean ready() throws UncheckedIOException {
    176        return Uncheck.getAsBoolean(super::ready);
    177    }
    178
    179    /**
    180     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    181     */
    182    @Override
    183    public void reset() throws UncheckedIOException {
    184        Uncheck.run(super::reset);
    185    }
    186
    187    /**
    188     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
    189     */
    190    @Override
    191    public long skip(final long n) throws UncheckedIOException {
    192        return Uncheck.apply(super::skip, n);
    193    }
    194
    195}