KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > encoding > CodeSetCache


1 /*
2  * @(#)CodeSetCache.java 1.8 04/03/01
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.corba.se.impl.encoding;
8
9 import java.nio.charset.CharsetDecoder JavaDoc;
10 import java.nio.charset.CharsetEncoder JavaDoc;
11 import java.util.Map JavaDoc;
12 import java.util.WeakHashMap JavaDoc;
13
14 /**
15  * Thread local cache of sun.io code set converters for performance.
16  *
17  * The thread local class contains a single reference to a Map[]
18  * containing two WeakHashMaps. One for CharsetEncoders and
19  * one for CharsetDecoders. Constants are defined for indexing.
20  *
21  * This is used internally by CodeSetConversion.
22  */

23 class CodeSetCache
24 {
25     /**
26      * The ThreadLocal data is a 2 element Map array indexed
27      * by BTC_CACHE_MAP and CTB_CACHE_MAP.
28      */

29     private ThreadLocal JavaDoc converterCaches = new ThreadLocal JavaDoc() {
30         public java.lang.Object JavaDoc initialValue() {
31             return new Map JavaDoc[] { new WeakHashMap JavaDoc(), new WeakHashMap JavaDoc() };
32         }
33     };
34
35     /**
36      * Index in the thread local converterCaches array for
37      * the byte to char converter Map. A key is the Java
38      * name corresponding to the desired code set.
39      */

40     private static final int BTC_CACHE_MAP = 0;
41
42     /**
43      * Index in the thread local converterCaches array for
44      * the char to byte converter Map. A key is the Java
45      * name corresponding to the desired code set.
46      */

47     private static final int CTB_CACHE_MAP = 1;
48
49     /**
50      * Retrieve a CharsetDecoder from the Map using the given key.
51      */

52     CharsetDecoder JavaDoc getByteToCharConverter(Object JavaDoc key) {
53         Map JavaDoc btcMap = ((Map JavaDoc[])converterCaches.get())[BTC_CACHE_MAP];
54         
55         return (CharsetDecoder JavaDoc)btcMap.get(key);
56     }
57
58     /**
59      * Retrieve a CharsetEncoder from the Map using the given key.
60      */

61     CharsetEncoder JavaDoc getCharToByteConverter(Object JavaDoc key) {
62         Map JavaDoc ctbMap = ((Map JavaDoc[])converterCaches.get())[CTB_CACHE_MAP];
63
64         return (CharsetEncoder JavaDoc)ctbMap.get(key);
65     }
66
67     /**
68      * Stores the given CharsetDecoder in the thread local cache,
69      * and returns the same converter.
70      */

71     CharsetDecoder JavaDoc setConverter(Object JavaDoc key, CharsetDecoder JavaDoc converter) {
72         Map JavaDoc btcMap = ((Map JavaDoc[])converterCaches.get())[BTC_CACHE_MAP];
73
74         btcMap.put(key, converter);
75
76         return converter;
77     }
78
79     /**
80      * Stores the given CharsetEncoder in the thread local cache,
81      * and returns the same converter.
82      */

83     CharsetEncoder JavaDoc setConverter(Object JavaDoc key, CharsetEncoder JavaDoc converter) {
84
85         Map JavaDoc ctbMap = ((Map JavaDoc[])converterCaches.get())[CTB_CACHE_MAP];
86
87         ctbMap.put(key, converter);
88
89         return converter;
90     }
91 }
92
Popular Tags