KickJava   Java API By Example, From Geeks To Geeks.

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


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.vfs;
31
32 import com.caucho.util.FreeList;
33
34 public class TempCharBuffer {
35   private static FreeList<TempCharBuffer> _freeList =
36     new FreeList<TempCharBuffer>(32);
37   
38   public static final int SIZE = TempBuffer.SIZE;
39
40   TempCharBuffer _next;
41   final char []_buf;
42   private int _offset;
43   int _length;
44   int _bufferCount;
45
46   /**
47    * Create a new TempBuffer.
48    */

49   public TempCharBuffer(int size)
50   {
51     _buf = new char[size];
52   }
53
54   /**
55    * Allocate a TempCharBuffer, reusing one if available.
56    */

57   public static TempCharBuffer allocate()
58   {
59     TempCharBuffer next = _freeList.allocate();
60
61     if (next == null)
62       return new TempCharBuffer(SIZE);
63
64     next._next = null;
65
66     next._offset = 0;
67     next._length = 0;
68     next._bufferCount = 0;
69
70     return next;
71   }
72
73   /**
74    * Clears the buffer.
75    */

76   public void clear()
77   {
78     _next = null;
79
80     _offset = 0;
81     _length = 0;
82     _bufferCount = 0;
83   }
84
85   /**
86    * Returns the buffer's underlying char array.
87    */

88   public final char []getBuffer()
89   {
90     return _buf;
91   }
92
93   /**
94    * Returns the number of chars in the buffer.
95    */

96   public final int getLength()
97   {
98     return _length;
99   }
100
101   public final void setLength(int length)
102   {
103     _length = length;
104   }
105
106   public final int getCapacity()
107   {
108     return _buf.length;
109   }
110
111   public int getAvailable()
112   {
113     return _buf.length - _length;
114   }
115
116   public final TempCharBuffer getNext()
117   {
118     return _next;
119   }
120
121   public final void setNext(TempCharBuffer next)
122   {
123     _next = next;
124   }
125
126   public int write(char []buf, int offset, int length)
127   {
128     char []thisBuf = _buf;
129     int thisLength = _length;
130     
131     if (thisBuf.length - thisLength < length)
132       length = thisBuf.length - thisLength;
133
134     System.arraycopy(buf, offset, thisBuf, thisLength, length);
135
136     _length = thisLength + length;
137
138     return length;
139   }
140
141   /**
142    * Frees a single buffer.
143    */

144   public static void free(TempCharBuffer buf)
145   {
146     buf._next = null;
147
148     if (buf._buf.length == SIZE)
149       _freeList.free(buf);
150   }
151
152   public static void freeAll(TempCharBuffer buf)
153   {
154     while (buf != null) {
155       TempCharBuffer next = buf._next;
156       buf._next = null;
157       if (buf._buf.length == SIZE)
158         _freeList.free(buf);
159       buf = next;
160     }
161   }
162 }
163
Popular Tags