001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.vfs;
030:
031: import java.io.Reader;
032:
033: /**
034: * Char reader based on an underlying buffer.
035: */
036: public class TempCharReader extends Reader {
037: private TempCharBuffer _top;
038: private TempCharBuffer _head;
039: private char[] _buffer;
040: private int _offset;
041: private int _length;
042: private boolean _isFree;
043:
044: /**
045: * Create a new TempCharReader.
046: */
047: public TempCharReader() {
048: }
049:
050: /**
051: * Create a new TempBuffer.
052: */
053: public TempCharReader(TempCharBuffer head) {
054: init(head);
055: }
056:
057: /**
058: * Set the reader to free the buffer.
059: */
060: public void setFree(boolean isFree) {
061: _isFree = isFree;
062: }
063:
064: /**
065: * Initialize the reader.
066: */
067: public void init(TempCharBuffer head) {
068: _top = head;
069: _head = head;
070:
071: if (head != null) {
072: _buffer = head.getBuffer();
073: _length = head.getLength();
074: } else
075: _length = 0;
076:
077: _offset = 0;
078: }
079:
080: /**
081: * Resets the reader
082: */
083: public void reset() {
084: init(_top);
085: }
086:
087: /**
088: * Reads the next character.
089: */
090: public int read() {
091: if (_length <= _offset) {
092: if (_head == null)
093: return -1;
094:
095: TempCharBuffer next = _head.getNext();
096: if (_isFree)
097: TempCharBuffer.free(_head);
098: _head = next;
099:
100: if (_head == null)
101: return -1;
102:
103: _buffer = _head.getBuffer();
104: _length = _head.getLength();
105: _offset = 0;
106: }
107:
108: return _buffer[_offset++];
109: }
110:
111: /**
112: * Reads the next character.
113: */
114: public int read(char[] buffer, int offset, int length) {
115: int readLength = 0;
116:
117: while (length > 0) {
118: if (_length <= _offset) {
119: if (_head == null)
120: return readLength == 0 ? -1 : readLength;
121:
122: TempCharBuffer next = _head.getNext();
123: if (_isFree)
124: TempCharBuffer.free(_head);
125: _head = next;
126:
127: if (_head == null)
128: return readLength == 0 ? -1 : readLength;
129:
130: _buffer = _head.getBuffer();
131: _length = _head.getLength();
132: _offset = 0;
133: }
134:
135: int sublen = _length - _offset;
136:
137: if (length < sublen)
138: sublen = length;
139:
140: System.arraycopy(_buffer, _offset, buffer, offset, sublen);
141:
142: _offset += sublen;
143: offset += sublen;
144: length -= sublen;
145: readLength += sublen;
146: }
147:
148: return readLength;
149: }
150:
151: public void unread() {
152: if (_offset > 0)
153: _offset--;
154: }
155:
156: /**
157: * Returns true if it's empty.
158: */
159: public boolean isEmpty() {
160: return _head == null;
161: }
162:
163: /**
164: * Closes the reader.
165: */
166: public void close() {
167: if (_isFree)
168: TempCharBuffer.freeAll(_head);
169:
170: _head = null;
171: _buffer = null;
172: _offset = 0;
173: _length = 0;
174: }
175: }
|