001: package com.quadcap.http.servlets.jsp;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.CharArrayWriter;
042: import java.io.IOException;
043: import java.io.PrintWriter;
044: import java.io.Writer;
045:
046: /**
047: * The JspWriter implementation. We handle 'bufSize' and 'autoCommit'
048: * in this class.
049: *
050: * @author Stan Bailes
051: */
052: public class JspWriter extends javax.servlet.jsp.JspWriter {
053: CharArrayWriter w;
054: Writer out;
055: int written = 0;
056: boolean flushed = false;
057: boolean closed = false;
058: static String lineSep = System.getProperty("line.separator");
059:
060: public JspWriter(Writer out, int bufferSize, boolean autoFlush) {
061: super (bufferSize, autoFlush);
062: this .out = out;
063: w = new CharArrayWriter();
064: }
065:
066: public void newLine() throws IOException {
067: print(lineSep);
068: }
069:
070: public void print(boolean b) throws IOException {
071: print(b ? "true" : "false");
072: }
073:
074: public void print(char c) throws IOException {
075: print(String.valueOf(c));
076: }
077:
078: public void print(int i) throws IOException {
079: print(String.valueOf(i));
080: }
081:
082: public void print(long l) throws IOException {
083: print(String.valueOf(l));
084: }
085:
086: public void print(float f) throws IOException {
087: print(String.valueOf(f));
088: }
089:
090: public void print(double d) throws IOException {
091: print(String.valueOf(d));
092: }
093:
094: public void print(char[] ch) throws IOException {
095: write(ch, 0, ch.length);
096: }
097:
098: public void print(String s) throws IOException {
099: w.write(s, 0, s.length());
100: }
101:
102: public void print(Object obj) throws IOException {
103: print(obj.toString());
104: }
105:
106: public void println() throws IOException {
107: print(lineSep);
108: }
109:
110: public void println(boolean b) throws IOException {
111: print(b);
112: println();
113: }
114:
115: public void println(char c) throws IOException {
116: print(c);
117: println();
118: }
119:
120: public void println(int i) throws IOException {
121: print(i);
122: println();
123: }
124:
125: public void println(long l) throws IOException {
126: print(l);
127: println();
128: }
129:
130: public void println(float f) throws IOException {
131: print(f);
132: println();
133: }
134:
135: public void println(double d) throws IOException {
136: print(d);
137: println();
138: }
139:
140: public void println(char[] ch) throws IOException {
141: print(ch);
142: println();
143: }
144:
145: public void println(String s) throws IOException {
146: print(s);
147: println();
148: }
149:
150: public void println(Object obj) throws IOException {
151: print(obj);
152: println();
153: }
154:
155: public void write(char[] ch, int off, int len) throws IOException {
156: if (closed)
157: throw new IOException("JspWriter closed");
158: if (written + len > getBufferSize()) {
159: if (autoFlush) {
160: flush();
161: w.reset();
162: flushed = true;
163: } else {
164: throw new IOException("JspWriter: buffer full");
165: }
166: }
167: w.write(ch, off, len);
168: }
169:
170: public void clear() throws IOException {
171: w.reset();
172: }
173:
174: public void clearBuffer() throws IOException {
175: if (flushed)
176: throw new IOException("JspWriter already flushed");
177: w.reset();
178: }
179:
180: public void flush() throws IOException {
181: w.writeTo(out);
182: }
183:
184: public void close() throws IOException {
185: if (!closed) {
186: flush();
187: closed = true;
188: }
189: }
190:
191: public int getRemaining() {
192: if (flushed)
193: return 0;
194: return getBufferSize() - w.size();
195: }
196: }
|