KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ServletOutputStreamBuffer.java,v 1.2 2003/10/27 11:00:55 thusted Exp $
3  * $Source: /cvsroot/mav/maverick/src/java/org/infohazard/maverick/util/ServletOutputStreamBuffer.java,v $
4  */

5
6 package org.infohazard.maverick.util;
7
8 import java.io.*;
9 import javax.servlet.*;
10
11 /**
12  */

13 public class ServletOutputStreamBuffer extends ServletOutputStream implements Buffer
14 {
15     /**
16      */

17     protected String JavaDoc charset;
18
19     /**
20      */

21     protected FastByteArrayOutputStream holder = new FastByteArrayOutputStream();
22
23     /**
24      * @param charset - if null, default character encoding is assumed.
25      */

26     public ServletOutputStreamBuffer(String JavaDoc charset)
27     {
28         this.charset = charset;
29     }
30
31     /**
32      * It's always more efficient to use a reader because the buffer need
33      * not be copied.
34      */

35     public boolean prefersReader()
36     {
37         return true;
38     }
39
40     /**
41      */

42     public Reader getAsReader() throws UnsupportedEncodingException
43     {
44         if (this.charset != null)
45             return new InputStreamReader(this.holder.getInputStream(), this.charset);
46         else
47             return new InputStreamReader(this.holder.getInputStream());
48     }
49
50     /**
51      */

52     public String JavaDoc getAsString() throws UnsupportedEncodingException
53     {
54         if (this.charset != null)
55             return this.holder.toString(this.charset);
56         else
57             return this.holder.toString();
58     }
59
60     /**
61      */

62     public int size()
63     {
64         return this.holder.size();
65     }
66
67     /**
68      * Overriden from ServletOutputStream
69      */

70     public void write(int b) throws IOException
71     {
72         holder.write(b);
73     }
74 }
75
Popular Tags