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.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 * Constructs a new builder of {@link UncheckedFilterOutputStream}.
69 */
70 public Builder() {
71 // empty
72 }
73
74 /**
75 * Builds a new {@link UncheckedFilterOutputStream}.
76 * <p>
77 * You must set an aspect that supports {@link #getOutputStream()} on this builder, otherwise, this method throws an exception.
78 * </p>
79 * <p>
80 * This builder uses the following aspects:
81 * </p>
82 * <ul>
83 * <li>{@link #getOutputStream()}</li>
84 * </ul>
85 *
86 * @return a new instance.
87 * @throws IllegalStateException if the {@code origin} is {@code null}.
88 * @throws UnsupportedOperationException if the origin cannot be converted to an {@link OutputStream}.
89 * @throws IOException if an I/O error occurs converting to an {@link OutputStream} using {@link #getOutputStream()}.
90 * @see #getOutputStream()
91 * @see #getUnchecked()
92 */
93 @Override
94 public UncheckedFilterOutputStream get() throws IOException {
95 return new UncheckedFilterOutputStream(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 an output stream filter built on top of the specified underlying output stream.
111 *
112 * @param builder the buider.
113 * @throws IOException if an I/O error occurs converting to an {@link OutputStream} using {@link #getOutputStream()}.
114 */
115 @SuppressWarnings("resource") // Caller closes.
116 private UncheckedFilterOutputStream(final Builder builder) throws IOException {
117 super(builder.getOutputStream());
118 }
119
120 /**
121 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
122 */
123 @Override
124 public void close() throws UncheckedIOException {
125 Uncheck.run(super::close);
126 }
127
128 /**
129 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
130 */
131 @Override
132 public void flush() throws UncheckedIOException {
133 Uncheck.run(super::flush);
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) throws UncheckedIOException {
141 Uncheck.accept(super::write, b);
142 }
143
144 /**
145 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
146 */
147 @Override
148 public void write(final byte[] b, final int off, final int len) throws UncheckedIOException {
149 Uncheck.accept(super::write, b, off, len);
150 }
151
152 /**
153 * Calls this method's super and rethrow {@link IOException} as {@link UncheckedIOException}.
154 */
155 @Override
156 public void write(final int b) throws UncheckedIOException {
157 Uncheck.accept(super::write, b);
158 }
159
160 }