KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > TempStream


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.vfs;
30
31 import java.io.IOException JavaDoc;
32
33 public class TempStream extends StreamImpl {
34   private String JavaDoc _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   /**
54    * Initializes the temp stream for writing.
55    */

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 JavaDoc 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 JavaDoc
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   /**
104    * Sets the encoding.
105    */

106   public void setEncoding(String JavaDoc encoding)
107   {
108     _encoding = encoding;
109   }
110
111   /**
112    * Gets the encoding.
113    */

114   public String JavaDoc 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 JavaDoc
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 JavaDoc
170   {
171     if (_backingStream != null)
172       _backingStream.flush();
173   }
174
175   public void close()
176     throws IOException JavaDoc
177   {
178     if (_backingStream != null) {
179       _backingStream.close();
180       _backingStream = null;
181     }
182
183     super.close();
184   }
185
186   /**
187    * Opens a read stream to the buffer.
188    */

189   public ReadStream openRead()
190     throws IOException JavaDoc
191   {
192     return openRead(false);
193   }
194
195   /**
196    * Opens a read stream to the buffer.
197    *
198    * @param free if true, frees the buffer as it's read
199    */

200   public ReadStream openRead(boolean free)
201     throws IOException JavaDoc
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   /**
220    * Opens a read stream to the buffer.
221    *
222    * @param free if true, frees the buffer as it's read
223    */

224   public void openRead(ReadStream rs, boolean free)
225     throws IOException JavaDoc
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   /**
253    * Returns the head buffer.
254    */

255   public TempBuffer getHead()
256   {
257     return _head;
258   }
259
260   /**
261    * Returns the total length of the buffer's bytes
262    */

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 JavaDoc
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 JavaDoc 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 JavaDoc e) {
319       }
320     }
321     _useBackingFile = false;
322   }
323
324   /**
325    * Copies the temp stream;
326    */

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   /**
349    * Clean up the temp stream.
350    */

351   public void destroy()
352   {
353     try {
354       close();
355     } catch (IOException JavaDoc e) {
356     }
357
358     try {
359       TempFile tempBackingFile = _tempBackingFile;
360       _tempBackingFile = tempBackingFile;
361       
362       if (tempBackingFile != null)
363     tempBackingFile.remove();
364     } catch (Throwable JavaDoc e) {
365     }
366
367     TempBuffer ptr = _head;
368     
369     _head = null;
370     _tail = null;
371
372     TempBuffer.freeAll(ptr);
373   }
374 }
375
Popular Tags