KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mortbay > jetty > servlet > ServletWriter


1 // ========================================================================
2
// $Id: ServletWriter.java,v 1.16 2005/08/13 00:01:27 gregwilkins Exp $
3
// Copyright 2000-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15

16 package org.mortbay.jetty.servlet;
17
18 import java.io.IOException JavaDoc;
19 import java.io.OutputStream JavaDoc;
20 import java.io.OutputStreamWriter JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.io.UnsupportedEncodingException JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.mortbay.log.LogFactory;
26 import org.mortbay.http.HttpOutputStream;
27 import org.mortbay.util.IO;
28 import org.mortbay.util.LogSupport;
29
30
31 /* ------------------------------------------------------------ */
32 /** Servlet PrintWriter.
33  * This writer can be disabled.
34  * It is crying out for optimization.
35  *
36  * @version $Revision: 1.16 $
37  * @author Greg Wilkins (gregw)
38  */

39 class ServletWriter extends PrintWriter JavaDoc
40 {
41     private static Log log = LogFactory.getLog(ServletWriter.class);
42
43     String JavaDoc encoding=null;
44     OutputStream JavaDoc os=null;
45     boolean written=false;
46     
47     /* ------------------------------------------------------------ */
48     ServletWriter(OutputStream JavaDoc os)
49         throws IOException JavaDoc
50     {
51         super((os instanceof HttpOutputStream)
52               ?((HttpOutputStream)os).getWriter(null)
53               :new OutputStreamWriter JavaDoc(os));
54         this.os=os;
55     }
56     
57     /* ------------------------------------------------------------ */
58     ServletWriter(OutputStream JavaDoc os, String JavaDoc encoding)
59         throws IOException JavaDoc
60     {
61         super((os instanceof HttpOutputStream)
62               ?((HttpOutputStream)os).getWriter(encoding)
63               :new OutputStreamWriter JavaDoc(os,encoding));
64         this.os=os;
65         this.encoding=encoding;
66     }
67
68     /* ------------------------------------------------------------ */
69     public void disable()
70     {
71         out=IO.getNullWriter();
72     }
73     
74     /* ------------------------------------------------------------ */
75     public void reset()
76     {
77         try{
78             out=IO.getNullWriter();
79             super.flush();
80             out=new OutputStreamWriter JavaDoc(os,encoding);
81             written=false;
82         }
83         catch(UnsupportedEncodingException JavaDoc e)
84         {
85             log.fatal(e); System.exit(1);
86         }
87     }
88     
89
90     /* ------------------------------------------------------------ */
91     public boolean isWritten()
92     {
93         return written;
94     }
95
96     
97     /* ------------------------------------------------------------ */
98     public void print(boolean p) {written=true;super.print(p);}
99     public void print(char p) {written=true;super.print(p);}
100     public void print(char[] p) {written=true;super.print(p);}
101     public void print(double p) {written=true;super.print(p);}
102     public void print(float p) {written=true;super.print(p);}
103     public void print(int p) {written=true;super.print(p);}
104     public void print(long p) {written=true;super.print(p);}
105     public void print(Object JavaDoc p) {written=true;super.print(p);}
106     public void print(String JavaDoc p) {written=true;super.print(p);}
107     public void println() {written=true;super.println();}
108     public void println(boolean p){written=true;super.println(p);}
109     public void println(char p) {written=true;super.println(p);}
110     public void println(char[] p) {written=true;super.println(p);}
111     public void println(double p) {written=true;super.println(p);}
112     public void println(float p) {written=true;super.println(p);}
113     public void println(int p) {written=true;super.println(p);}
114     public void println(long p) {written=true;super.println(p);}
115     public void println(Object JavaDoc p) {written=true;super.println(p);}
116     public void println(String JavaDoc p) {written=true;super.println(p);}
117
118     
119     public void write(int c)
120     {
121         try
122         {
123             if (out==null)
124                 throw new IOException JavaDoc("closed");
125             written=true;
126             out.write(c);
127         }
128         catch (IOException JavaDoc e){LogSupport.ignore(log,e);setError();}
129     }
130     
131     public void write(char[] cbuf, int off, int len)
132     {
133         try
134         {
135             if (out==null)
136                 throw new IOException JavaDoc("closed");
137             written=true;
138             out.write(cbuf,off,len);
139         }
140         catch (IOException JavaDoc e){LogSupport.ignore(log,e);setError();}
141     }
142     
143     public void write(char[] cbuf)
144     {
145         try
146         {
147             if (out==null)
148                 throw new IOException JavaDoc("closed");
149             written=true;
150             out.write(cbuf,0,cbuf.length);
151         }
152         catch (IOException JavaDoc e){LogSupport.ignore(log,e);setError();}
153     }
154
155     public void write(String JavaDoc s, int off, int len)
156     {
157         try
158         {
159             if (out==null)
160                 throw new IOException JavaDoc("closed");
161             written=true;
162             out.write(s,off,len);
163         }
164         catch (IOException JavaDoc e){LogSupport.ignore(log,e);setError();}
165     }
166
167     public void write(String JavaDoc s)
168     {
169         try
170         {
171             if (out==null)
172                 throw new IOException JavaDoc("closed");
173             written=true;
174             out.write(s,0,s.length());
175         }
176         catch (IOException JavaDoc e){LogSupport.ignore(log,e);setError();}
177     }
178 }
179
Popular Tags