Java Charset Create getEncoder()

Here you can find the source of getEncoder()

Description

get Encoder

License

Apache License

Declaration

private static CharsetEncoder getEncoder() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.lang.ref.SoftReference;

import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

public class Main {
    public static final Charset UTF8_CHARSET = Charset.forName("UTF-8");
    private final static ThreadLocal<SoftReference<CharsetEncoder>> encoder = new ThreadLocal<>();

    private static CharsetEncoder getEncoder() {
        SoftReference<CharsetEncoder> sr = encoder.get();
        if (sr == null || sr.get() == null) {
            CharsetEncoder ce = UTF8_CHARSET.newEncoder();
            encoder.set(new SoftReference<CharsetEncoder>(ce));
            return ce;
        } else {//ww w  .j  av  a 2  s.  co  m
            return sr.get();
        }
    }
}

Related

  1. getDefaultCharsetName()
  2. getDefaultSystemCharset()
  3. getEncodedStringLength(String str)
  4. getEncodedUrl(String unEncodedUrl)
  5. getEncoder()
  6. getEncoder()
  7. getEncoder(String encoding)
  8. getEncoding(byte[] htmlData)
  9. getEncoding(OutputStreamWriter inWriter)