01: /*
02: * ThreadLocalBufferAllocator.java
03: *
04: * Created on November 22, 2006, 1:58 PM
05: *
06: * To change this template, choose Tools | Template Manager
07: * and open the template in the editor.
08: */
09:
10: package com.sun.xml.stream.util;
11:
12: import java.lang.ref.*;
13:
14: /**
15: * This is a Singleton class that allows you to allocate buffer that
16: * are stored in ThreadLocal. This class stores 3 types of char
17: * buffers - small (< 128 bytes), medium (<2K) and large (> 2K) as
18: * well as three byte buffers - small, medium and large.
19: * The local storage is activated on the return of the buffer.
20: * The buffer returns null if it is already allocated.
21: */
22: public class ThreadLocalBufferAllocator {
23: private static ThreadLocal tlba = new ThreadLocal();
24:
25: public static BufferAllocator getBufferAllocator() {
26: SoftReference bAllocatorRef = (SoftReference) tlba.get();
27: if (bAllocatorRef == null || bAllocatorRef.get() == null) {
28: bAllocatorRef = new SoftReference(new BufferAllocator());
29: tlba.set(bAllocatorRef);
30: }
31:
32: return (BufferAllocator) bAllocatorRef.get();
33: }
34: }
|