1 31 32 package org.apache.commons.httpclient; 33 34 import org.apache.commons.logging.Log; 35 import org.apache.commons.logging.LogFactory; 36 37 import java.io.IOException ; 38 import java.io.OutputStream ; 39 40 41 54 public class RequestOutputStream 55 extends OutputStream { 56 57 59 67 public RequestOutputStream(OutputStream stream) { 68 this(stream, false); 69 } 70 71 80 public RequestOutputStream(OutputStream stream, boolean useChunking) { 81 if (stream == null) { 82 throw new NullPointerException ("stream parameter is null"); 83 } 84 this.stream = stream; 85 this.useChunking = useChunking; 86 } 87 88 90 91 private static final Log LOG = LogFactory.getLog(RequestOutputStream.class); 92 93 95 96 private boolean closed = false; 97 98 99 private OutputStream stream = null; 100 101 102 private boolean useChunking = false; 103 104 105 private static final byte CRLF[] = new byte[] {(byte) 13, (byte) 10}; 106 107 108 private static final byte ENDCHUNK[] = CRLF; 109 110 111 private static final byte ZERO[] = new byte[] {(byte) '0'}; 112 113 114 private static final byte ONE[] = new byte[] {(byte) '1'}; 115 116 118 119 126 public void setUseChunking(boolean useChunking) { 127 this.useChunking = useChunking; 128 } 129 130 131 138 public boolean isUseChunking() { 139 return useChunking; 140 } 141 142 144 153 public void print(String s) throws IOException { 154 LOG.trace("enter RequestOutputStream.print(String)"); 155 156 if (s == null) { 157 s = "null"; 158 } 159 int len = s.length(); 160 for (int i = 0; i < len; i++) { 161 write(s.charAt(i)); 162 } 163 } 164 165 172 public void println() throws IOException { 173 print("\r\n"); 174 } 175 176 185 public void println(String s) throws IOException { 186 print(s); 187 println(); 188 } 189 190 192 200 public void write(int b) throws IOException { 201 202 if (useChunking) { 204 stream.write(ONE, 0, ONE.length); 205 stream.write(CRLF, 0, CRLF.length); 206 stream.write(b); 207 stream.write(ENDCHUNK, 0, ENDCHUNK.length); 208 } else { 209 stream.write(b); 210 } 211 } 212 213 223 public void write(byte[] b, int off, int len) throws IOException { 224 LOG.trace("enter RequestOutputStream.write(byte[], int, int)"); 225 226 if (useChunking) { 227 byte chunkHeader[] = HttpConstants.getBytes(Integer.toHexString(len) + "\r\n"); 228 stream.write(chunkHeader, 0, chunkHeader.length); 229 stream.write(b, off, len); 230 stream.write(ENDCHUNK, 0, ENDCHUNK.length); 231 } else { 232 stream.write(b, off, len); 233 } 234 } 235 236 244 public void close() throws IOException { 245 LOG.trace("enter RequestOutputStream.close()"); 246 247 if (!closed) { 248 try { 249 if (useChunking) { 250 stream.write(ZERO, 0, ZERO.length); 252 stream.write(CRLF, 0, CRLF.length); 253 stream.write(ENDCHUNK, 0, ENDCHUNK.length); 254 } 255 } catch (IOException ioe) { 256 LOG.debug("Unexpected exception caught when closing output " 257 + " stream", ioe); 258 throw ioe; 259 } finally { 260 closed = true; 264 super.close(); 265 } 266 } 267 268 } 269 270 } 271 | Popular Tags |