KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infohazard > maverick > util > PrintWriterBuffer


1 /*
2  * $Id: PrintWriterBuffer.java,v 1.4 2004/06/07 20:38:42 eelco12 Exp $
3  * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/util/PrintWriterBuffer.java,v $
4  */

5
6 package org.infohazard.maverick.util;
7
8 import java.io.PrintWriter JavaDoc;
9 import java.io.Reader JavaDoc;
10 import java.io.StringReader JavaDoc;
11 import java.io.StringWriter JavaDoc;
12 import java.io.UnsupportedEncodingException JavaDoc;
13
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17 /**
18  */

19 public class PrintWriterBuffer extends PrintWriter JavaDoc implements Buffer
20 {
21     /**
22      * Logger.
23      */

24     private static Log log = LogFactory.getLog(PrintWriterBuffer.class);
25     
26     /**
27      * Construct.
28      */

29     public PrintWriterBuffer()
30     {
31         super(new StringWriter JavaDoc());
32     }
33
34     /**
35      * It's more efficient to simply go directly to String.
36      * @see org.infohazard.maverick.util.Buffer#prefersReader()
37      */

38     public boolean prefersReader()
39     {
40         return false;
41     }
42
43     /**
44      * @see org.infohazard.maverick.util.Buffer#getAsReader()
45      */

46     public Reader JavaDoc getAsReader() throws UnsupportedEncodingException JavaDoc
47     {
48         return new StringReader JavaDoc(this.getAsString());
49     }
50
51     /**
52      * @see org.infohazard.maverick.util.Buffer#getAsString()
53      */

54     public String JavaDoc getAsString() throws UnsupportedEncodingException JavaDoc
55     {
56         if (this.size() == 0)
57         {
58             log.debug("size() was 0, returning empty string");
59             return "";
60         }
61         else
62             return this.out.toString();
63     }
64
65     /**
66      * @see org.infohazard.maverick.util.Buffer#size()
67      */

68     public int size()
69     {
70         return ((StringWriter JavaDoc)this.out).getBuffer().length();
71     }
72
73     /**
74      * @see java.io.Writer#close()
75      */

76     public void close()
77     {
78         // Actually, do nothing... the default close sets this.out to null.
79
}
80 }
81
Popular Tags