View Javadoc
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    *      http://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           * Builds a new {@link UncheckedFilterWriter}.
70           * <p>
71           * You must set input that supports {@link #getWriter()} on this builder, otherwise, this method throws an exception.
72           * </p>
73           * <p>
74           * This builder use the following aspects:
75           * </p>
76           * <ul>
77           * <li>{@link #getWriter()}</li>
78           * </ul>
79           *
80           * @return a new instance.
81           * @throws UnsupportedOperationException if the origin cannot provide a Writer.
82           * @see #getWriter()
83           */
84          @SuppressWarnings("resource")
85          @Override
86          public UncheckedFilterWriter get() throws IOException {
87              return new UncheckedFilterWriter(getWriter());
88          }
89  
90      }
91  
92      /**
93       * Constructs a new {@link Builder}.
94       *
95       * @return a new {@link Builder}.
96       */
97      public static Builder builder() {
98          return new Builder();
99      }
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 }