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.output; 019 020import java.io.FilterWriter; 021import java.io.IOException; 022import java.io.UncheckedIOException; 023import java.io.Writer; 024 025import org.apache.commons.io.build.AbstractStreamBuilder; 026import org.apache.commons.io.function.Uncheck; 027 028/** 029 * A {@link FilterWriter} that throws {@link UncheckedIOException} instead of {@link IOException}. 030 * <p> 031 * To build an instance, use {@link Builder}. 032 * </p> 033 * 034 * @see Builder 035 * @see FilterWriter 036 * @see IOException 037 * @see UncheckedIOException 038 * @since 2.12.0 039 */ 040public final class UncheckedFilterWriter extends FilterWriter { 041 042 // @formatter:off 043 /** 044 * Builds a new {@link UncheckedFilterWriter}. 045 * 046 * <p> 047 * Using File IO: 048 * </p> 049 * <pre>{@code 050 * UncheckedFilterWriter s = UncheckedFilterWriter.builder() 051 * .setFile(file) 052 * .get();} 053 * </pre> 054 * <p> 055 * Using NIO Path: 056 * </p> 057 * <pre>{@code 058 * UncheckedFilterWriter s = UncheckedFilterWriter.builder() 059 * .setPath(path) 060 * .get();} 061 * </pre> 062 * 063 * @see #get() 064 */ 065 // @formatter:on 066 public static class Builder extends AbstractStreamBuilder<UncheckedFilterWriter, Builder> { 067 068 /** 069 * Builds a new {@link UncheckedFilterWriter}. 070 * <p> 071 * You must set input that supports {@link #getWriter()} on this builder, otherwise, this method throws an exception. 072 * </p> 073 * <p> 074 * This builder use the following aspects: 075 * </p> 076 * <ul> 077 * <li>{@link #getWriter()}</li> 078 * </ul> 079 * 080 * @return a new instance. 081 * @throws UnsupportedOperationException if the origin cannot provide a Writer. 082 * @see #getWriter() 083 */ 084 @SuppressWarnings("resource") 085 @Override 086 public UncheckedFilterWriter get() throws IOException { 087 return new UncheckedFilterWriter(getWriter()); 088 } 089 090 } 091 092 /** 093 * Constructs a new {@link Builder}. 094 * 095 * @return a new {@link Builder}. 096 */ 097 public static Builder builder() { 098 return new Builder(); 099 } 100 101 /** 102 * Constructs a new filtered writer. 103 * 104 * @param writer a Writer object providing the underlying stream. 105 * @throws NullPointerException if {@code writer} is {@code null}. 106 */ 107 private UncheckedFilterWriter(final Writer writer) { 108 super(writer); 109 } 110 111 /** 112 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 113 */ 114 @Override 115 public Writer append(final char c) throws UncheckedIOException { 116 return Uncheck.apply(super::append, c); 117 } 118 119 /** 120 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 121 */ 122 @Override 123 public Writer append(final CharSequence csq) throws UncheckedIOException { 124 return Uncheck.apply(super::append, csq); 125 } 126 127 /** 128 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 129 */ 130 @Override 131 public Writer append(final CharSequence csq, final int start, final int end) throws UncheckedIOException { 132 return Uncheck.apply(super::append, csq, start, end); 133 } 134 135 /** 136 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 137 */ 138 @Override 139 public void close() throws UncheckedIOException { 140 Uncheck.run(super::close); 141 } 142 143 /** 144 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 145 */ 146 @Override 147 public void flush() throws UncheckedIOException { 148 Uncheck.run(super::flush); 149 } 150 151 /** 152 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 153 */ 154 @Override 155 public void write(final char[] cbuf) throws UncheckedIOException { 156 Uncheck.accept(super::write, cbuf); 157 } 158 159 /** 160 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 161 */ 162 @Override 163 public void write(final char[] cbuf, final int off, final int len) throws UncheckedIOException { 164 Uncheck.accept(super::write, cbuf, off, len); 165 } 166 167 /** 168 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 169 */ 170 @Override 171 public void write(final int c) throws UncheckedIOException { 172 Uncheck.accept(super::write, c); 173 } 174 175 /** 176 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 177 */ 178 @Override 179 public void write(final String str) throws UncheckedIOException { 180 Uncheck.accept(super::write, str); 181 } 182 183 /** 184 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 185 */ 186 @Override 187 public void write(final String str, final int off, final int len) throws UncheckedIOException { 188 Uncheck.accept(super::write, str, off, len); 189 } 190 191}