Showing the changes to java/pkg/sun/rmi/transport/proxy/HttpOutputStream.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,80 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 1996, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 5 | + * This code is free software; you can redistribute it and/or modify it | |
| 6 | + * under the terms of the GNU General Public License version 2 only, as | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * 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.writeBytes("Content-type: application/octet-stream\r\n"); | |
| 67 | + dos.writeBytes("Content-length: " + size() + "\r\n"); | |
| 68 | + dos.writeBytes("\r\n"); | |
| 69 | + writeTo(dos); | |
| 70 | + dos.flush(); | |
| 71 | + // Do not close the underlying stream here, because that would | |
| 72 | + // close the underlying socket and prevent reading a response. | |
| 73 | + reset(); // reset byte array | |
| 74 | + responseSent = true; | |
| 75 | + } | |
| 76 | + } | |
| 77 | + | |
| 78 | + /** data to send if the response would otherwise be empty */ | |
| 79 | + private static byte[] emptyData = { 0 }; | |
| 80 | +} | |