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.IOException;
21 import java.io.OutputStream;
22
23 import org.apache.commons.io.IOUtils;
24
25 /**
26 * Throws an IOException on all attempts to write to the stream.
27 * <p>
28 * Typically uses of this class include testing for corner cases in methods that accept an output stream and acting as a sentinel value instead of a
29 * {@code null} output stream.
30 * </p>
31 *
32 * @since 1.4
33 */
34 public class ClosedOutputStream extends OutputStream {
35
36 /**
37 * The singleton instance.
38 *
39 * @since 2.12.0
40 */
41 public static final ClosedOutputStream INSTANCE = new ClosedOutputStream();
42
43 /**
44 * The singleton instance.
45 *
46 * @deprecated Use {@link #INSTANCE}.
47 */
48 @Deprecated
49 public static final ClosedOutputStream CLOSED_OUTPUT_STREAM = INSTANCE;
50
51 /**
52 * Construct a new instance.
53 */
54 public ClosedOutputStream() {
55 // empty
56 }
57
58 /**
59 * Throws an {@link IOException} to indicate that the stream is closed.
60 *
61 * @throws IOException always thrown
62 */
63 @Override
64 public void flush() throws IOException {
65 throw new IOException("flush() failed: stream is closed");
66 }
67
68 /**
69 * Throws an {@link IOException} to indicate that the stream is closed.
70 *
71 * @param b Byte array, never {@code null}.
72 * @param off The start offset in the byte array.
73 * @param len The number of bytes to write.
74 * @throws NullPointerException if the byte array is {@code null}.
75 * @throws IndexOutOfBoundsException if {@code off} or {@code len} are negative, or if {@code off + len} is greater than {@code b.length}.
76 * @throws IOException always thrown.
77 */
78 @Override
79 public void write(final byte b[], final int off, final int len) throws IOException {
80 IOUtils.checkFromIndexSize(b, off, len);
81 throw new IOException("write(byte[], int, int) failed: stream is closed");
82 }
83
84 /**
85 * Throws an {@link IOException} to indicate that the stream is closed.
86 *
87 * @param b ignored.
88 * @throws IOException always thrown.
89 */
90 @Override
91 public void write(final int b) throws IOException {
92 throw new IOException("write(int) failed: stream is closed");
93 }
94 }