KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > http > servlets > jsp > JspWriter


1 package com.quadcap.http.servlets.jsp;
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.CharArrayWriter JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.PrintWriter JavaDoc;
44 import java.io.Writer JavaDoc;
45
46 /**
47  * The JspWriter implementation. We handle 'bufSize' and 'autoCommit'
48  * in this class.
49  *
50  * @author Stan Bailes
51  */

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