001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.util;
018:
019: import org.apache.commons.vfs.FileSystemException;
020:
021: import java.io.BufferedOutputStream;
022: import java.io.IOException;
023: import java.io.OutputStream;
024:
025: /**
026: * An OutputStream that provides buffering and end-of-stream monitoring.
027: *
028: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
029: * @version $Revision: 520065 $ $Date: 2007-03-19 12:41:43 -0700 (Mon, 19 Mar 2007) $
030: */
031: public class MonitorOutputStream extends BufferedOutputStream {
032: private boolean finished;
033:
034: public MonitorOutputStream(final OutputStream out) {
035: super (out);
036: }
037:
038: /**
039: * Closes this output stream.
040: */
041: public void close() throws IOException {
042: if (finished) {
043: return;
044: }
045:
046: // Close the output stream
047: IOException exc = null;
048: try {
049: super .close();
050: } catch (final IOException ioe) {
051: exc = ioe;
052: }
053:
054: // Notify of end of output
055: try {
056: onClose();
057: } catch (final IOException ioe) {
058: exc = ioe;
059: }
060:
061: finished = true;
062:
063: if (exc != null) {
064: throw exc;
065: }
066: }
067:
068: public synchronized void write(int b) throws IOException {
069: assertOpen();
070: super .write(b);
071: }
072:
073: public synchronized void write(byte b[], int off, int len)
074: throws IOException {
075: assertOpen();
076: super .write(b, off, len);
077: }
078:
079: public synchronized void flush() throws IOException {
080: assertOpen();
081: super .flush();
082: }
083:
084: public void write(byte b[]) throws IOException {
085: assertOpen();
086: super .write(b);
087: }
088:
089: /**
090: * check if file is still open. <br />
091: * This is a workaround for an oddidy with javas BufferedOutputStream where you can write to
092: * even if the stream has been closed
093: */
094: protected void assertOpen() throws FileSystemException {
095: if (finished) {
096: throw new FileSystemException("vfs.provider/closed.error");
097: }
098: }
099:
100: /**
101: * Called after this stream is closed. This implementation does nothing.
102: */
103: protected void onClose() throws IOException {
104: }
105: }
|