1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.io.output;
19
20 import java.io.FilterWriter;
21 import java.io.IOException;
22 import java.io.UncheckedIOException;
23 import java.io.Writer;
24
25 import org.apache.commons.io.build.AbstractStreamBuilder;
26 import org.apache.commons.io.function.Uncheck;
27
28 /**
29 * A {@link FilterWriter} that throws {@link UncheckedIOException} instead of {@link IOException}.
30 * <p>
31 * To build an instance, use {@link Builder}.
32 * </p>
33 *
34 * @see Builder
35 * @see FilterWriter
36 * @see IOException
37 * @see UncheckedIOException
38 * @since 2.12.0
39 */
40 public final class UncheckedFilterWriter extends FilterWriter {
41
42 // @formatter:off
43 /**
44 * Builds a new {@link UncheckedFilterWriter}.
45 *
46 * <p>
47 * Using File IO:
48 * </p>
49 * <pre>{@code
50 * UncheckedFilterWriter s = UncheckedFilterWriter.builder()
51 * .setFile(file)
52 * .get();}
53 * </pre>
54 * <p>
55 * Using NIO Path:
56 * </p>
57 * <pre>{@code
58 * UncheckedFilterWriter s = UncheckedFilterWriter.builder()
59 * .setPath(path)
60 * .get();}
61 * </pre>
62 *
63 * @see #get()
64 */
65 // @formatter:on
66 public static class Builder extends AbstractStreamBuilder<UncheckedFilterWriter, Builder> {
67
68 /**
69 * Constructs a builder of {@link UncheckedFilterWriter}.
70 */
71 public Builder() {
72 // empty
73 }
74
75 /**
76 * Builds a new {@link UncheckedFilterWriter}.
77 * <p>
78 * You must set an aspect that supports {@link #getWriter()} on this builder, otherwise, this method throws an exception.
79 * </p>
80 * <p>
81 * This builder uses the following aspects:
82 * </p>
83 * <ul>
84 * <li>{@link #getWriter()}</li>
85 * </ul>
86 *
87 * @return a new instance.
88 * @throws UnsupportedOperationException if the origin cannot provide a {@link Writer}.
89 * @throws IOException if an I/O error occurs converting to an {@link Writer} using {@link #getWriter()}.
90 * @see #getWriter()
91 * @see #getUnchecked()
92 */
93 @Override
94 public UncheckedFilterWriter get() throws IOException {
95 return new UncheckedFilterWriter(this);
96 }
97
98 }
99
100 /**
101 * Constructs a new {@link Builder}.
102 *
103 * @return a new {@link Builder}.
104 */
105 public static Builder builder() {
106 return new Builder();
107 }
108
109 /**
110 * Constructs a new filtered writer.
111 *
112 * @param builder a Writer object providing the underlying stream.
113 * @throws NullPointerException if {@code builder} the its {@code Writer} is {@code null}.
114 * @throws IOException if an I/O error occurs converting to an {@link Writer} using {@link #getWriter()}.
115 */
116 @SuppressWarnings("resource") // Caller closes.
117 private UncheckedFilterWriter(final Builder builder) throws IOException {
118 super(builder.getWriter());
119 }
120
121 /**
122 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
123 */
124 @Override
125 public Writer append(final char c) throws UncheckedIOException {
126 return Uncheck.apply(super::append, c);
127 }
128
129 /**
130 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
131 */
132 @Override
133 public Writer append(final CharSequence csq) throws UncheckedIOException {
134 return Uncheck.apply(super::append, csq);
135 }
136
137 /**
138 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
139 */
140 @Override
141 public Writer append(final CharSequence csq, final int start, final int end) throws UncheckedIOException {
142 return Uncheck.apply(super::append, csq, start, end);
143 }
144
145 /**
146 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
147 */
148 @Override
149 public void close() throws UncheckedIOException {
150 Uncheck.run(super::close);
151 }
152
153 /**
154 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
155 */
156 @Override
157 public void flush() throws UncheckedIOException {
158 Uncheck.run(super::flush);
159 }
160
161 /**
162 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
163 */
164 @Override
165 public void write(final char[] cbuf) throws UncheckedIOException {
166 Uncheck.accept(super::write, cbuf);
167 }
168
169 /**
170 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
171 */
172 @Override
173 public void write(final char[] cbuf, final int off, final int len) throws UncheckedIOException {
174 Uncheck.accept(super::write, cbuf, off, len);
175 }
176
177 /**
178 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
179 */
180 @Override
181 public void write(final int c) throws UncheckedIOException {
182 Uncheck.accept(super::write, c);
183 }
184
185 /**
186 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
187 */
188 @Override
189 public void write(final String str) throws UncheckedIOException {
190 Uncheck.accept(super::write, str);
191 }
192
193 /**
194 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
195 */
196 @Override
197 public void write(final String str, final int off, final int len) throws UncheckedIOException {
198 Uncheck.accept(super::write, str, off, len);
199 }
200
201 }