1 31 32 package org.apache.commons.httpclient; 33 34 import java.io.IOException ; 35 import java.io.OutputStream ; 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 55 public class ChunkedOutputStream extends OutputStream { 56 57 59 60 private static final byte CRLF[] = new byte[] {(byte) 13, (byte) 10}; 61 62 63 private static final byte ENDCHUNK[] = CRLF; 64 65 66 private static final byte ZERO[] = new byte[] {(byte) '0'}; 67 68 69 private static final byte ONE[] = new byte[] {(byte) '1'}; 70 71 72 private static final Log LOG = LogFactory.getLog(ChunkedOutputStream.class); 73 74 76 77 private boolean closed = false; 78 79 80 private OutputStream stream = null; 81 82 84 90 public ChunkedOutputStream(OutputStream stream) { 91 if (stream == null) { 92 throw new NullPointerException ("stream parameter is null"); 93 } 94 this.stream = stream; 95 } 96 97 98 100 108 public void print(String s) throws IOException { 109 LOG.trace("enter ChunckedOutputStream.print(String)"); 110 if (s == null) { 111 s = "null"; 112 } 113 write(HttpConstants.getBytes(s)); 114 } 115 116 121 public void println() throws IOException { 122 print("\r\n"); 123 } 124 125 132 public void println(String s) throws IOException { 133 print(s); 134 println(); 135 } 136 137 139 146 public void write (int b) throws IOException , IllegalStateException { 147 if (closed) { 148 throw new IllegalStateException ("Output stream already closed"); 149 } 150 stream.write(ONE, 0, ONE.length); 152 stream.write(CRLF, 0, CRLF.length); 153 stream.write(b); 154 stream.write(ENDCHUNK, 0, ENDCHUNK.length); 155 LOG.debug("Writing chunk (length: 1)"); 156 } 157 158 166 public void write (byte[] b, int off, int len) throws IOException { 167 LOG.trace("enter ChunckedOutputStream.write(byte[], int, int)"); 168 169 if (closed) { 170 throw new IllegalStateException ("Output stream already closed"); 171 } 172 byte chunkHeader[] = HttpConstants.getBytes ( 173 Integer.toHexString(len) + "\r\n"); 174 stream.write(chunkHeader, 0, chunkHeader.length); 175 stream.write(b, off, len); 176 stream.write(ENDCHUNK, 0, ENDCHUNK.length); 177 if (LOG.isDebugEnabled()) { 178 LOG.debug("Writing chunk (length: " + len + ")"); 179 } 180 } 181 182 189 public void writeClosingChunk() throws IOException { 190 LOG.trace("enter ChunkedOutputStream.writeClosingChunk()"); 191 192 if (!closed) { 193 try { 194 stream.write(ZERO, 0, ZERO.length); 196 stream.write(CRLF, 0, CRLF.length); 197 stream.write(ENDCHUNK, 0, ENDCHUNK.length); 198 LOG.debug("Writing closing chunk"); 199 } catch (IOException e) { 200 LOG.debug("Unexpected exception caught when closing " 201 + "output stream", e); 202 throw e; 203 } finally { 204 closed = true; 208 } 209 } 210 } 211 212 216 public void flush() throws IOException { 217 stream.flush(); 218 } 219 220 227 public void close() throws IOException { 228 LOG.trace("enter ChunkedOutputStream.close()"); 229 writeClosingChunk(); 230 super.close(); 231 } 232 233 234 } 235 | Popular Tags |