KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > winstone > WinstoneResponseWriter


1 /*
2  * Copyright 2003-2006 Rick Knowles <winstone-devel at lists sourceforge net>
3  * Distributed under the terms of either:
4  * - the common development and distribution license (CDDL), v1.0; or
5  * - the GNU Lesser General Public License, v2.1 or later
6  */

7 package winstone;
8
9 import java.io.IOException JavaDoc;
10 import java.io.OutputStreamWriter JavaDoc;
11 import java.io.PrintWriter JavaDoc;
12 import java.io.UnsupportedEncodingException JavaDoc;
13
14 /**
15  * A hacked print writer that allows us to trigger an automatic flush on
16  * println operations that go over the content length or buffer size.
17  *
18  * This is only necessary because the spec authors seem intent of having
19  * the print writer's flushing behaviour be half auto-flush and half not.
20  * Damned if I know why - seems unnecessary and confusing to me.
21  *
22  * @author <a HREF="mailto:rick_knowles@hotmail.com">Rick Knowles</a>
23  * @version $Id: WinstoneResponseWriter.java,v 1.3 2006/02/28 07:32:47 rickknowles Exp $
24  */

25 public class WinstoneResponseWriter extends PrintWriter JavaDoc {
26
27     private WinstoneOutputStream outputStream;
28     private WinstoneResponse response;
29     private int bytesBuffered;
30     
31     public WinstoneResponseWriter(WinstoneOutputStream out,
32             WinstoneResponse response) throws UnsupportedEncodingException JavaDoc {
33         super(new OutputStreamWriter JavaDoc(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 JavaDoc(buf, off, len));
48         }
49     }
50
51     public void write(String JavaDoc 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 JavaDoc input) {
59         try {
60             this.bytesBuffered += input.getBytes(response.getCharacterEncoding()).length;
61         } catch (IOException JavaDoc err) {/* impossible */}
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 JavaDoc 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 JavaDoc[] {contentLengthHeader,
82                     (this.outputStream.getOutputStreamLength() + this.bytesBuffered) + ""});
83             flush();
84         }
85     }
86 }
87
Popular Tags