KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jsp;
30
31 import com.caucho.log.Log;
32 import com.caucho.server.connection.AbstractResponseStream;
33 import com.caucho.util.L10N;
34 import com.caucho.vfs.Encoding;
35 import com.caucho.vfs.TempBuffer;
36
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.io.Reader JavaDoc;
40 import java.io.Writer JavaDoc;
41 import java.util.logging.Level JavaDoc;
42 import java.util.logging.Logger JavaDoc;
43
44 public class BodyResponseStream extends AbstractResponseStream {
45   static final Logger JavaDoc log = Log.open(BodyResponseStream.class);
46   
47   static final L10N L = new L10N(BodyResponseStream.class);
48
49   private static final int SIZE = TempBuffer.SIZE;
50   
51   private final byte []_byteBuffer = new byte[SIZE];
52   private final char []_charBuffer = new char[SIZE];
53   
54   private Writer JavaDoc _out;
55   private String JavaDoc _encoding;
56
57   private BufferInputStream _in;
58   private Reader JavaDoc _encodingReader;
59   
60   public BodyResponseStream()
61   {
62   }
63
64   /**
65    * Returns true for a caucho response stream.
66    */

67   public boolean isCauchoResponseStream()
68   {
69     return true;
70   }
71
72   public void setWriter(Writer JavaDoc out)
73   {
74     _out = out;
75     _encoding = null;
76     _encodingReader = null;
77   }
78
79   public void setEncoding(String JavaDoc encoding)
80   {
81     _encoding = encoding;
82   }
83
84   /**
85    * Sets the buffer size.
86    */

87   public void setBufferSize(int size)
88   {
89   }
90
91   /**
92    * Gets the buffer size.
93    */

94   public int getBufferSize()
95   {
96     return SIZE;
97   }
98
99   /**
100    * Returns the remaining buffer entries.
101    */

102   public int getRemaining()
103   {
104     return SIZE;
105   }
106
107   /**
108    * Returns the char buffer.
109    */

110   public char []getCharBuffer()
111   {
112     return _charBuffer;
113   }
114
115   /**
116    * Returns the char buffer offset.
117    */

118   public int getCharOffset()
119   {
120     return 0;
121   }
122
123   /**
124    * Sets the char buffer offset.
125    */

126   public void setCharOffset(int offset)
127   {
128     if (offset > 0) {
129       try {
130     _out.write(_charBuffer, 0, offset);
131       } catch (IOException JavaDoc e) {
132     log.log(Level.FINER, e.toString(), e);
133       }
134     }
135   }
136
137   /**
138    * Returns the next char buffer.
139    */

140   public char []nextCharBuffer(int offset)
141     throws IOException JavaDoc
142   {
143     if (offset > 0)
144       _out.write(_charBuffer, 0, offset);
145
146     return _charBuffer;
147   }
148
149   /**
150    * Writes the next chunk of data to the response stream.
151    *
152    * @param buf the buffer containing the data
153    * @param offset start offset into the buffer
154    * @param length length of the data in the buffer
155    */

156   public void print(char []buf, int offset, int length)
157     throws IOException JavaDoc
158   {
159     if (length == 0)
160       return;
161
162     _out.write(buf, offset, length);
163   }
164
165   /**
166    * Writes the next chunk of data to the response stream.
167    *
168    * @param buf the buffer containing the data
169    * @param offset start offset into the buffer
170    * @param length length of the data in the buffer
171    */

172   public void print(int ch)
173     throws IOException JavaDoc
174   {
175     _out.write(ch);
176   }
177
178   /**
179    * Returns the buffer offset.
180    */

181   public int getBufferOffset()
182   {
183     return 0;
184   }
185
186   /**
187    * Sets the byte buffer offset.
188    */

189   public void setBufferOffset(int offset)
190   {
191     if (offset > 0) {
192       try {
193     write(_byteBuffer, 0, offset);
194       } catch (IOException JavaDoc e) {
195     log.log(Level.FINE, e.toString(), e);
196       }
197     }
198   }
199
200   /**
201    * Gets the byte buffer
202    */

203   public byte []getBuffer()
204   {
205     return _byteBuffer;
206   }
207
208   /**
209    * Returns the next buffer.
210    */

211   public byte []nextBuffer(int offset)
212     throws IOException JavaDoc
213   {
214     if (offset > 0)
215       write(_byteBuffer, 0, offset);
216
217     return _byteBuffer;
218   }
219
220   /**
221    * Writes the next chunk of data to the response stream.
222    *
223    * @param buf the buffer containing the data
224    * @param offset start offset into the buffer
225    * @param length length of the data in the buffer
226    */

227   public void write(byte []buf, int offset, int length)
228     throws IOException JavaDoc
229   {
230     if (length == 0)
231       return;
232     
233     if (_encodingReader == null) {
234       if (_in == null)
235     _in = new BufferInputStream();
236       _encodingReader = Encoding.getReadEncoding(_in, _encoding);
237     }
238
239     if (_encodingReader == null) {
240       for (; length > 0; length--) {
241     print((char) (buf[offset++] & 0xff));
242       }
243       return;
244     }
245     
246     _in.init(buf, offset, length);
247
248     int ch;
249     while ((ch = _encodingReader.read()) >= 0) {
250       print(ch);
251     }
252   }
253
254   /**
255    * Writes the next chunk of data to the response stream.
256    *
257    * @param buf the buffer containing the data
258    * @param offset start offset into the buffer
259    * @param length length of the data in the buffer
260    */

261   public void write(int ch)
262     throws IOException JavaDoc
263   {
264     Thread.dumpStack();
265     if (_encodingReader == null) {
266       if (_in == null)
267     _in = new BufferInputStream();
268       _byteBuffer[0] = (byte) ch;
269       _encodingReader = Encoding.getReadEncoding(_in, _encoding);
270     }
271
272     if (_encodingReader == null) {
273       print((char) ch);
274       return;
275     }
276     
277     _in.init(_byteBuffer, 0, 1);
278
279     while ((ch = _encodingReader.read()) >= 0) {
280       print(ch);
281     }
282   }
283
284   /**
285    * Flushes the buffer.
286    */

287   public void flushBuffer()
288     throws IOException JavaDoc
289   {
290   }
291
292   /**
293    * Clears the buffer.
294    */

295   public void clearBuffer()
296   {
297   }
298
299   static class BufferInputStream extends InputStream JavaDoc {
300     private byte []_buffer;
301     private int _offset;
302     private int _length;
303
304     void init(byte []buffer, int offset, int length)
305     {
306       _buffer = buffer;
307       _offset = offset;
308       _length = length;
309     }
310
311     public int read()
312     {
313       if (_offset < _length)
314     return _buffer[_offset++] & 0xff;
315       else
316     return -1;
317     }
318   }
319 }
320
Popular Tags