001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 /*
019 * $Id: StringBufferPool.java 468655 2006-10-28 07:12:06Z minchau $
020 */
021 package org.apache.xml.utils;
022
023 /**
024 * This class pools string buffers, since they are reused so often.
025 * String buffers are good candidates for pooling, because of
026 * their supporting character arrays.
027 * @xsl.usage internal
028 */
029 public class StringBufferPool
030 {
031
032 /** The global pool of string buffers. */
033 private static ObjectPool m_stringBufPool =
034 new ObjectPool(org.apache.xml.utils.FastStringBuffer.class);
035
036 /**
037 * Get the first free instance of a string buffer, or create one
038 * if there are no free instances.
039 *
040 * @return A string buffer ready for use.
041 */
042 public synchronized static FastStringBuffer get()
043 {
044 return (FastStringBuffer) m_stringBufPool.getInstance();
045 }
046
047 /**
048 * Return a string buffer back to the pool.
049 *
050 * @param sb Must be a non-null reference to a string buffer.
051 */
052 public synchronized static void free(FastStringBuffer sb)
053 {
054 // Since this isn't synchronized, setLength must be
055 // done before the instance is freed.
056 // Fix attributed to Peter Speck <speck@ruc.dk>.
057 sb.setLength(0);
058 m_stringBufPool.freeInstance(sb);
059 }
060 }