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         * 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        @SuppressWarnings("resource")
095        @Override
096        public UncheckedFilterReader get() {
097            // This an unchecked class, so this method is as well.
098            return Uncheck.get(() -> new UncheckedFilterReader(getReader()));
099        }
100
101    }
102
103    /**
104     * Constructs a new {@link Builder}.
105     *
106     * @return a new {@link Builder}.
107     */
108    public static Builder builder() {
109        return new Builder();
110    }
111
112    /**
113     * Constructs a new filtered reader.
114     *
115     * @param reader a Reader object providing the underlying stream.
116     * @throws NullPointerException if {@code reader} is {@code null}.
117     */
118    private UncheckedFilterReader(final Reader reader) {
119        super(reader);
120    }
121
122    /**
123     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
124     */
125    @Override
126    public void close() throws UncheckedIOException {
127        Uncheck.run(super::close);
128    }
129
130    /**
131     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
132     */
133    @Override
134    public void mark(final int readAheadLimit) throws UncheckedIOException {
135        Uncheck.accept(super::mark, readAheadLimit);
136    }
137
138    /**
139     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
140     */
141    @Override
142    public int read() throws UncheckedIOException {
143        return Uncheck.getAsInt(super::read);
144    }
145
146    /**
147     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
148     */
149    @Override
150    public int read(final char[] cbuf) throws UncheckedIOException {
151        return Uncheck.apply(super::read, cbuf);
152    }
153
154    /**
155     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
156     */
157    @Override
158    public int read(final char[] cbuf, final int off, final int len) throws UncheckedIOException {
159        return Uncheck.apply(super::read, cbuf, off, len);
160    }
161
162    /**
163     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
164     */
165    @Override
166    public int read(final CharBuffer target) throws UncheckedIOException {
167        return Uncheck.apply(super::read, target);
168    }
169
170    /**
171     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
172     */
173    @Override
174    public boolean ready() throws UncheckedIOException {
175        return Uncheck.getAsBoolean(super::ready);
176    }
177
178    /**
179     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
180     */
181    @Override
182    public void reset() throws UncheckedIOException {
183        Uncheck.run(super::reset);
184    }
185
186    /**
187     * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
188     */
189    @Override
190    public long skip(final long n) throws UncheckedIOException {
191        return Uncheck.apply(super::skip, n);
192    }
193
194}