< prev index next >

src/java.base/share/classes/java/nio/charset/Charset.java

Print this page
@@ -23,10 +23,11 @@
   * questions.
   */
  
  package java.nio.charset;
  
+ import jdk.internal.misc.Gate;
  import jdk.internal.misc.VM;
  import sun.nio.cs.ThreadLocalCoders;
  import sun.security.action.GetPropertyAction;
  
  import java.nio.ByteBuffer;

@@ -368,13 +369,12 @@
                  }
  
              };
      }
  
-     // Thread-local gate to prevent recursive provider lookups
-     private static ThreadLocal<ThreadLocal<?>> gate =
-             new ThreadLocal<ThreadLocal<?>>();
+     // gate to prevent recursive provider lookups
+     private static final Gate LOOKUP_GATE = Gate.create();
  
      @SuppressWarnings("removal")
      private static Charset lookupViaProviders(final String charsetName) {
  
          // The runtime startup sequence looks up standard charsets as a

@@ -386,16 +386,14 @@
          // information.
          //
          if (!VM.isBooted())
              return null;
  
-         if (gate.get() != null)
+         if (!LOOKUP_GATE.tryEnter())
              // Avoid recursive provider lookups
              return null;
          try {
-             gate.set(gate);
- 
              return AccessController.doPrivileged(
                  new PrivilegedAction<>() {
                      public Charset run() {
                          for (Iterator<CharsetProvider> i = providers();
                               i.hasNext();) {

@@ -407,11 +405,11 @@
                          return null;
                      }
                  });
  
          } finally {
-             gate.set(null);
+             LOOKUP_GATE.exit();
          }
      }
  
      /* The extended set of charsets */
      private static class ExtendedProviderHolder {

@@ -811,13 +809,18 @@
       * @param  bb  The byte buffer to be decoded
       *
       * @return  A char buffer containing the decoded characters
       */
      public final CharBuffer decode(ByteBuffer bb) {
+         CharsetDecoder decoder;
+         if (Thread.currentThread().isVirtual()) {
+             decoder = newDecoder();
+         } else {
+             decoder = ThreadLocalCoders.decoderFor(this);
+         }
          try {
-             return ThreadLocalCoders.decoderFor(this)
-                 .onMalformedInput(CodingErrorAction.REPLACE)
+             return decoder.onMalformedInput(CodingErrorAction.REPLACE)
                  .onUnmappableCharacter(CodingErrorAction.REPLACE)
                  .decode(bb);
          } catch (CharacterCodingException x) {
              throw new Error(x);         // Can't happen
          }

@@ -847,13 +850,18 @@
       * @param  cb  The char buffer to be encoded
       *
       * @return  A byte buffer containing the encoded characters
       */
      public final ByteBuffer encode(CharBuffer cb) {
+         CharsetEncoder encoder;
+         if (Thread.currentThread().isVirtual()) {
+             encoder = newEncoder();
+         } else {
+             encoder = ThreadLocalCoders.encoderFor(this);
+         }
          try {
-             return ThreadLocalCoders.encoderFor(this)
-                 .onMalformedInput(CodingErrorAction.REPLACE)
+             return encoder.onMalformedInput(CodingErrorAction.REPLACE)
                  .onUnmappableCharacter(CodingErrorAction.REPLACE)
                  .encode(cb);
          } catch (CharacterCodingException x) {
              throw new Error(x);         // Can't happen
          }
< prev index next >