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.FilterInputStream; 022import java.io.IOException; 023import java.io.InputStream; 024import java.io.UncheckedIOException; 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 UncheckedFilterInputStream extends FilterInputStream { 042 043 // @formatter:off 044 /** 045 * Builds a new {@link UncheckedFilterInputStream}. 046 * 047 * <p> 048 * Using File IO: 049 * </p> 050 * <pre>{@code 051 * UncheckedFilterInputStream s = UncheckedFilterInputStream.builder() 052 * .setFile(file) 053 * .get();} 054 * </pre> 055 * <p> 056 * Using NIO Path: 057 * </p> 058 * <pre>{@code 059 * UncheckedFilterInputStream s = UncheckedFilterInputStream.builder() 060 * .setPath(path) 061 * .get();} 062 * </pre> 063 * 064 * @see #get() 065 */ 066 // @formatter:on 067 public static class Builder extends AbstractStreamBuilder<UncheckedFilterInputStream, Builder> { 068 069 /** 070 * Builds a new {@link UncheckedFilterInputStream}. 071 * <p> 072 * You must set input that supports {@link #getInputStream()} on this builder, otherwise, this method throws an exception. 073 * </p> 074 * <p> 075 * This builder use the following aspects: 076 * </p> 077 * <ul> 078 * <li>{@link #getInputStream()}</li> 079 * </ul> 080 * 081 * @return a new instance. 082 * @throws UnsupportedOperationException if the origin cannot provide an InputStream. 083 * @see #getInputStream() 084 */ 085 @Override 086 public UncheckedFilterInputStream get() { 087 // This an unchecked class, so this method is as well. 088 return Uncheck.get(() -> new UncheckedFilterInputStream(getInputStream())); 089 } 090 091 } 092 093 /** 094 * Constructs a new {@link Builder}. 095 * 096 * @return a new {@link Builder}. 097 */ 098 public static Builder builder() { 099 return new Builder(); 100 } 101 102 /** 103 * Constructs a {@link UncheckedFilterInputStream}. 104 * 105 * @param inputStream the underlying input stream, or {@code null} if this instance is to be created without an 106 * underlying stream. 107 */ 108 private UncheckedFilterInputStream(final InputStream inputStream) { 109 super(inputStream); 110 } 111 112 /** 113 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 114 */ 115 @Override 116 public int available() throws UncheckedIOException { 117 return Uncheck.get(super::available); 118 } 119 120 /** 121 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 122 */ 123 @Override 124 public void close() throws UncheckedIOException { 125 Uncheck.run(super::close); 126 } 127 128 /** 129 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 130 */ 131 @Override 132 public int read() throws UncheckedIOException { 133 return Uncheck.get(super::read); 134 } 135 136 /** 137 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 138 */ 139 @Override 140 public int read(final byte[] b) throws UncheckedIOException { 141 return Uncheck.apply(super::read, b); 142 } 143 144 /** 145 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 146 */ 147 @Override 148 public int read(final byte[] b, final int off, final int len) throws UncheckedIOException { 149 return Uncheck.apply(super::read, b, off, len); 150 } 151 152 /** 153 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 154 */ 155 @Override 156 public synchronized void reset() throws UncheckedIOException { 157 Uncheck.run(super::reset); 158 } 159 160 /** 161 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 162 */ 163 @Override 164 public long skip(final long n) throws UncheckedIOException { 165 return Uncheck.apply(super::skip, n); 166 } 167 168}