1 29 30 package com.caucho.quercus.resources; 31 32 import com.caucho.quercus.QuercusException; 33 import com.caucho.quercus.env.Env; 34 import com.caucho.quercus.env.StringValue; 35 import com.caucho.quercus.env.StringValueImpl; 36 import com.caucho.vfs.ReadStream; 37 import com.caucho.vfs.WriteStream; 38 39 import java.io.IOException ; 40 41 44 public class StreamReadWrite extends StreamResource { 45 private ReadStream _is; 46 private WriteStream _os; 47 48 public StreamReadWrite(Env env) 49 { 50 env.addClose(this); 51 } 52 53 public StreamReadWrite(Env env, ReadStream is, WriteStream os) 54 { 55 this(env); 56 57 init(is, os); 58 } 59 60 protected void init(ReadStream is, WriteStream os) 61 { 62 _is = is; 63 _os = os; 64 } 65 66 69 public int read() 70 throws IOException 71 { 72 if (_is != null) 73 return _is.read(); 74 else 75 return -1; 76 } 77 78 81 public int read(byte []buffer, int offset, int length) 82 throws IOException 83 { 84 if (_is != null) 85 return _is.read(buffer, offset, length); 86 else 87 return -1; 88 } 89 90 93 public boolean readOptionalLinefeed() 94 throws IOException 95 { 96 if (_is != null) { 97 int ch = _is.read(); 98 99 if (ch == '\n') { 100 return true; 101 } 102 else { 103 _is.unread(); 104 return false; 105 } 106 } 107 else 108 return false; 109 } 110 111 114 public StringValue readLine() 115 throws IOException 116 { 117 if (_is != null) 118 return new StringValueImpl(_is.readLineNoChop()); 119 else 120 return StringValueImpl.EMPTY; 121 } 122 123 126 public int readLine(char []buffer) 127 { 128 try { 129 if (_is != null) 130 return _is.readLine(buffer, buffer.length, false); 131 else 132 return -1; 133 } catch (IOException e) { 134 return -1; 135 } 136 } 137 138 141 public int write(byte []buffer, int offset, int length) 142 throws IOException 143 { 144 if (_os != null) { 145 _os.write(buffer, offset, length); 146 147 return length; 148 } else { 149 return -1; 150 } 151 } 152 153 156 public void print(char ch) 157 throws IOException 158 { 159 print(String.valueOf(ch)); 160 } 161 162 165 public void print(String s) 166 throws IOException 167 { 168 if (_os != null) 169 _os.print(s); 170 } 171 172 175 public boolean isEOF() 176 { 177 return true; 178 } 179 180 183 public void flush() 184 { 185 try { 186 if (_os != null) 187 _os.flush(); 188 } catch (IOException e) { 189 throw new QuercusException(e); 190 } 191 } 192 193 196 public long getPosition() 197 { 198 return 0; 199 } 200 201 204 public void closeRead() 205 { 206 ReadStream is = _is; 207 _is = null; 208 209 if (is != null) 210 is.close(); 211 } 212 213 216 public void closeWrite() 217 { 218 WriteStream os = _os; 219 _os = null; 220 221 try { 222 if (os != null) 223 os.close(); 224 } catch (IOException e) { 225 } 226 } 227 228 231 public void close() 232 { 233 ReadStream is = _is; 234 _is = null; 235 236 WriteStream os = _os; 237 _os = null; 238 239 if (is != null) 240 is.close(); 241 242 try { 243 if (os != null) 244 os.close(); 245 } catch (IOException e) { 246 } 247 } 248 } 249 250 | Popular Tags |