1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id: StringBufferPool.java 468655 2006-10-28 07:12:06Z minchau $
20  */
21 package org.apache.xml.utils;
22 
23 /**
24  * This class pools string buffers, since they are reused so often.
25  * String buffers are good candidates for pooling, because of
26  * their supporting character arrays.
27  * @xsl.usage internal
28  */
29 public class StringBufferPool
30 {
31 
32   /** The global pool of string buffers.   */
33   private static ObjectPool m_stringBufPool =
34     new ObjectPool(org.apache.xml.utils.FastStringBuffer.class);
35 
36   /**
37    * Get the first free instance of a string buffer, or create one
38    * if there are no free instances.
39    *
40    * @return A string buffer ready for use.
41    */
get()42   public synchronized static FastStringBuffer get()
43   {
44     return (FastStringBuffer) m_stringBufPool.getInstance();
45   }
46 
47   /**
48    * Return a string buffer back to the pool.
49    *
50    * @param sb Must be a non-null reference to a string buffer.
51    */
free(FastStringBuffer sb)52   public synchronized static void free(FastStringBuffer sb)
53   {
54     // Since this isn't synchronized, setLength must be
55     // done before the instance is freed.
56     // Fix attributed to Peter Speck <speck@ruc.dk>.
57     sb.setLength(0);
58     m_stringBufPool.freeInstance(sb);
59   }
60 }
61