1 29 30 package com.caucho.quercus.lib.file; 31 32 import com.caucho.quercus.QuercusModuleException; 33 import com.caucho.quercus.env.BinaryBuilderValue; 34 import com.caucho.quercus.env.BinaryValue; 35 import com.caucho.quercus.env.BooleanValue; 36 import com.caucho.quercus.env.StringBuilderValue; 37 import com.caucho.quercus.env.StringValue; 38 import com.caucho.quercus.env.Value; 39 import com.caucho.vfs.ReadStream; 40 import com.caucho.vfs.TempBuffer; 41 import com.caucho.vfs.WriteStream; 42 43 import java.io.Closeable ; 44 import java.io.IOException ; 45 import java.io.InputStream ; 46 import java.io.OutputStream ; 47 import java.io.UnsupportedEncodingException ; 48 import java.util.logging.Level ; 49 import java.util.logging.Logger ; 50 51 54 public class AbstractBinaryInputOutput 55 implements BinaryInput, BinaryOutput, Closeable 56 { 57 private static final Logger log 58 = Logger.getLogger(AbstractBinaryInputOutput.class.getName()); 59 60 private ReadStream _is; 61 private WriteStream _os; 62 63 protected AbstractBinaryInputOutput() 64 { 65 } 66 67 protected AbstractBinaryInputOutput(ReadStream is, WriteStream os) 68 { 69 init(is, os); 70 } 71 72 public void init(ReadStream is, WriteStream os) 73 { 74 _is = is; 75 _os = os; 76 } 77 78 82 85 public InputStream getInputStream() 86 { 87 return _is; 88 } 89 90 93 public BinaryInput openCopy() 94 throws IOException 95 { 96 throw new UnsupportedOperationException (getClass().getName()); 97 } 98 99 public void setEncoding(String encoding) 100 throws UnsupportedEncodingException 101 { 102 if (_is != null) 103 _is.setEncoding(encoding); 104 } 105 106 109 public void unread() 110 throws IOException 111 { 112 if (_is != null) 113 _is.unread(); 114 } 115 116 119 public int read() 120 throws IOException 121 { 122 if (_is != null) 123 return _is.read(); 124 else 125 return -1; 126 } 127 128 131 public int read(byte []buffer, int offset, int length) 132 throws IOException 133 { 134 if (_is != null) { 135 return _is.read(buffer, offset, length); 136 } 137 else 138 return -1; 139 } 140 141 144 public BinaryValue read(int length) 145 throws IOException 146 { 147 if (_is == null) 148 return null; 149 150 BinaryBuilderValue bb = new BinaryBuilderValue(); 151 152 while (length > 0) { 153 bb.prepareReadBuffer(); 154 155 int sublen = bb.getLength() - bb.getOffset(); 156 157 if (length < sublen) 158 sublen = length; 159 160 sublen = read(bb.getBuffer(), bb.getOffset(), sublen); 161 162 if (sublen > 0) { 163 bb.setOffset(bb.getOffset() + sublen); 164 length -= sublen; 165 } 166 else 167 return bb; 168 } 169 170 return bb; 171 } 172 173 176 public boolean readOptionalLinefeed() 177 throws IOException 178 { 179 if (_is == null) 180 return false; 181 182 int ch = _is.read(); 183 184 if (ch == '\n') { 185 return true; 186 } 187 else { 188 _is.unread(); 189 return false; 190 } 191 } 192 193 public void writeToStream(OutputStream os, int length) 194 throws IOException 195 { 196 if (_is != null) { 197 _is.writeToStream(os, length); 198 } 199 } 200 201 204 public StringValue readLine(long length) 205 throws IOException 206 { 207 if (_is == null) 208 return null; 209 210 StringBuilderValue sb = new StringBuilderValue(); 211 212 int ch; 213 214 for (; length > 0 && (ch = _is.readChar()) >= 0; length--) { 215 if (ch == '\n') { 216 sb.append((char) ch); 217 return sb; 218 } 219 else if (ch == '\r') { 220 sb.append('\r'); 221 222 int ch2 = _is.read(); 223 224 if (ch2 == '\n') 225 sb.append('\n'); 226 else 227 _is.unread(); 228 229 return sb; 230 } 231 else 232 sb.append((char) ch); 233 } 234 235 if (sb.length() == 0) 236 return null; 237 else 238 return sb; 239 } 240 241 244 public boolean isEOF() 245 { 246 if (_is == null) 247 return true; 248 else { 249 try { 250 return _is.available() <= 0; 252 } catch (IOException e) { 253 log.log(Level.FINE, e.toString(), e); 254 255 return true; 256 } 257 } 258 } 259 260 263 public long getPosition() 264 { 265 if (_is == null) 266 return -1; 267 else 268 return _is.getPosition(); 269 } 270 271 274 public boolean setPosition(long offset) 275 { 276 if (_is == null) 277 return false; 278 279 try { 280 _is.setPosition(offset); 281 282 return true; 283 } catch (IOException e) { 284 throw new QuercusModuleException(e); 285 } 286 } 287 288 public long seek(long offset, int whence) 289 { 290 switch (whence) { 291 case BinaryInput.SEEK_CUR: 292 offset = getPosition() + offset; 293 break; 294 case BinaryInput.SEEK_END: 295 offset = getPosition(); 297 break; 298 case SEEK_SET: 299 break; 300 default: 301 break; 302 } 303 304 setPosition(offset); 305 306 return offset; 307 } 308 309 public Value stat() 310 { 311 return BooleanValue.FALSE; 312 } 313 314 317 public void closeRead() 318 { 319 ReadStream is = _is; 320 _is = null; 321 322 if (is != null) 323 is.close(); 324 } 325 326 330 333 public OutputStream getOutputStream() 334 { 335 return _os; 336 } 337 338 public void write(int ch) 339 throws IOException 340 { 341 _os.write(ch); 342 } 343 344 public void write(byte []buffer, int offset, int length) 345 throws IOException 346 { 347 _os.write(buffer, offset, length); 348 } 349 350 353 public int write(InputStream is, int length) 354 { 355 int writeLength = 0; 356 357 TempBuffer tb = TempBuffer.allocate(); 358 byte []buffer = tb.getBuffer(); 359 360 try { 361 while (length > 0) { 362 int sublen; 363 364 if (length < buffer.length) 365 sublen = length; 366 else 367 sublen = buffer.length; 368 369 sublen = is.read(buffer, 0, sublen); 370 371 if (sublen < 0) 372 break; 373 374 write(buffer, 0, sublen); 375 376 writeLength += sublen; 377 length -= sublen; 378 } 379 380 return writeLength; 381 } catch (IOException e) { 382 throw new QuercusModuleException(e); 383 } finally { 384 TempBuffer.free(tb); 385 } 386 } 387 388 391 public void print(char v) 392 throws IOException 393 { 394 write((byte) v); 395 } 396 397 400 public void print(String v) 401 throws IOException 402 { 403 _os.print(v); 404 } 405 406 409 public void flush() 410 throws IOException 411 { 412 _os.flush(); 413 } 414 415 416 419 public void closeWrite() 420 { 421 try { 422 WriteStream os = _os; 423 _os = null; 424 425 if (os != null) 426 os.close(); 427 } catch (IOException e) { 428 log.log(Level.FINE, e.toString(), e); 429 } 430 } 431 432 435 public void close() 436 { 437 closeRead(); 438 closeWrite(); 439 } 440 441 public Object toJavaObject() 442 { 443 return this; 444 } 445 446 public String getResourceType() 447 { 448 return "stream"; 449 } 450 451 454 public String toString() 455 { 456 if (_is != null) 457 return "AbstractBinaryInputOutput[" + _is.getPath() + "]"; 458 else 459 return "AbstractBinaryInputOutput[closed]"; 460 } 461 } 462 463 | Popular Tags |