UncheckedBufferedReader.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.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 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 UncheckedBufferedReader extends BufferedReader {

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

  67.         /**
  68.          * Constructs a new builder of {@link UncheckedBufferedReader}.
  69.          */
  70.         public Builder() {
  71.             // empty
  72.         }

  73.         /**
  74.          * Builds a new {@link UncheckedBufferedReader}.
  75.          *
  76.          * <p>
  77.          * You must set an aspect that supports {@link #getReader()} on this builder, otherwise, this method throws an exception.
  78.          * </p>
  79.          * <p>
  80.          * This builder uses the following aspects:
  81.          * </p>
  82.          * <ul>
  83.          * <li>{@link #getReader()} gets the target aspect.</li>
  84.          * <li>{@link #getBufferSize()}</li>
  85.          * </ul>
  86.          *
  87.          * @return a new instance.
  88.          * @throws UnsupportedOperationException if the origin cannot provide a {@link Reader}.
  89.          * @throws IllegalStateException if the {@code origin} is {@code null}.
  90.          * @see #getReader()
  91.          * @see #getBufferSize()
  92.          * @see #getUnchecked()
  93.          */
  94.         @SuppressWarnings("resource")
  95.         @Override
  96.         public UncheckedBufferedReader get() {
  97.             // This an unchecked class, so this method is as well.
  98.             return Uncheck.get(() -> new UncheckedBufferedReader(getReader(), getBufferSize()));
  99.         }

  100.     }

  101.     /**
  102.      * Constructs a new {@link Builder}.
  103.      *
  104.      * @return a new {@link Builder}.
  105.      */
  106.     public static Builder builder() {
  107.         return new Builder();
  108.     }

  109.     /**
  110.      * Constructs a buffering character-input stream that uses an input buffer of the specified size.
  111.      *
  112.      * @param reader     A Reader
  113.      * @param bufferSize Input-buffer size
  114.      * @throws IllegalArgumentException If {@code bufferSize <= 0}
  115.      */
  116.     private UncheckedBufferedReader(final Reader reader, final int bufferSize) {
  117.         super(reader, bufferSize);
  118.     }

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

  126.     /**
  127.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  128.      */
  129.     @Override
  130.     public void mark(final int readAheadLimit) throws UncheckedIOException {
  131.         Uncheck.accept(super::mark, readAheadLimit);
  132.     }

  133.     /**
  134.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  135.      */
  136.     @Override
  137.     public int read() throws UncheckedIOException {
  138.         return Uncheck.getAsInt(super::read);
  139.     }

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

  147.     /**
  148.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  149.      */
  150.     @Override
  151.     public int read(final char[] cbuf, final int off, final int len) throws UncheckedIOException {
  152.         return Uncheck.apply(super::read, cbuf, off, len);
  153.     }

  154.     /**
  155.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  156.      */
  157.     @Override
  158.     public int read(final CharBuffer target) throws UncheckedIOException {
  159.         return Uncheck.apply(super::read, target);
  160.     }

  161.     /**
  162.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  163.      */
  164.     @Override
  165.     public String readLine() throws UncheckedIOException {
  166.         return Uncheck.get(super::readLine);
  167.     }

  168.     /**
  169.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  170.      */
  171.     @Override
  172.     public boolean ready() throws UncheckedIOException {
  173.         return Uncheck.getAsBoolean(super::ready);
  174.     }

  175.     /**
  176.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  177.      */
  178.     @Override
  179.     public void reset() throws UncheckedIOException {
  180.         Uncheck.run(super::reset);
  181.     }

  182.     /**
  183.      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
  184.      */
  185.     @Override
  186.     public long skip(final long n) throws UncheckedIOException {
  187.         return Uncheck.apply(super::skip, n);
  188.     }

  189. }