1 28 29 package com.caucho.vfs; 30 31 import java.io.IOException ; 32 import java.io.InputStream ; 33 import java.io.OutputStream ; 34 import java.net.Socket ; 35 36 42 public class SocketStream extends StreamImpl { 43 private static byte []UNIX_NEWLINE = new byte[] { (byte) '\n' }; 44 45 private Socket _s; 46 private InputStream _is; 47 private OutputStream _os; 48 private boolean _needsFlush; 49 private byte []_newline = UNIX_NEWLINE; 50 51 private boolean _throwReadInterrupts = false; 52 private int _socketTimeout; 53 54 private long _totalReadBytes; 55 private long _totalWriteBytes; 56 57 public SocketStream() 58 { 59 } 60 61 public SocketStream(Socket s) 62 { 63 init(s); 64 } 65 66 71 public void init(Socket s) 72 { 73 _s = s; 74 75 _is = null; 76 _os = null; 77 _needsFlush = false; 78 } 79 80 85 public void init(InputStream is, OutputStream os) 86 { 87 _is = is; 88 _os = os; 89 _needsFlush = false; 90 } 91 92 96 public void setThrowReadInterrupts(boolean allowThrow) 97 { 98 _throwReadInterrupts = allowThrow; 99 } 100 101 105 public boolean getThrowReadInterrupts() 106 { 107 return _throwReadInterrupts; 108 } 109 110 public void setNewline(byte []newline) 111 { 112 _newline = newline; 113 } 114 115 public byte []getNewline() 116 { 117 return _newline; 118 } 119 120 123 public boolean canRead() 124 { 125 return _is != null || _s != null; 126 } 127 128 137 public int read(byte []buf, int offset, int length) throws IOException 138 { 139 try { 140 if (_is == null) { 141 if (_s == null) 142 return -1; 143 144 _is = _s.getInputStream(); 145 } 146 147 int readLength = _is.read(buf, offset, length); 148 149 if (readLength >= 0) 150 _totalReadBytes += readLength; 151 152 return readLength; 153 } catch (IOException e) { 154 if (_throwReadInterrupts) 155 throw e; 156 157 try { 158 close(); 159 } catch (IOException e1) { 160 } 161 162 return -1; 163 } 164 } 165 166 175 public int readTimeout(byte []buf, int offset, int length, long timeout) 176 throws IOException 177 { 178 Socket s = _s; 179 180 if (s == null) 181 return -1; 182 183 int oldTimeout = s.getSoTimeout();; 184 185 try { 186 s.setSoTimeout((int) timeout); 187 188 return read(buf, offset, length); 189 } finally { 190 s.setSoTimeout(oldTimeout); 191 } 192 } 193 194 197 public int getAvailable() throws IOException 198 { 199 if (_is == null) { 200 if (_s == null) 201 return -1; 202 203 _is = _s.getInputStream(); 204 } 205 206 return _is.available(); 207 } 208 209 public boolean canWrite() 210 { 211 return _os != null || _s != null; 212 } 213 214 224 public void write(byte []buf, int offset, int length, boolean isEnd) 225 throws IOException 226 { 227 if (_os == null) { 228 if (_s == null) 229 return; 230 231 _os = _s.getOutputStream(); 232 } 233 234 try { 235 _needsFlush = true; 236 _os.write(buf, offset, length); 237 _totalWriteBytes += length; 238 } catch (IOException e) { 239 try { 240 close(); 241 } catch (IOException e1) { 242 } 243 244 throw ClientDisconnectException.create(e); 245 } 246 } 247 248 251 public void flush() throws IOException 252 { 253 if (_os == null || ! _needsFlush) 254 return; 255 256 _needsFlush = false; 257 try { 258 _os.flush(); 259 } catch (IOException e) { 260 try { 261 close(); 262 } catch (IOException e1) { 263 } 264 265 throw ClientDisconnectException.create(e); 266 } 267 } 268 269 public void resetTotalBytes() 270 { 271 _totalReadBytes = 0; 272 _totalWriteBytes = 0; 273 } 274 275 public long getTotalReadBytes() 276 { 277 return _totalReadBytes; 278 } 279 280 public long getTotalWriteBytes() 281 { 282 return _totalWriteBytes; 283 } 284 285 288 public void closeWrite() throws IOException 289 { 290 OutputStream os = _os; 291 _os = null; 292 293 if (_s != null) 296 _s.shutdownOutput(); 297 301 } 302 303 306 public void close() throws IOException 307 { 308 Socket s = _s; 309 _s = null; 310 311 OutputStream os = _os; 312 _os = null; 313 314 InputStream is = _is; 315 _is = null; 316 317 try { 318 if (os != null) 319 os.close(); 320 321 if (is != null) 322 is.close(); 323 } finally { 324 if (s != null) 325 s.close(); 326 } 327 } 328 } 329 330 | Popular Tags |