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.IOException;
032:
033: public class TempCharStream extends StreamImpl {
034: private TempCharBuffer _head;
035: private TempCharBuffer _tail;
036:
037: public TempCharStream() {
038: }
039:
040: /**
041: * Initializes the temp stream for writing.
042: */
043: public void openWrite() {
044: TempCharBuffer ptr = _head;
045:
046: _head = null;
047: _tail = null;
048:
049: TempCharBuffer.freeAll(ptr);
050: }
051:
052: /**
053: * Returns the tail buffer.
054: */
055: public char[] getTail() {
056: return _tail.getBuffer();
057: }
058:
059: /**
060: * Returns the head.
061: */
062: public TempCharBuffer getHead() {
063: return _head;
064: }
065:
066: /**
067: * Returns true since the temp stream can write.
068: */
069: public boolean canWrite() {
070: return true;
071: }
072:
073: public void write(char[] buf, int offset, int length)
074: throws IOException {
075: int index = 0;
076: while (index < length) {
077: if (_tail == null)
078: addBuffer(TempCharBuffer.allocate());
079: else if (_tail._buf.length <= _tail._length) {
080: addBuffer(TempCharBuffer.allocate());
081:
082: // XXX: see TempStream for backing files
083: }
084:
085: int sublen = _tail._buf.length - _tail._length;
086: if (length - index < sublen)
087: sublen = length - index;
088:
089: System.arraycopy(buf, index + offset, _tail._buf,
090: _tail._length, sublen);
091:
092: index += sublen;
093: _tail._length += sublen;
094: }
095: }
096:
097: /**
098: * Writes part of a string.
099: */
100: public void write(String s, int offset, int length)
101: throws IOException {
102: while (length > 0) {
103: if (_tail == null)
104: addBuffer(TempCharBuffer.allocate());
105: else if (_tail._buf.length <= _tail._length) {
106: addBuffer(TempCharBuffer.allocate());
107:
108: // XXX: see TempStream for backing files
109: }
110:
111: int sublen = _tail._buf.length - _tail._length;
112: if (length < sublen)
113: sublen = length;
114:
115: s.getChars(offset, offset + sublen, _tail._buf,
116: _tail._length);
117:
118: offset += sublen;
119: length -= sublen;
120: _tail._length += sublen;
121: }
122: }
123:
124: public void write(int ch) throws IOException {
125: if (_tail == null)
126: addBuffer(TempCharBuffer.allocate());
127: else if (_tail._buf.length <= _tail._length) {
128: addBuffer(TempCharBuffer.allocate());
129:
130: // XXX: see TempStream for backing files
131: }
132:
133: _tail._buf[_tail._length++] = (char) ch;
134: }
135:
136: private void addBuffer(TempCharBuffer buf) {
137: buf._next = null;
138: if (_tail != null) {
139: _tail._next = buf;
140: _tail = buf;
141: } else {
142: _tail = buf;
143: _head = buf;
144: }
145:
146: _head._bufferCount++;
147: }
148:
149: public void flush() throws IOException {
150: }
151:
152: public void close() throws IOException {
153: super .close();
154: }
155:
156: /**
157: * Returns the total length of the buffer's bytes
158: */
159: public int getLength() {
160: int length = 0;
161:
162: for (TempCharBuffer ptr = _head; ptr != null; ptr = ptr._next) {
163: length += ptr.getLength();
164: }
165:
166: return length;
167: }
168:
169: public void clearWrite() {
170: TempCharBuffer ptr = _head;
171:
172: _head = null;
173: _tail = null;
174:
175: TempCharBuffer.freeAll(ptr);
176: }
177:
178: public void discard() {
179: _head = null;
180: _tail = null;
181: }
182:
183: /**
184: * Clean up the temp stream.
185: */
186: public void destroy() {
187: try {
188: close();
189: } catch (IOException e) {
190: }
191:
192: TempCharBuffer ptr = _head;
193:
194: _head = null;
195: _tail = null;
196:
197: TempCharBuffer.freeAll(ptr);
198: }
199: }
|