View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/RequestOutputStream.java,v 1.21 2003/04/04 02:37:02 mbecke Exp $ 3 * $Revision: 1.21 $ 4 * $Date: 2003/04/04 02:37:02 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>. 59 * 60 * [Additional notices, if required by prior licensing conditions] 61 * 62 */ 63 64 package org.apache.commons.httpclient; 65 66 import org.apache.commons.logging.Log; 67 import org.apache.commons.logging.LogFactory; 68 69 import java.io.IOException; 70 import java.io.OutputStream; 71 72 73 /*** 74 * <p> 75 * {@link OutputStream} wrapper supporting the chunked transfer encoding. 76 * </p> 77 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a> 78 * @author Sean C. Sullivan 79 * @author <a href="mailto:dion@apache.org">dIon Gillard</a> 80 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 81 * @version $Revision: 1.21 $ $Date: 2003/04/04 02:37:02 $ 82 * 83 * @deprecated Use new ChunkedOutputStream(HttpConnecion#getRequestOutputStream()); 84 * 85 */ 86 public class RequestOutputStream 87 extends OutputStream { 88 89 // ----------------------------------------------------------- Constructors 90 91 /*** 92 * Construct an output stream wrapping the given stream. 93 * The stream will not use chunking. 94 * 95 * @param stream wrapped output stream. Must be non-null. 96 * 97 * @deprecated Use ChunkedOutputStream; 98 */ 99 public RequestOutputStream(OutputStream stream) { 100 this(stream, false); 101 } 102 103 /*** 104 * Construct an output stream wrapping the given stream. 105 * 106 * @param stream wrapped output stream. Must be non-null. 107 * @param useChunking when <code>true</code>, the chunked transfer encoding 108 * will be used 109 * 110 * @deprecated Use ChunkedOutputStream; 111 */ 112 public RequestOutputStream(OutputStream stream, boolean useChunking) { 113 if (stream == null) { 114 throw new NullPointerException("stream parameter is null"); 115 } 116 this.stream = stream; 117 this.useChunking = useChunking; 118 } 119 120 // ------------------------------------------------------- Static Variables 121 122 /*** Log object for this class. */ 123 private static final Log LOG = LogFactory.getLog(RequestOutputStream.class); 124 125 // ----------------------------------------------------- Instance Variables 126 127 /*** Has this stream been closed? */ 128 private boolean closed = false; 129 130 /*** The underlying output stream to which we will write data */ 131 private OutputStream stream = null; 132 133 /*** True if chunking is allowed */ 134 private boolean useChunking = false; 135 136 /*** <tt>"\r\n"</tt>, as bytes. */ 137 private static final byte CRLF[] = new byte[] {(byte) 13, (byte) 10}; 138 139 /*** End chunk */ 140 private static final byte ENDCHUNK[] = CRLF; 141 142 /*** 0 */ 143 private static final byte ZERO[] = new byte[] {(byte) '0'}; 144 145 /*** 1 */ 146 private static final byte ONE[] = new byte[] {(byte) '1'}; 147 148 // ------------------------------------------------------------- Properties 149 150 151 /*** 152 * Use chunking flag setter. 153 * 154 * @param useChunking true if chunking is to be used, false otherwise 155 * 156 * @deprecated Use ChunkedOutputStream; 157 */ 158 public void setUseChunking(boolean useChunking) { 159 this.useChunking = useChunking; 160 } 161 162 163 /*** 164 * Use chunking flag getter. 165 * 166 * @return true if chunking is to be used, false otherwise 167 * 168 * @deprecated Use ChunkedOutputStream; 169 */ 170 public boolean isUseChunking() { 171 return useChunking; 172 } 173 174 // --------------------------------------------------------- Public Methods 175 176 /*** 177 * Writes a <code>String</code> to the client, without a carriage return 178 * line feed (CRLF) character at the end. 179 * 180 * @param s the <code>String</code> to send to the client. Must be non-null. 181 * @throws IOException if an input or output exception occurred 182 * 183 * @deprecated Use ChunkedOutputStream; 184 */ 185 public void print(String s) throws IOException { 186 LOG.trace("enter RequestOutputStream.print(String)"); 187 188 if (s == null) { 189 s = "null"; 190 } 191 int len = s.length(); 192 for (int i = 0; i < len; i++) { 193 write(s.charAt(i)); 194 } 195 } 196 197 /*** 198 * Writes a carriage return-line feed (CRLF) to the client. 199 * 200 * @throws IOException if an input or output exception occurred 201 * 202 * @deprecated Use ChunkedOutputStream; 203 */ 204 public void println() throws IOException { 205 print("\r\n"); 206 } 207 208 /*** 209 * Writes a <code>String</code> to the client, 210 * followed by a carriage return-line feed (CRLF). 211 * 212 * @param s the </code>String</code> to write to the client 213 * @exception IOException if an input or output exception occurred 214 * 215 * @deprecated Use ChunkedOutputStream; 216 */ 217 public void println(String s) throws IOException { 218 print(s); 219 println(); 220 } 221 222 // -------------------------------------------- ServletOutputStream Methods 223 224 /*** 225 * Write the specified byte to our output stream. 226 * 227 * @param b The byte to be written 228 * @throws IOException if an input/output error occurs 229 * 230 * @deprecated Use ChunkedOutputStream; 231 */ 232 public void write(int b) throws IOException { 233 234 //FIXME: If using chunking, the chunks are ONE byte long! 235 if (useChunking) { 236 stream.write(ONE, 0, ONE.length); 237 stream.write(CRLF, 0, CRLF.length); 238 stream.write(b); 239 stream.write(ENDCHUNK, 0, ENDCHUNK.length); 240 } else { 241 stream.write(b); 242 } 243 } 244 245 /*** 246 * Write the specified byte array. 247 * 248 * @param b the byte array to write out 249 * @param off the offset within <code>b</code> to start writing from 250 * @param len the length of data within <code>b</code> to write 251 * @throws IOException when errors occur writing output 252 * 253 * @deprecated Use ChunkedOutputStream; 254 */ 255 public void write(byte[] b, int off, int len) throws IOException { 256 LOG.trace("enter RequestOutputStream.write(byte[], int, int)"); 257 258 if (useChunking) { 259 byte chunkHeader[] = HttpConstants.getBytes(Integer.toHexString(len) + "\r\n"); 260 stream.write(chunkHeader, 0, chunkHeader.length); 261 stream.write(b, off, len); 262 stream.write(ENDCHUNK, 0, ENDCHUNK.length); 263 } else { 264 stream.write(b, off, len); 265 } 266 } 267 268 /*** 269 * Close this output stream, causing any buffered data to be flushed and 270 * any further output data to throw an IOException. 271 * 272 * @throws IOException if an error occurs closing the stream 273 * 274 * @deprecated Use ChunkedOutputStream; 275 */ 276 public void close() throws IOException { 277 LOG.trace("enter RequestOutputStream.close()"); 278 279 if (!closed) { 280 try { 281 if (useChunking) { 282 // Write the final chunk. 283 stream.write(ZERO, 0, ZERO.length); 284 stream.write(CRLF, 0, CRLF.length); 285 stream.write(ENDCHUNK, 0, ENDCHUNK.length); 286 } 287 } catch (IOException ioe) { 288 LOG.debug("Unexpected exception caught when closing output " 289 + " stream", ioe); 290 throw ioe; 291 } finally { 292 // regardless of what happens, mark the stream as closed. 293 // if there are errors closing it, there's not much we can do 294 // about it 295 closed = true; 296 super.close(); 297 } 298 } 299 300 } 301 302 }

This page was automatically generated by Maven