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.BufferedReader;
21  import java.io.FilterInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.UncheckedIOException;
25  
26  import org.apache.commons.io.build.AbstractStreamBuilder;
27  import org.apache.commons.io.function.Uncheck;
28  
29  /**
30   * A {@link BufferedReader} 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 BufferedReader
37   * @see IOException
38   * @see UncheckedIOException
39   * @since 2.12.0
40   */
41  public final class UncheckedFilterInputStream extends FilterInputStream {
42  
43      // @formatter:off
44      /**
45       * Builds a new {@link UncheckedFilterInputStream}.
46       *
47       * <p>
48       * Using File IO:
49       * </p>
50       * <pre>{@code
51       * UncheckedFilterInputStream s = UncheckedFilterInputStream.builder()
52       *   .setFile(file)
53       *   .get();}
54       * </pre>
55       * <p>
56       * Using NIO Path:
57       * </p>
58       * <pre>{@code
59       * UncheckedFilterInputStream s = UncheckedFilterInputStream.builder()
60       *   .setPath(path)
61       *   .get();}
62       * </pre>
63       *
64       * @see #get()
65       */
66      // @formatter:on
67      public static class Builder extends AbstractStreamBuilder<UncheckedFilterInputStream, Builder> {
68  
69          /**
70           * Builds a new {@link UncheckedFilterInputStream}.
71           * <p>
72           * You must set input that supports {@link #getInputStream()} 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 #getInputStream()}</li>
79           * </ul>
80           *
81           * @return a new instance.
82           * @throws UnsupportedOperationException if the origin cannot provide an InputStream.
83           * @see #getInputStream()
84           */
85          @Override
86          public UncheckedFilterInputStream get() {
87              // This an unchecked class, so this method is as well.
88              return Uncheck.get(() -> new UncheckedFilterInputStream(getInputStream()));
89          }
90  
91      }
92  
93      /**
94       * Constructs a new {@link Builder}.
95       *
96       * @return a new {@link Builder}.
97       */
98      public static Builder builder() {
99          return new Builder();
100     }
101 
102     /**
103      * Constructs a {@link UncheckedFilterInputStream}.
104      *
105      * @param inputStream the underlying input stream, or {@code null} if this instance is to be created without an
106      *        underlying stream.
107      */
108     private UncheckedFilterInputStream(final InputStream inputStream) {
109         super(inputStream);
110     }
111 
112     /**
113      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
114      */
115     @Override
116     public int available() throws UncheckedIOException {
117         return Uncheck.get(super::available);
118     }
119 
120     /**
121      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
122      */
123     @Override
124     public void close() throws UncheckedIOException {
125         Uncheck.run(super::close);
126     }
127 
128     /**
129      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
130      */
131     @Override
132     public int read() throws UncheckedIOException {
133         return Uncheck.get(super::read);
134     }
135 
136     /**
137      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
138      */
139     @Override
140     public int read(final byte[] b) throws UncheckedIOException {
141         return Uncheck.apply(super::read, b);
142     }
143 
144     /**
145      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
146      */
147     @Override
148     public int read(final byte[] b, final int off, final int len) throws UncheckedIOException {
149         return Uncheck.apply(super::read, b, off, len);
150     }
151 
152     /**
153      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
154      */
155     @Override
156     public synchronized void reset() throws UncheckedIOException {
157         Uncheck.run(super::reset);
158     }
159 
160     /**
161      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
162      */
163     @Override
164     public long skip(final long n) throws UncheckedIOException {
165         return Uncheck.apply(super::skip, n);
166     }
167 
168 }