UncheckedFilterInputStream.java

  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. package org.apache.commons.io.input;

  18. import java.io.BufferedReader;
  19. import java.io.FilterInputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.UncheckedIOException;

  23. import org.apache.commons.io.build.AbstractStreamBuilder;
  24. import org.apache.commons.io.function.Uncheck;

  25. /**
  26.  * A {@link BufferedReader} that throws {@link UncheckedIOException} instead of {@link IOException}.
  27.  * <p>
  28.  * To build an instance, use {@link Builder}.
  29.  * </p>
  30.  *
  31.  * @see Builder
  32.  * @see BufferedReader
  33.  * @see IOException
  34.  * @see UncheckedIOException
  35.  * @since 2.12.0
  36.  */
  37. public final class UncheckedFilterInputStream extends FilterInputStream {

  38.     // @formatter:off
  39.     /**
  40.      * Builds a new {@link UncheckedFilterInputStream}.
  41.      *
  42.      * <p>
  43.      * Using File IO:
  44.      * </p>
  45.      * <pre>{@code
  46.      * UncheckedFilterInputStream s = UncheckedFilterInputStream.builder()
  47.      *   .setFile(file)
  48.      *   .get();}
  49.      * </pre>
  50.      * <p>
  51.      * Using NIO Path:
  52.      * </p>
  53.      * <pre>{@code
  54.      * UncheckedFilterInputStream s = UncheckedFilterInputStream.builder()
  55.      *   .setPath(path)
  56.      *   .get();}
  57.      * </pre>
  58.      *
  59.      * @see #get()
  60.      */
  61.     // @formatter:on
  62.     public static class Builder extends AbstractStreamBuilder<UncheckedFilterInputStream, Builder> {

  63.         /**
  64.          * Constructs a new builder of {@link UncheckedFilterInputStream}.
  65.          */
  66.         public Builder() {
  67.             // empty
  68.         }

  69.         /**
  70.          * Builds a new {@link UncheckedFilterInputStream}.
  71.          * <p>
  72.          * You must set an aspect that supports {@link #getInputStream()} on this builder, otherwise, this method throws an exception.
  73.          * </p>
  74.          * <p>
  75.          * This builder uses the following aspects:
  76.          * </p>
  77.          * <ul>
  78.          * <li>{@link #getInputStream()} gets the target aspect.</li>
  79.          * </ul>
  80.          *
  81.          * @return a new instance.
  82.          * @throws UnsupportedOperationException if the origin cannot provide an {@link #getInputStream()}.
  83.          * @see #getInputStream()
  84.          * @see #getUnchecked()
  85.          */
  86.         @SuppressWarnings("resource")
  87.         @Override
  88.         public UncheckedFilterInputStream get() {
  89.             // This an unchecked class, so this method is as well.
  90.             return Uncheck.get(() -> new UncheckedFilterInputStream(getInputStream()));
  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.      * Constructs a {@link UncheckedFilterInputStream}.
  103.      *
  104.      * @param inputStream the underlying input stream, or {@code null} if this instance is to be created without an
  105.      *        underlying stream.
  106.      */
  107.     private UncheckedFilterInputStream(final InputStream inputStream) {
  108.         super(inputStream);
  109.     }

  110.     /**
  111.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  112.      */
  113.     @Override
  114.     public int available() throws UncheckedIOException {
  115.         return Uncheck.getAsInt(super::available);
  116.     }

  117.     /**
  118.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  119.      */
  120.     @Override
  121.     public void close() throws UncheckedIOException {
  122.         Uncheck.run(super::close);
  123.     }

  124.     /**
  125.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  126.      */
  127.     @Override
  128.     public int read() throws UncheckedIOException {
  129.         return Uncheck.getAsInt(super::read);
  130.     }

  131.     /**
  132.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  133.      */
  134.     @Override
  135.     public int read(final byte[] b) throws UncheckedIOException {
  136.         return Uncheck.apply(super::read, b);
  137.     }

  138.     /**
  139.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  140.      */
  141.     @Override
  142.     public int read(final byte[] b, final int off, final int len) throws UncheckedIOException {
  143.         return Uncheck.apply(super::read, b, off, len);
  144.     }

  145.     /**
  146.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  147.      */
  148.     @Override
  149.     public synchronized void reset() throws UncheckedIOException {
  150.         Uncheck.run(super::reset);
  151.     }

  152.     /**
  153.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  154.      */
  155.     @Override
  156.     public long skip(final long n) throws UncheckedIOException {
  157.         return Uncheck.apply(super::skip, n);
  158.     }

  159. }