01: /*
02: * Copyright 1999-2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: /** The global pool of string buffers. */
30: private static ObjectPool m_stringBufPool = new ObjectPool(
31: org.apache.xml.utils.FastStringBuffer.class);
32:
33: /**
34: * Get the first free instance of a string buffer, or create one
35: * if there are no free instances.
36: *
37: * @return A string buffer ready for use.
38: */
39: public synchronized static FastStringBuffer get() {
40: return (FastStringBuffer) m_stringBufPool.getInstance();
41: }
42:
43: /**
44: * Return a string buffer back to the pool.
45: *
46: * @param sb Must be a non-null reference to a string buffer.
47: */
48: public synchronized static void free(FastStringBuffer sb) {
49: // Since this isn't synchronized, setLength must be
50: // done before the instance is freed.
51: // Fix attributed to Peter Speck <speck@ruc.dk>.
52: sb.setLength(0);
53: m_stringBufPool.freeInstance(sb);
54: }
55: }
|