Here you can find the source of getEncoder()
private static CharsetEncoder getEncoder()
//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(); } } }