KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jsp > JspWriterAdapter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jsp;
31
32 import com.caucho.server.connection.AbstractResponseStream;
33 import com.caucho.util.L10N;
34
35 import javax.servlet.jsp.JspWriter JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.util.logging.Level JavaDoc;
38
39 /**
40  * A buffered JSP writer encapsulating a Writer.
41  */

42 public class JspWriterAdapter extends AbstractBodyContent {
43   private static final L10N L = new L10N(JspWriterAdapter.class);
44   
45   // the parent writer
46
private JspWriter JavaDoc _parent;
47   
48   // the underlying writer
49
private AbstractResponseStream _out;
50   
51   // the page context
52
private PageContextImpl _pageContext;
53
54   private boolean _isClosed;
55
56   /**
57    * Creates a new JspWriterAdapter
58    */

59   JspWriterAdapter()
60   {
61   }
62
63   /**
64    * Initialize the JSP writer
65    *
66    * @param os the underlying stream
67    */

68   void init(PageContextImpl pageContext)
69   {
70     _pageContext = pageContext;
71     _out = null;
72     _isClosed = false;
73   }
74
75   /**
76    * Initialize the JSP writer
77    *
78    * @param os the underlying stream
79    */

80   void init(JspWriter JavaDoc parent, AbstractResponseStream out)
81   {
82     _parent = parent;
83     _out = out;
84     _isClosed = false;
85   }
86
87   /**
88    * Writes a character array to the writer.
89    *
90    * @param buf the buffer to write.
91    * @param off the offset into the buffer
92    * @param len the number of characters to write
93    */

94   final public void write(char []buf, int offset, int length)
95     throws IOException JavaDoc
96   {
97     if (_isClosed)
98       throw new IOException JavaDoc(L.l("write() forbidden after writer is closed"));
99
100     _out.print(buf, offset, length);
101   }
102   
103   /**
104    * Writes a character to the output.
105    *
106    * @param buf the buffer to write.
107    */

108   final public void write(int ch) throws IOException JavaDoc
109   {
110     if (_isClosed) {
111       if (Character.isWhitespace(ch))
112     return;
113       
114       throw new IOException JavaDoc(L.l("write() forbidden after writer is closed"));
115     }
116
117     _out.print(ch);
118   }
119
120   /**
121    * Prints the newline.
122    */

123   final public void println() throws IOException JavaDoc
124   {
125     if (_isClosed) {
126       throw new IOException JavaDoc(L.l("write() forbidden after writer is closed"));
127     }
128
129     _out.print('\n');
130   }
131
132   /**
133    * Writes a subsection of a string to the output.
134    */

135   final public void write(String JavaDoc s, int off, int len)
136     throws IOException JavaDoc
137   {
138     if (_isClosed)
139       throw new IOException JavaDoc(L.l("write() forbidden after writer is closed"));
140
141     char []writeBuffer = _out.getCharBuffer();
142     int size = writeBuffer.length;
143     int writeLength = _out.getCharOffset();
144     int end = off + len;
145
146     while (off < end) {
147       int sublen = end - off;
148
149       if (size - writeLength < sublen) {
150     if (size == writeLength) {
151       writeBuffer = _out.nextCharBuffer(writeLength);
152       writeLength = 0;
153       
154       if (size < sublen)
155         sublen = size;
156     }
157     else
158       sublen = size - writeLength;
159       }
160
161       int tail = off + sublen;
162       s.getChars(off, tail, writeBuffer, writeLength);
163
164       off = tail;
165       writeLength += sublen;
166     }
167
168     _out.setCharOffset(writeLength);
169   }
170
171   /**
172    * Returns the buffer size of the writer.
173    */

174   public int getBufferSize()
175   {
176     return _out.getBufferSize();
177   }
178
179   /**
180    * Returns the remaining bytes in the buffer.
181    */

182   public int getRemaining()
183   {
184     return _out.getRemaining();
185   }
186
187   public void clear() throws IOException JavaDoc
188   {
189     if (_isClosed)
190       throw new IOException JavaDoc(L.l("clear() forbidden after writer is closed"));
191     /*
192     else if (_out.isCommitted()) {
193       // jsp/0502
194       throw new IOException(L.l("clear() forbidden after data is committed"));
195     }
196     */

197
198     _out.clear();
199     // clearBuffer();
200
}
201
202   public void clearBuffer() throws IOException JavaDoc
203   {
204     if (_isClosed)
205       return;
206
207     _out.clearBuffer();
208   }
209
210   public void flushBuffer()
211     throws IOException JavaDoc
212   {
213     if (_isClosed)
214       throw new IOException JavaDoc(L.l("flushBuffer() forbidden after writer is closed"));
215
216     _out.flushBuffer();
217   }
218
219   /**
220    * Flushes the output stream.
221    */

222   public void flush() throws IOException JavaDoc
223   {
224     if (_isClosed)
225       throw new IOException JavaDoc(L.l("flush() forbidden after writer is closed"));
226
227     _out.flushChar();
228   }
229
230   /**
231    * Pops the enclosing writer.
232    */

233   AbstractJspWriter popWriter()
234   {
235     try {
236       close();
237     } catch (Throwable JavaDoc e) {
238       log.log(Level.FINER, e.toString(), e);
239     }
240
241     return super.popWriter();
242   }
243
244   final public void close() throws IOException JavaDoc
245   {
246     _isClosed = true;
247
248     _out = null;
249     _parent = null;
250     _pageContext = null;
251   }
252 }
253
Popular Tags