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 com.sun.org.apache.xml.internal.utils;
  20. /**
  21. * This class pools string buffers, since they are reused so often.
  22. * String buffers are good candidates for pooling, because of
  23. * their supporting character arrays.
  24. * @xsl.usage internal
  25. */
  26. public class StringBufferPool
  27. {
  28. /** The global pool of string buffers. */
  29. private static ObjectPool m_stringBufPool =
  30. new ObjectPool(com.sun.org.apache.xml.internal.utils.FastStringBuffer.class);
  31. /**
  32. * Get the first free instance of a string buffer, or create one
  33. * if there are no free instances.
  34. *
  35. * @return A string buffer ready for use.
  36. */
  37. public synchronized static FastStringBuffer get()
  38. {
  39. return (FastStringBuffer) m_stringBufPool.getInstance();
  40. }
  41. /**
  42. * Return a string buffer back to the pool.
  43. *
  44. * @param sb Must be a non-null reference to a string buffer.
  45. */
  46. public synchronized static void free(FastStringBuffer sb)
  47. {
  48. // Since this isn't synchronized, setLength must be
  49. // done before the instance is freed.
  50. // Fix attributed to Peter Speck <speck@ruc.dk>.
  51. sb.setLength(0);
  52. m_stringBufPool.freeInstance(sb);
  53. }
  54. }