1 28 29 package com.caucho.vfs; 30 31 import java.io.IOException ; 32 33 public class TempReadStream extends StreamImpl { 34 private TempBuffer _cursor; 35 36 private int _offset; 37 private Path _backingDir; 38 private Path _backingFile; 39 private ReadStream _backingStream; 40 private boolean _freeWhenDone = true; 41 42 public TempReadStream(TempBuffer cursor) 43 { 44 init(cursor, null); 45 } 46 47 public TempReadStream() 48 { 49 } 50 51 TempReadStream(TempBuffer cursor, Path path) 52 { 53 init(cursor, path); 54 } 55 56 public void init(TempBuffer cursor) 57 { 58 init(cursor, null); 59 } 60 61 public void init(TempBuffer cursor, Path path) 62 { 63 setPath(path); 64 _cursor = cursor; 65 _offset = 0; 66 _freeWhenDone = true; 67 _backingFile = null; 68 _backingStream = null; 69 } 70 71 public void setFreeWhenDone(boolean free) 72 { 73 _freeWhenDone = free; 74 } 75 76 public boolean canRead() { return true; } 77 78 public int read(byte []buf, int offset, int length) throws IOException 80 { 81 if (_cursor == null) 82 return -1; 83 84 if (_cursor._length - _offset < length) 85 length = _cursor._length - _offset; 86 87 System.arraycopy(_cursor._buf, _offset, buf, offset, length); 88 89 if (_offset + length >= _cursor._length) { 90 TempBuffer next = _cursor._next; 91 if (_freeWhenDone) 92 TempBuffer.free(_cursor); 93 _cursor = next; 94 _offset = 0; 95 } 96 else 97 _offset += length; 98 99 return length > 0 ? length : -1; 100 } 101 102 public int getAvailable() throws IOException 103 { 104 if (_cursor != null) 105 return _cursor._length - _offset; 106 else 107 return 0; 108 } 109 110 public void close() 111 throws IOException 112 { 113 if (_freeWhenDone && _cursor != null) 114 TempBuffer.freeAll(_cursor); 115 116 _cursor = null; 117 } 118 119 public String toString() 120 { 121 return "TempReadStream[]"; 122 } 123 } 124 | Popular Tags |