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 * 031 * @see FilterWriter 032 * @see IOException 033 * @see UncheckedIOException 034 * @since 2.12.0 035 */ 036public final class UncheckedFilterWriter extends FilterWriter { 037 038 /** 039 * Builds a new {@link UncheckedFilterWriter} instance. 040 * <p> 041 * Using File IO: 042 * </p> 043 * <pre>{@code 044 * UncheckedFilterWriter s = UncheckedFilterWriter.builder() 045 * .setFile(file) 046 * .get()} 047 * </pre> 048 * <p> 049 * Using NIO Path: 050 * </p> 051 * <pre>{@code 052 * UncheckedFilterWriter s = UncheckedFilterWriter.builder() 053 * .setPath(path) 054 * .get()} 055 * </pre> 056 */ 057 public static class Builder extends AbstractStreamBuilder<UncheckedFilterWriter, Builder> { 058 059 /** 060 * Constructs a new instance. 061 * 062 * @throws UnsupportedOperationException if the origin cannot be converted to a Writer. 063 */ 064 @SuppressWarnings("resource") 065 @Override 066 public UncheckedFilterWriter get() throws IOException { 067 return new UncheckedFilterWriter(getOrigin().getWriter(getCharset())); 068 } 069 070 } 071 072 /** 073 * Constructs a new {@link Builder}. 074 * 075 * @return a new {@link Builder}. 076 */ 077 public static Builder builder() { 078 return new Builder(); 079 } 080 081 /** 082 * Creates a new filtered writer. 083 * 084 * @param writer a Writer object providing the underlying stream. 085 * @throws NullPointerException if {@code writer} is {@code null}. 086 */ 087 private UncheckedFilterWriter(final Writer writer) { 088 super(writer); 089 } 090 091 /** 092 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 093 */ 094 @Override 095 public Writer append(final char c) throws UncheckedIOException { 096 return Uncheck.apply(super::append, c); 097 } 098 099 /** 100 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 101 */ 102 @Override 103 public Writer append(final CharSequence csq) throws UncheckedIOException { 104 return Uncheck.apply(super::append, csq); 105 } 106 107 /** 108 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 109 */ 110 @Override 111 public Writer append(final CharSequence csq, final int start, final int end) throws UncheckedIOException { 112 return Uncheck.apply(super::append, csq, start, end); 113 } 114 115 /** 116 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 117 */ 118 @Override 119 public void close() throws UncheckedIOException { 120 Uncheck.run(super::close); 121 } 122 123 /** 124 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 125 */ 126 @Override 127 public void flush() throws UncheckedIOException { 128 Uncheck.run(super::flush); 129 } 130 131 /** 132 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 133 */ 134 @Override 135 public void write(final char[] cbuf) throws UncheckedIOException { 136 Uncheck.accept(super::write, cbuf); 137 } 138 139 /** 140 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 141 */ 142 @Override 143 public void write(final char[] cbuf, final int off, final int len) throws UncheckedIOException { 144 Uncheck.accept(super::write, cbuf, off, len); 145 } 146 147 /** 148 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 149 */ 150 @Override 151 public void write(final int c) throws UncheckedIOException { 152 Uncheck.accept(super::write, c); 153 } 154 155 /** 156 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 157 */ 158 @Override 159 public void write(final String str) throws UncheckedIOException { 160 Uncheck.accept(super::write, str); 161 } 162 163 /** 164 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}. 165 */ 166 @Override 167 public void write(final String str, final int off, final int len) throws UncheckedIOException { 168 Uncheck.accept(super::write, str, off, len); 169 } 170 171}