Source code: org/enhydra/servlet/connectionMethods/EnhydraDirector/EnhydraDirectorBufferedOutputStream.java
1 /*
2 * Enhydra Java Application Server Project
3 *
4 * The contents of this file are subject to the Enhydra Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License on
7 * the Enhydra web site ( http://www.enhydra.org/ ).
8 *
9 * Software distributed under the License is distributed on an "AS IS"
10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11 * the License for the specific terms governing rights and limitations
12 * under the License.
13 *
14 * The Initial Developer of the Enhydra Application Server is Lutris
15 * Technologies, Inc. The Enhydra Application Server and portions created
16 * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17 * All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 * $Id: EnhydraDirectorBufferedOutputStream.java,v 1.3.12.1 2000/10/19 17:59:09 jasona Exp $
22 */
23
24 package org.enhydra.servlet.connectionMethods.EnhydraDirector;
25
26 import java.io.OutputStream;
27 import java.io.BufferedOutputStream;
28 import java.io.IOException;
29
30 /**
31 * Extension of <code>BufferedOutputStream</code> to allow "reset" of
32 * the buffer.
33 * <P>
34 * A servlet container's <code>ServletResponse</code> object should
35 * not go into the "committed" (data sent) state until the headers
36 * have actually been sent over the wire. This allows a servlet to
37 * specify a large buffer size and be able to reset or change headers
38 * well after the first byte of data has been written to the
39 * <code>ServletOutputStream</code>.
40 * <P>
41 * This class has a <code>reset()</code> that, after checking for
42 * committed state of the connection, discards any buffered data
43 * and self destructs, dropping any reference to its internally
44 * held output stream. Once <code>reset()</code> has been called,
45 * this object must be discarded as it is then useless. A new
46 * instance should be created in its place.
47 */
48 public class EnhydraDirectorBufferedOutputStream extends BufferedOutputStream
49 {
50 /**
51 * Internal reference for for this stream's underlying connection.
52 */
53 private EnhydraDirectorConnection conn;
54
55 /**
56 * Creates a new instance with the specified connection context,
57 * <code>OutputStream</code>, and buffer size.
58 *
59 * @param os The output stream.
60 * @param size Buffer size.
61 * @param connection The connection context.
62 */
63 public
64 EnhydraDirectorBufferedOutputStream(OutputStream os, int size,
65 EnhydraDirectorConnection connection)
66 {
67 super(os, size);
68 conn = connection;
69 }
70
71 /**
72 * Discards buffered data and drops reference to the output stream.
73 * <P>
74 * If the current connection has not sent any data over the wire,
75 * this method drops any buffered data, as well as the output stream
76 * reference, to ensure that this instance makes no further calls
77 * to the underlying stream. It is expected that the caller of
78 * this method will also discard any reference to this
79 * <code>EnhydraDirectorBufferedOutputStream</code> object. Once
80 * <code>reset()</code> has been called, this stream becomes
81 * unusable.
82 *
83 * @exception IllegalStateException If called after data has been sent.
84 */
85 public
86 void reset()
87 throws IllegalStateException
88 {
89 if (conn.isCommitted()) {
90 throw new IllegalStateException("Data already sent: cannot reset");
91 }
92 out = null;
93 count = 0;
94 buf = null;
95 try { close(); } catch (Exception e) {}
96 }
97
98 /**
99 * Flushes outgoing data, then flushes the contained output stream.
100 *
101 * @exception IOException If there is an error sending data.
102 */
103 public
104 void flush()
105 throws IOException
106 {
107 if (out != null) {
108 super.flush();
109 out.flush();
110 }
111 }
112 }
113