KickJava   Java API By Example, From Geeks To Geeks.

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


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.Reader JavaDoc;
32
33 /**
34  * Char reader based on an underlying buffer.
35  */

36 public class TempCharReader extends Reader JavaDoc {
37   private TempCharBuffer _top;
38   private TempCharBuffer _head;
39   private char []_buffer;
40   private int _offset;
41   private int _length;
42   private boolean _isFree;
43
44   /**
45    * Create a new TempCharReader.
46    */

47   public TempCharReader()
48   {
49   }
50
51   /**
52    * Create a new TempBuffer.
53    */

54   public TempCharReader(TempCharBuffer head)
55   {
56     init(head);
57   }
58
59   /**
60    * Set the reader to free the buffer.
61    */

62   public void setFree(boolean isFree)
63   {
64     _isFree = isFree;
65   }
66
67   /**
68    * Initialize the reader.
69    */

70   public void init(TempCharBuffer head)
71   {
72     _top = head;
73     _head = head;
74
75     if (head != null) {
76       _buffer = head.getBuffer();
77       _length = head.getLength();
78     }
79     else
80       _length = 0;
81
82     _offset = 0;
83   }
84
85   /**
86    * Resets the reader
87    */

88   public void reset()
89   {
90     init(_top);
91   }
92
93   /**
94    * Reads the next character.
95    */

96   public int read()
97   {
98     if (_length <= _offset) {
99       if (_head == null)
100     return -1;
101
102       TempCharBuffer next = _head.getNext();
103       if (_isFree)
104     TempCharBuffer.free(_head);
105       _head = next;
106
107       if (_head == null)
108     return -1;
109       
110       _buffer = _head.getBuffer();
111       _length = _head.getLength();
112       _offset = 0;
113     }
114
115     return _buffer[_offset++];
116   }
117
118   /**
119    * Reads the next character.
120    */

121   public int read(char []buffer, int offset, int length)
122   {
123     int readLength = 0;
124     
125     while (length > 0) {
126       if (_length <= _offset) {
127     if (_head == null)
128       return readLength == 0 ? -1 : readLength;
129
130     TempCharBuffer next = _head.getNext();
131     if (_isFree)
132       TempCharBuffer.free(_head);
133     _head = next;
134
135     if (_head == null)
136       return readLength == 0 ? -1 : readLength;
137       
138     _buffer = _head.getBuffer();
139     _length = _head.getLength();
140     _offset = 0;
141       }
142
143       int sublen = _length - _offset;
144
145       if (length < sublen)
146     sublen = length;
147
148       System.arraycopy(_buffer, _offset, buffer, offset, sublen);
149
150       _offset += sublen;
151       offset += sublen;
152       length -= sublen;
153       readLength += sublen;
154     }
155
156     return readLength;
157   }
158
159   /**
160    * Returns true if it's empty.
161    */

162   public boolean isEmpty()
163   {
164     return _head == null;
165   }
166
167   /**
168    * Closes the reader.
169    */

170   public void close()
171   {
172     if (_isFree)
173       TempCharBuffer.freeAll(_head);
174
175     _head = null;
176     _buffer = null;
177     _offset = 0;
178     _length = 0;
179   }
180 }
181
Popular Tags