View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.io.input;
19  
20  import java.io.FilterReader;
21  import java.io.IOException;
22  import java.io.Reader;
23  import java.io.UncheckedIOException;
24  import java.nio.CharBuffer;
25  
26  import org.apache.commons.io.build.AbstractStreamBuilder;
27  import org.apache.commons.io.function.Uncheck;
28  
29  /**
30   * A {@link FilterReader} that throws {@link UncheckedIOException} instead of {@link IOException}.
31   * <p>
32   * To build an instance, use {@link Builder}.
33   * </p>
34   *
35   * @see Builder
36   * @see FilterReader
37   * @see IOException
38   * @see UncheckedIOException
39   * @since 2.12.0
40   */
41  public final class UncheckedFilterReader extends FilterReader {
42  
43      // @formatter:off
44      /**
45       * Builds a new {@link UncheckedFilterReader}.
46       *
47       * <p>
48       * Using File IO:
49       * </p>
50       * <pre>{@code
51       * UncheckedFilterReader s = UncheckedFilterReader.builder()
52       *   .setFile(file)
53       *   .get();}
54       * </pre>
55       * <p>
56       * Using NIO Path:
57       * </p>
58       * <pre>{@code
59       * UncheckedFilterReader s = UncheckedFilterReader.builder()
60       *   .setPath(path)
61       *   .get();}
62       * </pre>
63       *
64       * @see #get()
65       */
66      // @formatter:on
67      public static class Builder extends AbstractStreamBuilder<UncheckedFilterReader, Builder> {
68  
69          /**
70           * Builds a new {@link UncheckedFilterReader}.
71           * <p>
72           * You must set input that supports {@link #getReader()} on this builder, otherwise, this method throws an exception.
73           * </p>
74           * <p>
75           * This builder use the following aspects:
76           * </p>
77           * <ul>
78           * <li>{@link #getReader()}</li>
79           * </ul>
80           *
81           * @return a new instance.
82           * @throws UnsupportedOperationException if the origin cannot provide a Reader.
83           * @throws IllegalStateException if the {@code origin} is {@code null}.
84           * @see #getReader()
85           */
86          @Override
87          public UncheckedFilterReader get() {
88              // This an unchecked class, so this method is as well.
89              return Uncheck.get(() -> new UncheckedFilterReader(getReader()));
90          }
91  
92      }
93  
94      /**
95       * Constructs a new {@link Builder}.
96       *
97       * @return a new {@link Builder}.
98       */
99      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 }