1 28 29 package com.caucho.vfs; 30 31 import java.io.IOException ; 32 33 public class TempStream extends StreamImpl { 34 private String _encoding; 35 private TempBuffer _head; 36 private TempBuffer _tail; 37 private Path _backingDir; 38 private Path _backingFile; 39 private TempFile _tempBackingFile; 40 private WriteStream _backingStream; 41 private boolean _useBackingFile; 42 private TempReadStream _tempReadStream; 43 44 public TempStream(Path backingDir) 45 { 46 _backingDir = backingDir; 47 } 48 49 public TempStream() 50 { 51 } 52 53 56 public void openWrite() 57 { 58 TempBuffer ptr = _head; 59 60 _head = null; 61 _tail = null; 62 63 _encoding = null; 64 65 TempBuffer.freeAll(ptr); 66 67 _useBackingFile = false; 68 69 if (_backingStream != null) { 70 try { 71 _backingStream.close(); 72 } catch (IOException e) { 73 } 74 _backingStream = null; 75 } 76 } 77 78 public byte []getTail() 79 { 80 return _tail.getBuffer(); 81 } 82 83 public void changeToBackingFile(int index) 84 throws IOException 85 { 86 if (_backingFile == null) { 87 _backingFile = _backingDir.createTempFile("tmp", ".tmp"); 88 _tempBackingFile = new TempFile(_backingFile); 89 } 90 91 _backingStream = _backingFile.openWrite(); 92 _useBackingFile = true; 93 94 TempBuffer next; 95 for (; _head != null; _head = next) { 96 next = _head._next; 97 98 _backingStream.write(_head._buf, 0, _head._length); 99 TempBuffer.free(_head); 100 } 101 } 102 103 106 public void setEncoding(String encoding) 107 { 108 _encoding = encoding; 109 } 110 111 114 public String getEncoding() 115 { 116 return _encoding; 117 } 118 119 public boolean canWrite() { return true; } 120 121 public void write(byte []buf, int offset, int length, boolean isEnd) 122 throws IOException 123 { 124 if (_backingStream != null) { 125 _backingStream.write(buf, offset, length); 126 } 127 else { 128 int index = 0; 129 while (index < length) { 130 if (_tail == null) 131 addBuffer(TempBuffer.allocate()); 132 else if (_tail._length >= _tail._buf.length) { 133 if (_head._bufferCount < 8 || _backingDir == null) 134 addBuffer(TempBuffer.allocate()); 135 else { 136 changeToBackingFile(index); 137 _backingStream.write(buf, offset, length); 138 return; 139 } 140 } 141 142 int sublen = _tail._buf.length - _tail._length; 143 if (length - index < sublen) 144 sublen = length - index; 145 146 System.arraycopy(buf, index + offset, _tail._buf, _tail._length, sublen); 147 148 index += sublen; 149 _tail._length += sublen; 150 } 151 } 152 } 153 154 private void addBuffer(TempBuffer buf) 155 { 156 buf._next = null; 157 if (_tail != null) { 158 _tail._next = buf; 159 _tail = buf; 160 } else { 161 _tail = buf; 162 _head = buf; 163 } 164 165 _head._bufferCount++; 166 } 167 168 public void flush() 169 throws IOException 170 { 171 if (_backingStream != null) 172 _backingStream.flush(); 173 } 174 175 public void close() 176 throws IOException 177 { 178 if (_backingStream != null) { 179 _backingStream.close(); 180 _backingStream = null; 181 } 182 183 super.close(); 184 } 185 186 189 public ReadStream openRead() 190 throws IOException 191 { 192 return openRead(false); 193 } 194 195 200 public ReadStream openRead(boolean free) 201 throws IOException 202 { 203 close(); 204 205 if (_useBackingFile) 206 return _backingFile.openRead(); 207 else { 208 TempReadStream read = new TempReadStream(_head); 209 read.setFreeWhenDone(free); 210 if (free) { 211 _head = null; 212 _tail = null; 213 } 214 read.setPath(getPath()); 215 return new ReadStream(read); 216 } 217 } 218 219 224 public void openRead(ReadStream rs, boolean free) 225 throws IOException 226 { 227 close(); 228 229 if (_useBackingFile) { 230 StreamImpl impl = _backingFile.openReadImpl(); 231 232 rs.init(impl, null); 233 } 234 else { 235 if (_tempReadStream == null) { 236 _tempReadStream = new TempReadStream(); 237 _tempReadStream.setPath(getPath()); 238 } 239 240 _tempReadStream.init(_head); 241 242 _tempReadStream.setFreeWhenDone(free); 243 if (free) { 244 _head = null; 245 _tail = null; 246 } 247 248 rs.init(_tempReadStream, null); 249 } 250 } 251 252 255 public TempBuffer getHead() 256 { 257 return _head; 258 } 259 260 263 public int getLength() 264 { 265 int length = 0; 266 267 for (TempBuffer ptr = _head; ptr != null; ptr = ptr._next) { 268 length += ptr.getLength(); 269 } 270 271 return length; 272 } 273 274 public ReadStream openRead(ReadStream s) 275 throws IOException 276 { 277 close(); 278 279 if (_useBackingFile) 280 return _backingFile.openRead(); 281 else { 282 TempReadStream read = new TempReadStream(_head); 283 read.setFreeWhenDone(false); 284 read.setPath(getPath()); 285 s.init(read, null); 286 return s; 287 } 288 } 289 290 public void clearWrite() 291 { 292 TempBuffer ptr = _head; 293 294 _head = null; 295 _tail = null; 296 297 TempBuffer.freeAll(ptr); 298 299 if (_backingStream != null) { 300 try { 301 _backingStream.close(); 302 _backingStream = _backingFile.openWrite(); 303 } catch (Exception e) { 304 } 305 } 306 _useBackingFile = false; 307 } 308 309 public void discard() 310 { 311 _head = null; 312 _tail = null; 313 314 if (_backingStream != null) { 315 try { 316 _backingStream.close(); 317 _backingStream = _backingFile.openWrite(); 318 } catch (Exception e) { 319 } 320 } 321 _useBackingFile = false; 322 } 323 324 327 public TempStream copy() 328 { 329 TempStream newStream = new TempStream(); 330 331 TempBuffer ptr = _head; 332 333 for (; ptr != null; ptr = ptr.getNext()) { 334 TempBuffer newPtr = TempBuffer.allocate(); 335 336 if (newStream._tail != null) 337 newStream._tail.setNext(newPtr); 338 else 339 newStream._head = newPtr; 340 newStream._tail = newPtr; 341 342 newPtr.write(ptr.getBuffer(), 0, ptr.getLength()); 343 } 344 345 return newStream; 346 } 347 348 351 public void destroy() 352 { 353 try { 354 close(); 355 } catch (IOException e) { 356 } 357 358 try { 359 TempFile tempBackingFile = _tempBackingFile; 360 _tempBackingFile = tempBackingFile; 361 362 if (tempBackingFile != null) 363 tempBackingFile.remove(); 364 } catch (Throwable e) { 365 } 366 367 TempBuffer ptr = _head; 368 369 _head = null; 370 _tail = null; 371 372 TempBuffer.freeAll(ptr); 373 } 374 } 375 | Popular Tags |