01: /*
02: * Copyright 1996 Sun Microsystems, Inc. All Rights Reserved.
03: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
04: *
05: * This code is free software; you can redistribute it and/or modify it
06: * under the terms of the GNU General Public License version 2 only, as
07: * published by the Free Software Foundation. Sun designates this
08: * particular file as subject to the "Classpath" exception as provided
09: * by Sun in the LICENSE file that accompanied this code.
10: *
11: * This code is distributed in the hope that it will be useful, but WITHOUT
12: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14: * version 2 for more details (a copy is included in the LICENSE file that
15: * accompanied this code).
16: *
17: * You should have received a copy of the GNU General Public License version
18: * 2 along with this work; if not, write to the Free Software Foundation,
19: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20: *
21: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22: * CA 95054 USA or visit www.sun.com if you need additional information or
23: * have any questions.
24: */
25: package sun.rmi.transport.proxy;
26:
27: import java.io.*;
28:
29: /**
30: * The HttpOutputStream class assists the HttpSendSocket and HttpReceiveSocket
31: * classes by providing an output stream that buffers its entire input until
32: * closed, and then it sends the complete transmission prefixed by the end of
33: * an HTTP header that specifies the content length.
34: */
35: class HttpOutputStream extends ByteArrayOutputStream {
36:
37: /** the output stream to send response to */
38: protected OutputStream out;
39:
40: /** true if HTTP response has been sent */
41: boolean responseSent = false;
42:
43: /**
44: * Begin buffering new HTTP response to be sent to a given stream.
45: * @param out the OutputStream to send response to
46: */
47: public HttpOutputStream(OutputStream out) {
48: super ();
49: this .out = out;
50: }
51:
52: /**
53: * On close, send HTTP-packaged response.
54: */
55: public synchronized void close() throws IOException {
56: if (!responseSent) {
57: /*
58: * If response would have zero content length, then make it
59: * have some arbitrary data so that certain clients will not
60: * fail because the "document contains no data".
61: */
62: if (size() == 0)
63: write(emptyData);
64:
65: DataOutputStream dos = new DataOutputStream(out);
66: dos
67: .writeBytes("Content-type: application/octet-stream\r\n");
68: dos.writeBytes("Content-length: " + size() + "\r\n");
69: dos.writeBytes("\r\n");
70: writeTo(dos);
71: dos.flush();
72: // Do not close the underlying stream here, because that would
73: // close the underlying socket and prevent reading a response.
74: reset(); // reset byte array
75: responseSent = true;
76: }
77: }
78:
79: /** data to send if the response would otherwise be empty */
80: private static byte[] emptyData = { 0 };
81: }
|