1 19 20 package com.maverick.http; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 25 29 public class ChunkedContentSource implements ContentSource { 30 31 ChunkedOutputStream actualOut = new ChunkedOutputStream(); 32 HttpConnection con; 33 34 long bytesTransfered = 0; 35 36 public ChunkedContentSource() throws IOException { 37 38 } 39 40 public long getBytesTransferred() { 41 return bytesTransfered; 42 } 43 44 public void setHeaders(HttpRequest request, HttpConnection con) { 45 this.con = con; 46 request.setHeaderField("transfer-encoding", "chunked"); } 48 49 public OutputStream getOutputStream() { 50 return actualOut; 51 } 52 53 class ChunkedOutputStream extends OutputStream { 54 55 public void write(byte[] buf, int off, int len) throws IOException { 56 bytesTransfered += len; 57 con.getOutputStream().write((Integer.toHexString(len) + "\r\n").getBytes("UTF8")); con.getOutputStream().write(buf, off, len); 59 con.getOutputStream().write("\r\n".getBytes("UTF8")); } 61 62 public void write(int b) throws IOException { 63 write(new byte[] { (byte) b }, 0, 1); 64 } 65 66 public void close() throws IOException { 67 con.getOutputStream().write("0\r\n\r\n".getBytes("UTF8")); } 69 } 70 } 71 | Popular Tags |