KickJava   Java API By Example, From Geeks To Geeks.

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


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 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   // XXX: any way to make this automatically free?
79
public int read(byte []buf, int offset, int length) throws IOException JavaDoc
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 JavaDoc
103   {
104     if (_cursor != null)
105       return _cursor._length - _offset;
106     else
107       return 0;
108   }
109
110   public void close()
111     throws IOException JavaDoc
112   {
113     if (_freeWhenDone && _cursor != null)
114       TempBuffer.freeAll(_cursor);
115     
116     _cursor = null;
117   }
118
119   public String JavaDoc toString()
120   {
121     return "TempReadStream[]";
122   }
123 }
124
Popular Tags