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 *
032 * @see FilterReader
033 * @see IOException
034 * @see UncheckedIOException
035 * @since 2.12.0
036 */
037public final class UncheckedFilterReader extends FilterReader {
038
039    /**
040     * Builds a new {@link UncheckedFilterReader} instance.
041     * <p>
042     * Using File IO:
043     * </p>
044     * <pre>{@code
045     * UncheckedFilterReader s = UncheckedFilterReader.builder()
046     *   .setFile(file)
047     *   .get()}
048     * </pre>
049     * <p>
050     * Using NIO Path:
051     * </p>
052     * <pre>{@code
053     * UncheckedFilterReader s = UncheckedFilterReader.builder()
054     *   .setPath(path)
055     *   .get()}
056     * </pre>
057     */
058    public static class Builder extends AbstractStreamBuilder<UncheckedFilterReader, Builder> {
059
060        /**
061         * Constructs a new instance.
062         *
063         * @throws UnsupportedOperationException if the origin cannot be converted to a Reader.
064         */
065        @Override
066        public UncheckedFilterReader get() {
067            // This an unchecked class, so this method is as well.
068            return Uncheck.get(() -> new UncheckedFilterReader(getOrigin().getReader(getCharset())));
069        }
070
071    }
072
073    /**
074     * Constructs a new {@link Builder}.
075     *
076     * @return a new {@link Builder}.
077     */
078    public static Builder builder() {
079        return new Builder();
080    }
081
082    /**
083     * Creates a new filtered reader.
084     *
085     * @param reader a Reader object providing the underlying stream.
086     * @throws NullPointerException if {@code reader} is {@code null}.
087     */
088    private UncheckedFilterReader(final Reader reader) {
089        super(reader);
090    }
091
092    /**
093     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
094     */
095    @Override
096    public void close() throws UncheckedIOException {
097        Uncheck.run(super::close);
098    }
099
100    /**
101     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
102     */
103    @Override
104    public void mark(final int readAheadLimit) throws UncheckedIOException {
105        Uncheck.accept(super::mark, readAheadLimit);
106    }
107
108    /**
109     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
110     */
111    @Override
112    public int read() throws UncheckedIOException {
113        return Uncheck.get(super::read);
114    }
115
116    /**
117     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
118     */
119    @Override
120    public int read(final char[] cbuf) throws UncheckedIOException {
121        return Uncheck.apply(super::read, cbuf);
122    }
123
124    /**
125     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
126     */
127    @Override
128    public int read(final char[] cbuf, final int off, final int len) throws UncheckedIOException {
129        return Uncheck.apply(super::read, cbuf, off, len);
130    }
131
132    /**
133     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
134     */
135    @Override
136    public int read(final CharBuffer target) throws UncheckedIOException {
137        return Uncheck.apply(super::read, target);
138    }
139
140    /**
141     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
142     */
143    @Override
144    public boolean ready() throws UncheckedIOException {
145        return Uncheck.get(super::ready);
146    }
147
148    /**
149     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
150     */
151    @Override
152    public void reset() throws UncheckedIOException {
153        Uncheck.run(super::reset);
154    }
155
156    /**
157     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
158     */
159    @Override
160    public long skip(final long n) throws UncheckedIOException {
161        return Uncheck.apply(super::skip, n);
162    }
163
164}