1 7 package winstone; 8 9 import java.io.IOException ; 10 import java.io.OutputStreamWriter ; 11 import java.io.PrintWriter ; 12 import java.io.UnsupportedEncodingException ; 13 14 25 public class WinstoneResponseWriter extends PrintWriter { 26 27 private WinstoneOutputStream outputStream; 28 private WinstoneResponse response; 29 private int bytesBuffered; 30 31 public WinstoneResponseWriter(WinstoneOutputStream out, 32 WinstoneResponse response) throws UnsupportedEncodingException { 33 super(new OutputStreamWriter (out, response.getCharacterEncoding()), false); 34 this.outputStream = out; 35 this.response = response; 36 this.bytesBuffered = 0; 37 } 38 39 public void write(int c) { 40 super.write(c); 41 appendByteCount("" + ((char) c)); 42 } 43 44 public void write(char[] buf, int off, int len) { 45 super.write(buf, off, len); 46 if (buf != null) { 47 appendByteCount(new String (buf, off, len)); 48 } 49 } 50 51 public void write(String s, int off, int len) { 52 super.write(s, off, len); 53 if (s != null) { 54 appendByteCount(s.substring(off, len)); 55 } 56 } 57 58 protected void appendByteCount(String input) { 59 try { 60 this.bytesBuffered += input.getBytes(response.getCharacterEncoding()).length; 61 } catch (IOException err) {} 62 63 } 64 65 public void println() { 66 super.println(); 67 simulateAutoFlush(); 68 } 69 70 public void flush() { 71 super.flush(); 72 this.bytesBuffered = 0; 73 } 74 75 protected void simulateAutoFlush() { 76 String contentLengthHeader = response.getHeader(WinstoneResponse.CONTENT_LENGTH_HEADER); 77 if ((contentLengthHeader != null) && 78 ((this.outputStream.getOutputStreamLength() + this.bytesBuffered) >= 79 Integer.parseInt(contentLengthHeader))) { 80 Logger.log(Logger.FULL_DEBUG, Launcher.RESOURCES, "WinstoneResponseWriter.AutoFlush", 81 new String [] {contentLengthHeader, 82 (this.outputStream.getOutputStreamLength() + this.bytesBuffered) + ""}); 83 flush(); 84 } 85 } 86 } 87 | Popular Tags |