1 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 ; 38 import java.io.InputStream ; 39 import java.io.Reader ; 40 import java.io.Writer ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 public class BodyResponseStream extends AbstractResponseStream { 45 static final Logger 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 _out; 55 private String _encoding; 56 57 private BufferInputStream _in; 58 private Reader _encodingReader; 59 60 public BodyResponseStream() 61 { 62 } 63 64 67 public boolean isCauchoResponseStream() 68 { 69 return true; 70 } 71 72 public void setWriter(Writer out) 73 { 74 _out = out; 75 _encoding = null; 76 _encodingReader = null; 77 } 78 79 public void setEncoding(String encoding) 80 { 81 _encoding = encoding; 82 } 83 84 87 public void setBufferSize(int size) 88 { 89 } 90 91 94 public int getBufferSize() 95 { 96 return SIZE; 97 } 98 99 102 public int getRemaining() 103 { 104 return SIZE; 105 } 106 107 110 public char []getCharBuffer() 111 { 112 return _charBuffer; 113 } 114 115 118 public int getCharOffset() 119 { 120 return 0; 121 } 122 123 126 public void setCharOffset(int offset) 127 { 128 if (offset > 0) { 129 try { 130 _out.write(_charBuffer, 0, offset); 131 } catch (IOException e) { 132 log.log(Level.FINER, e.toString(), e); 133 } 134 } 135 } 136 137 140 public char []nextCharBuffer(int offset) 141 throws IOException 142 { 143 if (offset > 0) 144 _out.write(_charBuffer, 0, offset); 145 146 return _charBuffer; 147 } 148 149 156 public void print(char []buf, int offset, int length) 157 throws IOException 158 { 159 if (length == 0) 160 return; 161 162 _out.write(buf, offset, length); 163 } 164 165 172 public void print(int ch) 173 throws IOException 174 { 175 _out.write(ch); 176 } 177 178 181 public int getBufferOffset() 182 { 183 return 0; 184 } 185 186 189 public void setBufferOffset(int offset) 190 { 191 if (offset > 0) { 192 try { 193 write(_byteBuffer, 0, offset); 194 } catch (IOException e) { 195 log.log(Level.FINE, e.toString(), e); 196 } 197 } 198 } 199 200 203 public byte []getBuffer() 204 { 205 return _byteBuffer; 206 } 207 208 211 public byte []nextBuffer(int offset) 212 throws IOException 213 { 214 if (offset > 0) 215 write(_byteBuffer, 0, offset); 216 217 return _byteBuffer; 218 } 219 220 227 public void write(byte []buf, int offset, int length) 228 throws IOException 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 261 public void write(int ch) 262 throws IOException 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 287 public void flushBuffer() 288 throws IOException 289 { 290 } 291 292 295 public void clearBuffer() 296 { 297 } 298 299 static class BufferInputStream extends InputStream { 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 |