001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2007 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.lib.web.micro.base;
028:
029: import java.io.IOException;
030: import java.io.OutputStream;
031: import javax.servlet.ServletOutputStream;
032:
033: /**
034: * A marker class for a {@link ServletOutputStream} that contains an embedded
035: * {@link #done} command.
036: */
037: public abstract class AnnotatedOutputStream extends ServletOutputStream {
038:
039: /**
040: * @return the output stream cast as an AnnotatedOutputStream, or trivially
041: * wrapped if it is not an AnnotatedOutputStream
042: */
043: public static AnnotatedOutputStream toAnnotatedOutputStream(
044: final OutputStream out) {
045: if (out instanceof AnnotatedOutputStream || out == null) {
046: return (AnnotatedOutputStream) out;
047: }
048: return new AnnotatedOutputStream() {
049: private boolean done = false;
050:
051: public void write(int b) throws IOException {
052: checkDone();
053: out.write(b);
054: }
055:
056: public void write(byte b[]) throws IOException {
057: checkDone();
058: out.write(b);
059: }
060:
061: public void write(byte b[], int off, int len)
062: throws IOException {
063: checkDone();
064: out.write(b, off, len);
065: }
066:
067: public void flush() throws IOException {
068: checkDone();
069: out.flush();
070: }
071:
072: public void done() {
073: checkDone();
074: done = true;
075: }
076:
077: public void close() throws IOException {
078: out.close();
079: }
080:
081: private void checkDone() {
082: if (done) {
083: throw new IllegalStateException("done");
084: }
085: }
086: };
087: }
088:
089: /**
090: * Mark the stream as "done" -- after this call, we're only allowed to call
091: * {@link #close}.
092: * <p>
093: * When using sockets, we're not allowed to close the output stream before
094: * we've finished using the input stream. This is inefficient if we know
095: * we've completed all our output. The <code>done</code> method allows us to
096: * tell the stream that we won't write any more data or call flush. In
097: * practice this may be handled just like <code>close</code>.
098: */
099: public abstract void done() throws IOException;
100:
101: }
|