KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > server22 > JspWriter


1 package com.quadcap.http.server22;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42
43 import com.quadcap.io.IO;
44
45 /**
46  * A simple implementation of JspWriter that uses the
47  * <code>HttpOutputStream</code>
48  * class to do most of the work.
49  *
50  * @author Stan Bailes
51  */

52 public class JspWriter extends javax.servlet.jsp.JspWriter JavaDoc {
53     HttpOutputStream out;
54     boolean closed = false;
55     byte[] buf = null;
56     static String JavaDoc lineSep = System.getProperty("line.separator");
57
58     public JspWriter(int bufferSize, boolean autoFlush) {
59     super(bufferSize, autoFlush);
60     }
61
62     final void setHttpOutputStream(HttpOutputStream out) {
63         this.out = out;
64     }
65
66     public final void newLine() throws IOException JavaDoc {
67     print(lineSep);
68     }
69
70     public final void print(boolean b) throws IOException JavaDoc {
71     print(b ? "true" : "false");
72     }
73
74     public final void print(char c) throws IOException JavaDoc {
75     print(String.valueOf(c));
76     }
77
78     public final void print(int i) throws IOException JavaDoc {
79     print(String.valueOf(i));
80     }
81
82     public final void print(long l) throws IOException JavaDoc {
83     print(String.valueOf(l));
84     }
85
86     public final void print(float f) throws IOException JavaDoc {
87     print(String.valueOf(f));
88     }
89
90     public final void print(double d) throws IOException JavaDoc {
91     print(String.valueOf(d));
92     }
93
94     public final void print(char[] ch) throws IOException JavaDoc {
95     write(ch, 0, ch.length);
96     }
97
98     public final void print(String JavaDoc s) throws IOException JavaDoc {
99         final int len = s.length();
100     if (buf == null || buf.length < len) {
101             buf = new byte[len];
102         }
103         for (int i = 0; i < len; i++) {
104             buf[i] = (byte)(s.charAt(i) & 0xff);
105         }
106         out.write(buf, 0, len);
107     }
108
109     public final void write(String JavaDoc s) throws IOException JavaDoc {
110         IO.write(out, s);
111     }
112
113     public final void write(String JavaDoc s, int off, int len) throws IOException JavaDoc {
114         for (int i = 0; i < len; i++) {
115             out.write(s.charAt(off + i) & 0xff);
116         }
117     }
118
119     public final void print(Object JavaDoc obj) throws IOException JavaDoc {
120     print(obj.toString());
121     }
122
123     public final void println() throws IOException JavaDoc {
124     print(lineSep);
125     }
126
127     public final void println(boolean b) throws IOException JavaDoc {
128     print(b);
129     println();
130     }
131
132     public final void println(char c) throws IOException JavaDoc {
133     print(c);
134     println();
135     }
136
137     public final void println(int i) throws IOException JavaDoc {
138     print(i);
139     println();
140     }
141
142     public final void println(long l) throws IOException JavaDoc {
143     print(l);
144     println();
145     }
146
147     public final void println(float f) throws IOException JavaDoc {
148     print(f);
149     println();
150     }
151
152     public final void println(double d) throws IOException JavaDoc {
153     print(d);
154     println();
155     }
156
157     public final void println(char[] ch) throws IOException JavaDoc {
158     print(ch);
159     println();
160     }
161
162     public final void println(String JavaDoc s) throws IOException JavaDoc {
163     print(s);
164     println();
165     }
166
167     public final void println(Object JavaDoc obj) throws IOException JavaDoc {
168     print(obj);
169     println();
170     }
171
172     public final void write(char[] ch, int off, int len) throws IOException JavaDoc {
173     if (closed) throw new IOException JavaDoc("JspWriter closed");
174     for (int i = 0; i < len; i++) {
175             out.write(ch[off + i]);
176         }
177     }
178
179     public final void clear() throws IOException JavaDoc {
180         try {
181             out.reset();
182         } catch (IllegalStateException JavaDoc e) {
183             throw new IOException JavaDoc(e.toString());
184         }
185     }
186
187     public final void clearBuffer() throws IOException JavaDoc {
188         try {
189             out.reset();
190         } catch (IllegalStateException JavaDoc e) {}
191     }
192
193     public final void flush() throws IOException JavaDoc {
194     out.flush();
195     }
196
197     public final void close() throws IOException JavaDoc {
198     if (!closed) {
199         out.close();
200         closed = true;
201     }
202     }
203
204     public final int getRemaining() {
205     return out.getRemaining();
206     }
207
208     public final void setBufferSize(int bufferSize) {
209         out.setBufferSize(bufferSize);
210     }
211
212     public final void setAutoFlush(boolean autoFlush) {
213         out.setAutoFlush(autoFlush);
214     }
215 }
216
217
Popular Tags