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 * Builds a new {@link UncheckedBufferedReader}. 075 * 076 * <p> 077 * You must set input that supports {@link #getReader()} on this builder, otherwise, this method throws an exception. 078 * </p> 079 * <p> 080 * This builder use the following aspects: 081 * </p> 082 * <ul> 083 * <li>{@link #getReader()}</li> 084 * <li>{@link #getBufferSize()}</li> 085 * </ul> 086 * 087 * @return a new instance. 088 * @throws UnsupportedOperationException if the origin cannot provide a Reader. 089 * @throws IllegalStateException if the {@code origin} is {@code null}. 090 * @see #getReader() 091 * @see #getBufferSize() 092 */ 093 @Override 094 public UncheckedBufferedReader get() { 095 // This an unchecked class, so this method is as well. 096 return Uncheck.get(() -> new UncheckedBufferedReader(getReader(), getBufferSize())); 097 } 098 099 } 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 /** 111 * Constructs a buffering character-input stream that uses an input buffer of the specified size. 112 * 113 * @param reader A Reader 114 * @param bufferSize Input-buffer size 115 * 116 * @throws IllegalArgumentException If {@code bufferSize <= 0} 117 */ 118 private UncheckedBufferedReader(final Reader reader, final int bufferSize) { 119 super(reader, bufferSize); 120 } 121 122 /** 123 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 124 */ 125 @Override 126 public void close() throws UncheckedIOException { 127 Uncheck.run(super::close); 128 } 129 130 /** 131 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 132 */ 133 @Override 134 public void mark(final int readAheadLimit) throws UncheckedIOException { 135 Uncheck.accept(super::mark, readAheadLimit); 136 } 137 138 /** 139 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 140 */ 141 @Override 142 public int read() throws UncheckedIOException { 143 return Uncheck.get(super::read); 144 } 145 146 /** 147 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 148 */ 149 @Override 150 public int read(final char[] cbuf) throws UncheckedIOException { 151 return Uncheck.apply(super::read, cbuf); 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, final int off, final int len) throws UncheckedIOException { 159 return Uncheck.apply(super::read, cbuf, off, len); 160 } 161 162 /** 163 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 164 */ 165 @Override 166 public int read(final CharBuffer target) throws UncheckedIOException { 167 return Uncheck.apply(super::read, target); 168 } 169 170 /** 171 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 172 */ 173 @Override 174 public String readLine() throws UncheckedIOException { 175 return Uncheck.get(super::readLine); 176 } 177 178 /** 179 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 180 */ 181 @Override 182 public boolean ready() throws UncheckedIOException { 183 return Uncheck.get(super::ready); 184 } 185 186 /** 187 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 188 */ 189 @Override 190 public void reset() throws UncheckedIOException { 191 Uncheck.run(super::reset); 192 } 193 194 /** 195 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 196 */ 197 @Override 198 public long skip(final long n) throws UncheckedIOException { 199 return Uncheck.apply(super::skip, n); 200 } 201 202}