1. /*
  2. * @(#)CodeSetCache.java 1.5 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.internal.core;
  8. import java.util.Map;
  9. import java.util.WeakHashMap;
  10. import sun.io.*;
  11. /**
  12. * Thread local cache of sun.io code set converters for performance.
  13. *
  14. * The thread local class contains a single reference to a Map[]
  15. * containing two WeakHashMaps. One for CharToByteConverters and
  16. * one for ByteToCharConverters. Constants are defined for
  17. * indexing.
  18. *
  19. * This is used internally by CodeSetConversion.
  20. */
  21. class CodeSetCache
  22. {
  23. /**
  24. * The ThreadLocal data is a 2 element Map array indexed
  25. * by BTC_CACHE_MAP and CTB_CACHE_MAP.
  26. */
  27. private ThreadLocal converterCaches = new ThreadLocal() {
  28. public java.lang.Object initialValue() {
  29. return new Map[] { new WeakHashMap(), new WeakHashMap() };
  30. }
  31. };
  32. /**
  33. * Index in the thread local converterCaches array for
  34. * the byte to char converter Map. A key is the Java
  35. * name corresponding to the desired code set.
  36. */
  37. private static final int BTC_CACHE_MAP = 0;
  38. /**
  39. * Index in the thread local converterCaches array for
  40. * the char to byte converter Map. A key is the Java
  41. * name corresponding to the desired code set.
  42. */
  43. private static final int CTB_CACHE_MAP = 1;
  44. /**
  45. * Retrieve a ByteToCharConverter from the Map using the given key.
  46. */
  47. ByteToCharConverter getByteToCharConverter(Object key) {
  48. Map btcMap = ((Map[])converterCaches.get())[BTC_CACHE_MAP];
  49. return (ByteToCharConverter)btcMap.get(key);
  50. }
  51. /**
  52. * Retrieve a CharToByteConverter from the Map using the given key.
  53. */
  54. CharToByteConverter getCharToByteConverter(Object key) {
  55. Map ctbMap = ((Map[])converterCaches.get())[CTB_CACHE_MAP];
  56. return (CharToByteConverter)ctbMap.get(key);
  57. }
  58. /**
  59. * Stores the given ByteToCharConverter in the thread local cache,
  60. * and returns the same converter.
  61. */
  62. ByteToCharConverter setConverter(Object key,
  63. ByteToCharConverter converter) {
  64. Map btcMap = ((Map[])converterCaches.get())[BTC_CACHE_MAP];
  65. btcMap.put(key, converter);
  66. return converter;
  67. }
  68. /**
  69. * Stores the given CharToByteConverter in the thread local cache,
  70. * and returns the same converter.
  71. */
  72. CharToByteConverter setConverter(Object key,
  73. CharToByteConverter converter) {
  74. Map ctbMap = ((Map[])converterCaches.get())[CTB_CACHE_MAP];
  75. ctbMap.put(key, converter);
  76. return converter;
  77. }
  78. }