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.FilterOutputStream;
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.io.UncheckedIOException;
24  
25  import org.apache.commons.io.build.AbstractStreamBuilder;
26  import org.apache.commons.io.function.Uncheck;
27  
28  /**
29   * A {@link FilterOutputStream} that throws {@link UncheckedIOException} instead of {@link UncheckedIOException}.
30   * <p>
31   * To build an instance, use {@link Builder}.
32   * </p>
33   *
34   * @see Builder
35   * @see FilterOutputStream
36   * @see UncheckedIOException
37   * @since 2.12.0
38   */
39  public final class UncheckedFilterOutputStream extends FilterOutputStream {
40  
41      // @formatter:off
42      /**
43       * Builds a new {@link UncheckedFilterOutputStream}.
44       *
45       * <p>
46       * Using File IO:
47       * </p>
48       * <pre>{@code
49       * UncheckedFilterOutputStream s = UncheckedFilterOutputStream.builder()
50       *   .setFile(file)
51       *   .get();}
52       * </pre>
53       * <p>
54       * Using NIO Path:
55       * </p>
56       * <pre>{@code
57       * UncheckedFilterOutputStream s = UncheckedFilterOutputStream.builder()
58       *   .setPath(path)
59       *   .get();}
60       * </pre>
61       *
62       * @see #get()
63       */
64      // @formatter:on
65      public static class Builder extends AbstractStreamBuilder<UncheckedFilterOutputStream, Builder> {
66  
67          /**
68           * Builds a new {@link UncheckedFilterOutputStream}.
69           * <p>
70           * You must set input that supports {@link #getOutputStream()} on this builder, otherwise, this method throws an exception.
71           * </p>
72           * <p>
73           * This builder use the following aspects:
74           * </p>
75           * <ul>
76           * <li>{@link #getOutputStream()}</li>
77           * </ul>
78           *
79           * @return a new instance.
80           * @throws IllegalStateException         if the {@code origin} is {@code null}.
81           * @throws UnsupportedOperationException if the origin cannot be converted to an {@link OutputStream}.
82           * @throws IOException                   if an I/O error occurs.
83           * @see #getOutputStream()
84           */
85          @SuppressWarnings("resource")
86          @Override
87          public UncheckedFilterOutputStream get() throws IOException {
88              return new UncheckedFilterOutputStream(getOutputStream());
89          }
90  
91      }
92  
93      /**
94       * Constructs a new {@link Builder}.
95       *
96       * @return a new {@link Builder}.
97       */
98      public static Builder builder() {
99          return new Builder();
100     }
101 
102     /**
103      * Constructs an output stream filter built on top of the specified underlying output stream.
104      *
105      * @param outputStream the underlying output stream, or {@code null} if this instance is to be created without an
106      *        underlying stream.
107      */
108     private UncheckedFilterOutputStream(final OutputStream outputStream) {
109         super(outputStream);
110     }
111 
112     /**
113      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
114      */
115     @Override
116     public void close() throws UncheckedIOException {
117         Uncheck.run(super::close);
118     }
119 
120     /**
121      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
122      */
123     @Override
124     public void flush() throws UncheckedIOException {
125         Uncheck.run(super::flush);
126     }
127 
128     /**
129      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
130      */
131     @Override
132     public void write(final byte[] b) throws UncheckedIOException {
133         Uncheck.accept(super::write, b);
134     }
135 
136     /**
137      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
138      */
139     @Override
140     public void write(final byte[] b, final int off, final int len) throws UncheckedIOException {
141         Uncheck.accept(super::write, b, off, len);
142     }
143 
144     /**
145      * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
146      */
147     @Override
148     public void write(final int b) throws UncheckedIOException {
149         Uncheck.accept(super::write, b);
150     }
151 
152 }