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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.vfs;
031:
032: import com.caucho.util.FreeList;
033:
034: public class TempCharBuffer {
035: private static FreeList<TempCharBuffer> _freeList = new FreeList<TempCharBuffer>(
036: 32);
037:
038: public static final int SIZE = TempBuffer.SIZE;
039:
040: TempCharBuffer _next;
041: final char[] _buf;
042: private int _offset;
043: int _length;
044: int _bufferCount;
045:
046: /**
047: * Create a new TempBuffer.
048: */
049: public TempCharBuffer(int size) {
050: _buf = new char[size];
051: }
052:
053: /**
054: * Allocate a TempCharBuffer, reusing one if available.
055: */
056: public static TempCharBuffer allocate() {
057: TempCharBuffer next = _freeList.allocate();
058:
059: if (next == null)
060: return new TempCharBuffer(SIZE);
061:
062: next._next = null;
063:
064: next._offset = 0;
065: next._length = 0;
066: next._bufferCount = 0;
067:
068: return next;
069: }
070:
071: /**
072: * Clears the buffer.
073: */
074: public void clear() {
075: _next = null;
076:
077: _offset = 0;
078: _length = 0;
079: _bufferCount = 0;
080: }
081:
082: /**
083: * Returns the buffer's underlying char array.
084: */
085: public final char[] getBuffer() {
086: return _buf;
087: }
088:
089: /**
090: * Returns the number of chars in the buffer.
091: */
092: public final int getLength() {
093: return _length;
094: }
095:
096: public final void setLength(int length) {
097: _length = length;
098: }
099:
100: public final int getCapacity() {
101: return _buf.length;
102: }
103:
104: public int getAvailable() {
105: return _buf.length - _length;
106: }
107:
108: public final TempCharBuffer getNext() {
109: return _next;
110: }
111:
112: public final void setNext(TempCharBuffer next) {
113: _next = next;
114: }
115:
116: public int write(char[] buf, int offset, int length) {
117: char[] this Buf = _buf;
118: int this Length = _length;
119:
120: if (this Buf.length - this Length < length)
121: length = this Buf.length - this Length;
122:
123: System.arraycopy(buf, offset, this Buf, this Length, length);
124:
125: _length = this Length + length;
126:
127: return length;
128: }
129:
130: /**
131: * Frees a single buffer.
132: */
133: public static void free(TempCharBuffer buf) {
134: buf._next = null;
135:
136: if (buf._buf.length == SIZE)
137: _freeList.free(buf);
138: }
139:
140: public static void freeAll(TempCharBuffer buf) {
141: while (buf != null) {
142: TempCharBuffer next = buf._next;
143: buf._next = null;
144: if (buf._buf.length == SIZE)
145: _freeList.free(buf);
146: buf = next;
147: }
148: }
149: }
|