KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xml > utils > StringBufferPool


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: StringBufferPool.java,v 1.6 2004/02/17 04:21:14 minchau Exp $
18  */

19 package org.apache.xml.utils;
20
21 /**
22  * This class pools string buffers, since they are reused so often.
23  * String buffers are good candidates for pooling, because of
24  * their supporting character arrays.
25  * @xsl.usage internal
26  */

27 public class StringBufferPool
28 {
29
30   /** The global pool of string buffers. */
31   private static ObjectPool m_stringBufPool =
32     new ObjectPool(org.apache.xml.utils.FastStringBuffer.class);
33
34   /**
35    * Get the first free instance of a string buffer, or create one
36    * if there are no free instances.
37    *
38    * @return A string buffer ready for use.
39    */

40   public synchronized static FastStringBuffer get()
41   {
42     return (FastStringBuffer) m_stringBufPool.getInstance();
43   }
44
45   /**
46    * Return a string buffer back to the pool.
47    *
48    * @param sb Must be a non-null reference to a string buffer.
49    */

50   public synchronized static void free(FastStringBuffer sb)
51   {
52     // Since this isn't synchronized, setLength must be
53
// done before the instance is freed.
54
// Fix attributed to Peter Speck <speck@ruc.dk>.
55
sb.setLength(0);
56     m_stringBufPool.freeInstance(sb);
57   }
58 }
59
Popular Tags