KickJava   Java API By Example, From Geeks To Geeks.

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


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 TempCharStream extends StreamImpl {
34   private TempCharBuffer _head;
35   private TempCharBuffer _tail;
36
37   public TempCharStream()
38   {
39   }
40
41   /**
42    * Initializes the temp stream for writing.
43    */

44   public void openWrite()
45   {
46     TempCharBuffer ptr = _head;
47
48     _head = null;
49     _tail = null;
50
51     TempCharBuffer.freeAll(ptr);
52   }
53
54   /**
55    * Returns the tail buffer.
56    */

57   public char []getTail()
58   {
59     return _tail.getBuffer();
60   }
61
62   /**
63    * Returns the head.
64    */

65   public TempCharBuffer getHead()
66   {
67     return _head;
68   }
69
70   /**
71    * Returns true since the temp stream can write.
72    */

73   public boolean canWrite()
74   {
75     return true;
76   }
77
78   public void write(char []buf, int offset, int length)
79     throws IOException JavaDoc
80   {
81     int index = 0;
82     while (index < length) {
83       if (_tail == null)
84     addBuffer(TempCharBuffer.allocate());
85       else if (_tail._buf.length <= _tail._length) {
86     addBuffer(TempCharBuffer.allocate());
87
88     // XXX: see TempStream for backing files
89
}
90
91       int sublen = _tail._buf.length - _tail._length;
92       if (length - index < sublen)
93     sublen = length - index;
94
95       System.arraycopy(buf, index + offset, _tail._buf, _tail._length, sublen);
96
97       index += sublen;
98       _tail._length += sublen;
99     }
100   }
101
102   /**
103    * Writes part of a string.
104    */

105   public void write(String JavaDoc s, int offset, int length)
106     throws IOException JavaDoc
107   {
108     while (length > 0) {
109       if (_tail == null)
110     addBuffer(TempCharBuffer.allocate());
111       else if (_tail._buf.length <= _tail._length) {
112     addBuffer(TempCharBuffer.allocate());
113
114     // XXX: see TempStream for backing files
115
}
116
117       int sublen = _tail._buf.length - _tail._length;
118       if (length < sublen)
119     sublen = length;
120
121       s.getChars(offset, offset + sublen, _tail._buf, _tail._length);
122
123       offset += sublen;
124       length -= sublen;
125       _tail._length += sublen;
126     }
127   }
128
129   public void write(int ch)
130     throws IOException JavaDoc
131   {
132     if (_tail == null)
133       addBuffer(TempCharBuffer.allocate());
134     else if (_tail._buf.length <= _tail._length) {
135       addBuffer(TempCharBuffer.allocate());
136
137       // XXX: see TempStream for backing files
138
}
139
140     _tail._buf[_tail._length++] = (char) ch;
141   }
142
143   private void addBuffer(TempCharBuffer buf)
144   {
145     buf._next = null;
146     if (_tail != null) {
147       _tail._next = buf;
148       _tail = buf;
149     } else {
150       _tail = buf;
151       _head = buf;
152     }
153
154     _head._bufferCount++;
155   }
156
157   public void flush()
158     throws IOException JavaDoc
159   {
160   }
161
162   public void close()
163     throws IOException JavaDoc
164   {
165     super.close();
166   }
167
168   /**
169    * Returns the total length of the buffer's bytes
170    */

171   public int getLength()
172   {
173     int length = 0;
174     
175     for (TempCharBuffer ptr = _head; ptr != null; ptr = ptr._next) {
176       length += ptr.getLength();
177     }
178
179     return length;
180   }
181
182   public void clearWrite()
183   {
184     TempCharBuffer ptr = _head;
185
186     _head = null;
187     _tail = null;
188
189     TempCharBuffer.freeAll(ptr);
190   }
191
192   public void discard()
193   {
194     _head = null;
195     _tail = null;
196   }
197
198   /**
199    * Clean up the temp stream.
200    */

201   public void destroy()
202   {
203     try {
204       close();
205     } catch (IOException JavaDoc e) {
206     }
207
208     TempCharBuffer ptr = _head;
209     
210     _head = null;
211     _tail = null;
212
213     TempCharBuffer.freeAll(ptr);
214   }
215 }
216
Popular Tags