1 29 30 package com.caucho.server.connection; 31 32 import com.caucho.log.Log; 33 import com.caucho.util.L10N; 34 import com.caucho.vfs.AbstractPrintWriter; 35 36 import java.io.IOException ; 37 import java.util.logging.Level ; 38 import java.util.logging.Logger ; 39 40 public class ResponseWriter extends AbstractPrintWriter { 41 static final Logger log = Log.open(ResponseWriter.class); 42 static final L10N L = new L10N(ResponseWriter.class); 43 44 private AbstractResponseStream _out; 45 private boolean _hasError; 46 47 public ResponseWriter() 48 { 49 } 50 51 ResponseWriter(AbstractResponseStream out) 52 { 53 _out = out; 54 } 55 56 public void init(AbstractResponseStream out) 57 { 58 _out = out; 59 _hasError = false; 60 } 61 62 public int getBufferSize() 63 { 64 return _out.getBufferSize(); 65 } 66 67 70 public void setBufferSize(int size) 71 { 72 _out.setBufferSize(size); 73 } 74 75 public int getRemaining() 76 { 77 return _out.getRemaining(); 78 } 79 80 83 public boolean checkError() 84 { 85 return _hasError; 86 } 87 88 91 public void clearBuffer() 92 { 93 _out.clearBuffer(); 94 } 95 96 101 final public void write(int ch) 102 { 103 try { 104 _out.print(ch); 105 } catch (IOException e) { 106 _hasError = true; 107 log.log(Level.FINE, e.toString(), e); 108 } 109 } 110 111 118 final public void write(char []buf, int offset, int length) 119 { 120 try { 121 _out.print(buf, offset, length); 122 } catch (IOException e) { 123 _hasError = true; 124 log.log(Level.FINE, e.toString(), e); 125 } 126 } 127 128 131 final public void write(String s, int off, int len) 132 { 133 try { 134 char []writeBuffer = _out.getCharBuffer(); 135 int size = writeBuffer.length; 136 int writeLength = _out.getCharOffset(); 137 int end = off + len; 138 139 while (off < end) { 140 int sublen = end - off; 141 142 if (size - writeLength < sublen) { 143 if (size == writeLength) { 144 writeBuffer = _out.nextCharBuffer(writeLength); 145 writeLength = 0; 146 147 if (size < sublen) 148 sublen = size; 149 } 150 else 151 sublen = size - writeLength; 152 } 153 154 int tail = off + sublen; 155 s.getChars(off, tail, writeBuffer, writeLength); 156 157 off = tail; 158 writeLength += sublen; 159 } 160 161 _out.setCharOffset(writeLength); 162 } catch (IOException e) { 163 _hasError = true; 164 log.log(Level.FINE, e.toString(), e); 165 } 166 } 167 168 171 public void flush() 172 { 173 try { 174 _out.flushChar(); 175 } catch (IOException e) { 176 _hasError = true; 177 log.log(Level.FINE, e.toString(), e); 178 } 179 } 180 181 186 public void flushBuffer() 187 { 188 try { 189 _out.flushBuffer(); 190 } catch (IOException e) { 191 _hasError = true; 192 log.log(Level.FINE, e.toString(), e); 193 } 194 } 195 196 public void close() 197 { 198 _hasError = false; 199 200 209 210 217 } 218 } 219 | Popular Tags |