1 /*
   2  * Copyright (c) 2001, 2004, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.corba.se.impl.encoding;
  26 
  27 import java.nio.charset.CharsetDecoder;
  28 import java.nio.charset.CharsetEncoder;
  29 import java.util.Map;
  30 import java.util.WeakHashMap;
  31 
  32 /**
  33  * Thread local cache of sun.io code set converters for performance.
  34  *
  35  * The thread local class contains a single reference to a Map[]
  36  * containing two WeakHashMaps.  One for CharsetEncoders and
  37  * one for CharsetDecoders.  Constants are defined for indexing.
  38  *
  39  * This is used internally by CodeSetConversion.
  40  */
  41 class CodeSetCache
  42 {
  43     /**
  44      * The ThreadLocal data is a 2 element Map array indexed
  45      * by BTC_CACHE_MAP and CTB_CACHE_MAP.
  46      */
  47     private ThreadLocal converterCaches = new ThreadLocal() {
  48         public java.lang.Object initialValue() {
  49             return new Map[] { new WeakHashMap(), new WeakHashMap() };
  50         }
  51     };
  52 
  53     /**
  54      * Index in the thread local converterCaches array for
  55      * the byte to char converter Map.  A key is the Java
  56      * name corresponding to the desired code set.
  57      */
  58     private static final int BTC_CACHE_MAP = 0;
  59 
  60     /**
  61      * Index in the thread local converterCaches array for
  62      * the char to byte converter Map.  A key is the Java
  63      * name corresponding to the desired code set.
  64      */
  65     private static final int CTB_CACHE_MAP = 1;
  66 
  67     /**
  68      * Retrieve a CharsetDecoder from the Map using the given key.
  69      */
  70     CharsetDecoder getByteToCharConverter(Object key) {
  71         Map btcMap = ((Map[])converterCaches.get())[BTC_CACHE_MAP];
  72 
  73         return (CharsetDecoder)btcMap.get(key);
  74     }
  75 
  76     /**
  77      * Retrieve a CharsetEncoder from the Map using the given key.
  78      */
  79     CharsetEncoder getCharToByteConverter(Object key) {
  80         Map ctbMap = ((Map[])converterCaches.get())[CTB_CACHE_MAP];
  81 
  82         return (CharsetEncoder)ctbMap.get(key);
  83     }
  84 
  85     /**
  86      * Stores the given CharsetDecoder in the thread local cache,
  87      * and returns the same converter.
  88      */
  89     CharsetDecoder setConverter(Object key, CharsetDecoder converter) {
  90         Map btcMap = ((Map[])converterCaches.get())[BTC_CACHE_MAP];
  91 
  92         btcMap.put(key, converter);
  93 
  94         return converter;
  95     }
  96 
  97     /**
  98      * Stores the given CharsetEncoder in the thread local cache,
  99      * and returns the same converter.
 100      */
 101     CharsetEncoder setConverter(Object key, CharsetEncoder converter) {
 102 
 103         Map ctbMap = ((Map[])converterCaches.get())[CTB_CACHE_MAP];
 104 
 105         ctbMap.put(key, converter);
 106 
 107         return converter;
 108     }
 109 }