Quadcap Embeddable Server

com/quadcap/http/servlets/jsp/JspWriter.java

Go to the documentation of this file.
00001 package com.quadcap.http.servlets.jsp; 00002 00003 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.CharArrayWriter; 00042 import java.io.IOException; 00043 import java.io.PrintWriter; 00044 import java.io.Writer; 00045 00046 /** 00047 * The JspWriter implementation. We handle 'bufSize' and 'autoCommit' 00048 * in this class. 00049 * 00050 * @author Stan Bailes 00051 */ 00052 public class JspWriter extends javax.servlet.jsp.JspWriter { 00053 CharArrayWriter w; 00054 Writer out; 00055 int written = 0; 00056 boolean flushed = false; 00057 boolean closed = false; 00058 static String lineSep = System.getProperty("line.separator"); 00059 00060 public JspWriter(Writer out, int bufferSize, boolean autoFlush) { 00061 super(bufferSize, autoFlush); 00062 this.out = out; 00063 w = new CharArrayWriter(); 00064 } 00065 00066 public void newLine() throws IOException { 00067 print(lineSep); 00068 } 00069 00070 public void print(boolean b) throws IOException { 00071 print(b ? "true" : "false"); 00072 } 00073 00074 public void print(char c) throws IOException { 00075 print(String.valueOf(c)); 00076 } 00077 00078 public void print(int i) throws IOException { 00079 print(String.valueOf(i)); 00080 } 00081 00082 public void print(long l) throws IOException { 00083 print(String.valueOf(l)); 00084 } 00085 00086 public void print(float f) throws IOException { 00087 print(String.valueOf(f)); 00088 } 00089 00090 public void print(double d) throws IOException { 00091 print(String.valueOf(d)); 00092 } 00093 00094 public void print(char[] ch) throws IOException { 00095 write(ch, 0, ch.length); 00096 } 00097 00098 public void print(String s) throws IOException { 00099 w.write(s, 0, s.length()); 00100 } 00101 00102 public void print(Object obj) throws IOException { 00103 print(obj.toString()); 00104 } 00105 00106 public void println() throws IOException { 00107 print(lineSep); 00108 } 00109 00110 public void println(boolean b) throws IOException { 00111 print(b); 00112 println(); 00113 } 00114 00115 public void println(char c) throws IOException { 00116 print(c); 00117 println(); 00118 } 00119 00120 public void println(int i) throws IOException { 00121 print(i); 00122 println(); 00123 } 00124 00125 public void println(long l) throws IOException { 00126 print(l); 00127 println(); 00128 } 00129 00130 public void println(float f) throws IOException { 00131 print(f); 00132 println(); 00133 } 00134 00135 public void println(double d) throws IOException { 00136 print(d); 00137 println(); 00138 } 00139 00140 public void println(char[] ch) throws IOException { 00141 print(ch); 00142 println(); 00143 } 00144 00145 public void println(String s) throws IOException { 00146 print(s); 00147 println(); 00148 } 00149 00150 public void println(Object obj) throws IOException { 00151 print(obj); 00152 println(); 00153 } 00154 00155 public void write(char[] ch, int off, int len) throws IOException { 00156 if (closed) throw new IOException("JspWriter closed"); 00157 if (written + len > getBufferSize()) { 00158 if (autoFlush) { 00159 flush(); 00160 w.reset(); 00161 flushed = true; 00162 } else { 00163 throw new IOException("JspWriter: buffer full"); 00164 } 00165 } 00166 w.write(ch, off, len); 00167 } 00168 00169 public void clear() throws IOException { 00170 w.reset(); 00171 } 00172 00173 public void clearBuffer() throws IOException { 00174 if (flushed) throw new IOException("JspWriter already flushed"); 00175 w.reset(); 00176 } 00177 00178 public void flush() throws IOException { 00179 w.writeTo(out); 00180 } 00181 00182 public void close() throws IOException { 00183 if (!closed) { 00184 flush(); 00185 closed = true; 00186 } 00187 } 00188 00189 public int getRemaining() { 00190 if (flushed) return 0; 00191 return getBufferSize() - w.size(); 00192 } 00193 } 00194