View Javadoc
1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ChunkedOutputStream.java,v 1.10 2003/04/19 22:29:31 mbecke Exp $ 3 * $Revision: 1.10 $ 4 * $Date: 2003/04/19 22:29:31 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 2002-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 java.io.IOException; 67 import java.io.OutputStream; 68 import org.apache.commons.logging.Log; 69 import org.apache.commons.logging.LogFactory; 70 71 /*** 72 * <p> 73 * Wrapper supporting the chunked transfer encoding. 74 * </p> 75 * 76 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a> 77 * @author Sean C. Sullivan 78 * @author <a href="mailto:dion@apache.org">dIon Gillard</a> 79 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> 80 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 81 * @version $Revision: 1.10 $ $Date: 2003/04/19 22:29:31 $ 82 * 83 * @see ChunkedInputStream 84 * @since 2.0 85 * 86 */ 87 public class ChunkedOutputStream extends OutputStream { 88 89 // ------------------------------------------------------- Static Variables 90 91 /*** <tt>"\r\n"</tt>, as bytes. */ 92 private static final byte CRLF[] = new byte[] {(byte) 13, (byte) 10}; 93 94 /*** End chunk */ 95 private static final byte ENDCHUNK[] = CRLF; 96 97 /*** 0 */ 98 private static final byte ZERO[] = new byte[] {(byte) '0'}; 99 100 /*** 1 */ 101 private static final byte ONE[] = new byte[] {(byte) '1'}; 102 103 /*** Log object for this class. */ 104 private static final Log LOG = LogFactory.getLog(ChunkedOutputStream.class); 105 106 // ----------------------------------------------------- Instance Variables 107 108 /*** Has this stream been closed? */ 109 private boolean closed = false; 110 111 /*** The underlying output stream to which we will write data */ 112 private OutputStream stream = null; 113 114 // ----------------------------------------------------------- Constructors 115 116 /*** 117 * Construct an output stream wrapping the given stream. 118 * The stream will not use chunking. 119 * 120 * @param stream wrapped output stream. Must be non-null. 121 */ 122 public ChunkedOutputStream(OutputStream stream) { 123 if (stream == null) { 124 throw new NullPointerException("stream parameter is null"); 125 } 126 this.stream = stream; 127 } 128 129 130 // --------------------------------------------------------- Public Methods 131 132 /*** 133 * Writes a <code>String</code> to the client, without a carriage return 134 * line feed (CRLF) character at the end. The platform default encoding is 135 * used! 136 * 137 * @param s the <code>String</code> to send to the client. Must be non-null. 138 * @throws IOException if an input or output exception occurred 139 */ 140 public void print(String s) throws IOException { 141 LOG.trace("enter ChunckedOutputStream.print(String)"); 142 if (s == null) { 143 s = "null"; 144 } 145 write(HttpConstants.getBytes(s)); 146 } 147 148 /*** 149 * Writes a carriage return-line feed (CRLF) to the client. 150 * 151 * @throws IOException if an input or output exception occurred 152 */ 153 public void println() throws IOException { 154 print("\r\n"); 155 } 156 157 /*** 158 * Writes a <code>String</code> to the client, 159 * followed by a carriage return-line feed (CRLF). 160 * 161 * @param s the </code>String</code> to write to the client 162 * @exception IOException if an input or output exception occurred 163 */ 164 public void println(String s) throws IOException { 165 print(s); 166 println(); 167 } 168 169 // -------------------------------------------- OutputStream Methods 170 171 /*** 172 * Write the specified byte to our output stream. 173 * 174 * @param b The byte to be written 175 * @throws IOException if an input/output error occurs 176 * @throws IllegalStateException if stream already closed 177 */ 178 public void write (int b) throws IOException, IllegalStateException { 179 if (closed) { 180 throw new IllegalStateException("Output stream already closed"); 181 } 182 //FIXME: If using chunking, the chunks are ONE byte long! 183 stream.write(ONE, 0, ONE.length); 184 stream.write(CRLF, 0, CRLF.length); 185 stream.write(b); 186 stream.write(ENDCHUNK, 0, ENDCHUNK.length); 187 LOG.debug("Writing chunk (length: 1)"); 188 } 189 190 /*** 191 * Write the specified byte array. 192 * 193 * @param b the byte array to write out 194 * @param off the offset within <code>b</code> to start writing from 195 * @param len the length of data within <code>b</code> to write 196 * @throws IOException when errors occur writing output 197 */ 198 public void write (byte[] b, int off, int len) throws IOException { 199 LOG.trace("enter ChunckedOutputStream.write(byte[], int, int)"); 200 201 if (closed) { 202 throw new IllegalStateException("Output stream already closed"); 203 } 204 byte chunkHeader[] = HttpConstants.getBytes ( 205 Integer.toHexString(len) + "\r\n"); 206 stream.write(chunkHeader, 0, chunkHeader.length); 207 stream.write(b, off, len); 208 stream.write(ENDCHUNK, 0, ENDCHUNK.length); 209 if (LOG.isDebugEnabled()) { 210 LOG.debug("Writing chunk (length: " + len + ")"); 211 } 212 } 213 214 /*** 215 * Close this output stream, causing any buffered data to be flushed and 216 * any further output data to throw an IOException. The underlying stream 217 * is not closed! 218 * 219 * @throws IOException if an error occurs closing the stream 220 */ 221 public void writeClosingChunk() throws IOException { 222 LOG.trace("enter ChunkedOutputStream.writeClosingChunk()"); 223 224 if (!closed) { 225 try { 226 // Write the final chunk. 227 stream.write(ZERO, 0, ZERO.length); 228 stream.write(CRLF, 0, CRLF.length); 229 stream.write(ENDCHUNK, 0, ENDCHUNK.length); 230 LOG.debug("Writing closing chunk"); 231 } catch (IOException e) { 232 LOG.debug("Unexpected exception caught when closing " 233 + "output stream", e); 234 throw e; 235 } finally { 236 // regardless of what happens, mark the stream as closed. 237 // if there are errors closing it, there's not much we can do 238 // about it 239 closed = true; 240 } 241 } 242 } 243 244 /*** 245 * Flushes the underlying stream. 246 * @throws IOException If an IO problem occurs. 247 */ 248 public void flush() throws IOException { 249 stream.flush(); 250 } 251 252 /*** 253 * Close this output stream, causing any buffered data to be flushed and 254 * any further output data to throw an IOException. The underlying stream 255 * is not closed! 256 * 257 * @throws IOException if an error occurs closing the stream 258 */ 259 public void close() throws IOException { 260 LOG.trace("enter ChunkedOutputStream.close()"); 261 writeClosingChunk(); 262 super.close(); 263 } 264 265 266 }

This page was automatically generated by Maven