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 package org.apache.commons.vfs2.util;
18
19 import java.io.BufferedOutputStream;
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import java.util.concurrent.atomic.AtomicBoolean;
23
24 import org.apache.commons.vfs2.FileSystemException;
25
26 /**
27 * An OutputStream that provides buffering and end-of-stream monitoring.
28 */
29 public class MonitorOutputStream extends BufferedOutputStream {
30
31 private final AtomicBoolean closed = new AtomicBoolean();
32
33 /**
34 * Constructs a MonitorOutputStream from the passed OutputStream.
35 *
36 * @param out The output stream to wrap.
37 */
38 public MonitorOutputStream(final OutputStream out) {
39 super(out);
40 }
41
42 /**
43 * Constructs a MonitorOutputStream from the passed OutputStream and with the specified buffer size.
44 *
45 * @param out The output stream to wrap.
46 * @param bufferSize The buffer size to use.
47 * @since 2.4
48 */
49 public MonitorOutputStream(final OutputStream out, final int bufferSize) {
50 super(out, bufferSize);
51 }
52
53 /**
54 * Check if file is still open.
55 * <p>
56 * This is a workaround for an oddity with Java's BufferedOutputStream where you can write to even if the stream has
57 * been closed.
58 * </p>
59 *
60 * @throws FileSystemException if already closed.
61 * @since 2.0
62 */
63 protected void assertOpen() throws FileSystemException {
64 if (isClosed()) {
65 throw new FileSystemException("vfs.provider/closed.error");
66 }
67 }
68
69 /**
70 * Closes this output stream.
71 * <p>
72 * This makes sure the buffers are flushed, close the output stream and it will call {@link #onClose()} and re-throw
73 * last exception from any of the three.
74 * </p>
75 * <p>
76 * This does nothing if the stream is closed already.
77 * </p>
78 *
79 * @throws IOException if an IO error occurs.
80 */
81 @Override
82 public void close() throws IOException {
83 // do not use super.close()
84 // on Java 8 it might throw self suppression, see JDK-8042377
85 // in older Java it silently ignores flush() errors
86 if (closed.getAndSet(true)) {
87 return;
88 }
89
90 IOException exc = null;
91
92 // flush the buffer and out stream
93 try {
94 super.flush();
95 } catch (final IOException ioe) {
96 exc = ioe;
97 }
98
99 // close the out stream without using super.close()
100 try {
101 super.out.close();
102 } catch (final IOException ioe) {
103 exc = ioe;
104 }
105
106 // Notify of end of output
107 try {
108 onClose();
109 } catch (final IOException ioe) {
110 exc = ioe;
111 }
112
113 if (exc != null) {
114 throw exc;
115 }
116 }
117
118 /**
119 * @throws IOException if an error occurs.
120 * @since 2.0
121 */
122 @Override
123 public synchronized void flush() throws IOException {
124 if (isClosed()) {
125 return;
126 }
127 super.flush();
128 }
129
130 private boolean isClosed() {
131 return closed.get();
132 }
133
134 /**
135 * Called after this stream is closed.
136 * <p>
137 * This implementation does nothing.
138 * </p>
139 *
140 * @throws IOException if an error occurs.
141 */
142 // IOException is needed because subclasses may need to throw it
143 protected void onClose() throws IOException {
144 }
145
146 /**
147 * @param b The byte array.
148 * @throws IOException if an error occurs.
149 * @since 2.0
150 */
151 @Override
152 public void write(final byte[] b) throws IOException {
153 assertOpen();
154 super.write(b);
155 }
156
157 /**
158 * @param b The byte array.
159 * @param off The offset into the array.
160 * @param len The number of bytes to write.
161 * @throws IOException if an error occurs.
162 * @since 2.0
163 */
164 @Override
165 public synchronized void write(final byte[] b, final int off, final int len) throws IOException {
166 assertOpen();
167 super.write(b, off, len);
168 }
169
170 /**
171 * @param b The character to write.
172 * @throws IOException if an error occurs.
173 * @since 2.0
174 */
175 @Override
176 public synchronized void write(final int b) throws IOException {
177 assertOpen();
178 super.write(b);
179 }
180 }