001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.io.input; 019 020import java.io.BufferedReader; 021import java.io.IOException; 022import java.io.Reader; 023import java.io.UncheckedIOException; 024import java.nio.CharBuffer; 025 026import org.apache.commons.io.build.AbstractStreamBuilder; 027import org.apache.commons.io.function.Uncheck; 028 029/** 030 * A {@link BufferedReader} that throws {@link UncheckedIOException} instead of {@link IOException}. 031 * <p> 032 * To build an instance, use {@link Builder}. 033 * </p> 034 * 035 * @see Builder 036 * @see BufferedReader 037 * @see IOException 038 * @see UncheckedIOException 039 * @since 2.12.0 040 */ 041public final class UncheckedBufferedReader extends BufferedReader { 042 043 // @formatter:off 044 /** 045 * Builds a new {@link UncheckedBufferedReader}. 046 * 047 * <p> 048 * Using File IO: 049 * </p> 050 * <pre>{@code 051 * UncheckedBufferedReader s = UncheckedBufferedReader.builder() 052 * .setFile(file) 053 * .setBufferSize(8192) 054 * .setCharset(Charset.defaultCharset()) 055 * .get();} 056 * </pre> 057 * <p> 058 * Using NIO Path: 059 * </p> 060 * <pre>{@code 061 * UncheckedBufferedReader s = UncheckedBufferedReader.builder() 062 * .setPath(path) 063 * .setBufferSize(8192) 064 * .setCharset(Charset.defaultCharset()) 065 * .get();} 066 * </pre> 067 * 068 * @see #get() 069 */ 070 // @formatter:on 071 public static class Builder extends AbstractStreamBuilder<UncheckedBufferedReader, Builder> { 072 073 /** 074 * Constructs a new builder of {@link UncheckedBufferedReader}. 075 */ 076 public Builder() { 077 // empty 078 } 079 080 /** 081 * Builds a new {@link UncheckedBufferedReader}. 082 * 083 * <p> 084 * You must set an aspect that supports {@link #getReader()} on this builder, otherwise, this method throws an exception. 085 * </p> 086 * <p> 087 * This builder uses the following aspects: 088 * </p> 089 * <ul> 090 * <li>{@link #getReader()} gets the target aspect.</li> 091 * <li>{@link #getBufferSize()}</li> 092 * </ul> 093 * 094 * @return a new instance. 095 * @throws UnsupportedOperationException if the origin cannot provide a {@link Reader}. 096 * @throws IllegalStateException if the {@code origin} is {@code null}. 097 * @see #getReader() 098 * @see #getBufferSize() 099 * @see #getUnchecked() 100 */ 101 @SuppressWarnings("resource") 102 @Override 103 public UncheckedBufferedReader get() { 104 // This an unchecked class, so this method is as well. 105 return Uncheck.get(() -> new UncheckedBufferedReader(getReader(), getBufferSize())); 106 } 107 108 } 109 110 /** 111 * Constructs a new {@link Builder}. 112 * 113 * @return a new {@link Builder}. 114 */ 115 public static Builder builder() { 116 return new Builder(); 117 } 118 119 /** 120 * Constructs a buffering character-input stream that uses an input buffer of the specified size. 121 * 122 * @param reader A Reader 123 * @param bufferSize Input-buffer size 124 * @throws IllegalArgumentException If {@code bufferSize <= 0} 125 */ 126 private UncheckedBufferedReader(final Reader reader, final int bufferSize) { 127 super(reader, bufferSize); 128 } 129 130 /** 131 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 132 */ 133 @Override 134 public void close() throws UncheckedIOException { 135 Uncheck.run(super::close); 136 } 137 138 /** 139 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 140 */ 141 @Override 142 public void mark(final int readAheadLimit) throws UncheckedIOException { 143 Uncheck.accept(super::mark, readAheadLimit); 144 } 145 146 /** 147 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 148 */ 149 @Override 150 public int read() throws UncheckedIOException { 151 return Uncheck.getAsInt(super::read); 152 } 153 154 /** 155 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 156 */ 157 @Override 158 public int read(final char[] cbuf) throws UncheckedIOException { 159 return Uncheck.apply(super::read, cbuf); 160 } 161 162 /** 163 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 164 */ 165 @Override 166 public int read(final char[] cbuf, final int off, final int len) throws UncheckedIOException { 167 return Uncheck.apply(super::read, cbuf, off, len); 168 } 169 170 /** 171 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 172 */ 173 @Override 174 public int read(final CharBuffer target) throws UncheckedIOException { 175 return Uncheck.apply(super::read, target); 176 } 177 178 /** 179 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 180 */ 181 @Override 182 public String readLine() throws UncheckedIOException { 183 return Uncheck.get(super::readLine); 184 } 185 186 /** 187 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 188 */ 189 @Override 190 public boolean ready() throws UncheckedIOException { 191 return Uncheck.getAsBoolean(super::ready); 192 } 193 194 /** 195 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 196 */ 197 @Override 198 public void reset() throws UncheckedIOException { 199 Uncheck.run(super::reset); 200 } 201 202 /** 203 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 204 */ 205 @Override 206 public long skip(final long n) throws UncheckedIOException { 207 return Uncheck.apply(super::skip, n); 208 } 209 210}