UncheckedFilterReader.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.FilterReader;
  19. import java.io.IOException;
  20. import java.io.Reader;
  21. import java.io.UncheckedIOException;
  22. import java.nio.CharBuffer;

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

  25. /**
  26.  * A {@link FilterReader} 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 FilterReader
  33.  * @see IOException
  34.  * @see UncheckedIOException
  35.  * @since 2.12.0
  36.  */
  37. public final class UncheckedFilterReader extends FilterReader {

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

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

  69.         /**
  70.          * Builds a new {@link UncheckedFilterReader}.
  71.          * <p>
  72.          * You must set an aspect that supports {@link #getReader()} 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 #getReader()}</li>
  79.          * </ul>
  80.          *
  81.          * @return a new instance.
  82.          * @throws UnsupportedOperationException if the origin cannot provide a {@link Reader}.
  83.          * @throws IllegalStateException if the {@code origin} is {@code null}.
  84.          * @see #getReader()
  85.          * @see #getUnchecked()
  86.          */
  87.         @SuppressWarnings("resource")
  88.         @Override
  89.         public UncheckedFilterReader get() {
  90.             // This an unchecked class, so this method is as well.
  91.             return Uncheck.get(() -> new UncheckedFilterReader(getReader()));
  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.      * Constructs a new filtered reader.
  104.      *
  105.      * @param reader a Reader object providing the underlying stream.
  106.      * @throws NullPointerException if {@code reader} is {@code null}.
  107.      */
  108.     private UncheckedFilterReader(final Reader reader) {
  109.         super(reader);
  110.     }

  111.     /**
  112.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  113.      */
  114.     @Override
  115.     public void close() throws UncheckedIOException {
  116.         Uncheck.run(super::close);
  117.     }

  118.     /**
  119.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  120.      */
  121.     @Override
  122.     public void mark(final int readAheadLimit) throws UncheckedIOException {
  123.         Uncheck.accept(super::mark, readAheadLimit);
  124.     }

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

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

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

  146.     /**
  147.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  148.      */
  149.     @Override
  150.     public int read(final CharBuffer target) throws UncheckedIOException {
  151.         return Uncheck.apply(super::read, target);
  152.     }

  153.     /**
  154.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  155.      */
  156.     @Override
  157.     public boolean ready() throws UncheckedIOException {
  158.         return Uncheck.getAsBoolean(super::ready);
  159.     }

  160.     /**
  161.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  162.      */
  163.     @Override
  164.     public void reset() throws UncheckedIOException {
  165.         Uncheck.run(super::reset);
  166.     }

  167.     /**
  168.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  169.      */
  170.     @Override
  171.     public long skip(final long n) throws UncheckedIOException {
  172.         return Uncheck.apply(super::skip, n);
  173.     }

  174. }