Code to fetch an instance of {@link java.nio.charset.Charset} corresponding to the given encoding.

@param encoding as a string name (eg. UTF-8).
@return the code to fetch the associated Charset.
354358
public static String charset(final String encoding)
    {
        final String charsetName = STD_CHARSETS.get(encoding);
        if (charsetName != null)
        {
            return "java.nio.charset.StandardCharsets." + charsetName;
        }
        else
        {
            return "java.nio.charset.Charset.forName(\"" + encoding + "\")";
        }
    }
691679
public Charset getCharset() {
        String charset = getParameter(PARAM_CHARSET);
        return (charset != null ? Charset.forName(unquote(charset)) : null);
    }
437256
public static OutputStreamWriter getWriter(OutputStream out, String charsetName) {
		return getWriter(out, Charset.forName(charsetName));
	}
723035
public Charset contentCharset() {
        String ct = contentType();
        if (ct != null) {
            int index = ct.indexOf("charset=");
            if (index != -1) {
                return Charset.forName(ct.substring(index + 8));
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
843263
public void setEncoding(String encoding)
  {
    _charEncoding = encoding;

    String javaEncoding = Encoding.getJavaName(encoding);

    if ("ISO8859_1".equals(javaEncoding))
      _charEncoding = null;
  }
547680
public String getStringBody(Charset charset) throws IOException {
        BufferedReader rd = new BufferedReader(new InputStreamReader(getInputStreamBody(), charset));
        String line;
        StringBuilder response = new StringBuilder();
        while ((line = rd.readLine()) != null) {
            if (response.length() != 0) {
                response.append('\n');
            }
            response.append(line);
        }
        rd.close();
        return response.toString();
    }
1000986
public static Charset forNameUEE(String charsetName) throws UnsupportedEncodingException {
        try {
            return Charset.forName(charsetName);
        } catch (Exception cause) {
            UnsupportedEncodingException ex = new UnsupportedEncodingException(charsetName);
            ex.initCause(cause);
            throw ex;
        }
    }
750473
public boolean setEncoding(String encoding) {
        for(Map.Entry<Byte, String> entry : CODE_PAGE_ENCODING.entrySet()) {
            if(entry.getValue().equalsIgnoreCase(encoding)) {
                this.fileEncoding = entry.getValue();
                return true;
            }
        }
        return false;
    }
742856
public static CharsetEncoder encoder(Charset charset) {
        checkNotNull(charset, "charset");

        Map<Charset, CharsetEncoder> map = InternalThreadLocalMap.get().charsetEncoderCache();
        CharsetEncoder e = map.get(charset);
        if (e != null) {
            e.reset().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
            return e;
        }

        e = encoder(charset, CodingErrorAction.REPLACE, CodingErrorAction.REPLACE);
        map.put(charset, e);
        return e;
    }
378894
public Writer getWriter(String encoding)
        throws IOException
    {
        if (encoding==null ||
            StringUtil.__ISO_8859_1.equalsIgnoreCase(encoding)  ||
            "ISO8859_1".equalsIgnoreCase(encoding))
            return getISO8859Writer();

        if ("UTF-8".equalsIgnoreCase(encoding) ||
            "UTF8".equalsIgnoreCase(encoding))
            return getUTF8Writer();
        
        if ("US-ASCII".equalsIgnoreCase(encoding))
            return getASCIIWriter();

        return new OutputStreamWriter(this,encoding);
    }
845189
public static Charset getStringEncodingCharset() {
		if (currentStringEncoding == null) {
			final String value = JanusConfig.getSystemProperty(BYTE_ARRAY_STRING_CHARSET_NAME, null);
			if (value != null) {
				try {
					currentStringEncoding = Charset.forName(value);
					if (currentStringEncoding == null) {
						currentStringEncoding = BYTE_ARRAY_STRING_CHARSET_VALUE;
					}
				} catch (Throwable exception) {
					currentStringEncoding = BYTE_ARRAY_STRING_CHARSET_VALUE;
				}
			} else {
				currentStringEncoding = BYTE_ARRAY_STRING_CHARSET_VALUE;
			}
		}
		return currentStringEncoding;
	}
984109
public static Reader getReadEncoding(InputStream is, String encoding)
    throws UnsupportedEncodingException
  {
    return getReadFactory(encoding).create(is);
  }
1025125
public static String getString(byte[] contentBytes, Charset charset, boolean isGetCharsetFromContent) throws IOException {
		if (null == contentBytes) {
			return null;
		}

		if (null == charset) {
			charset = CharsetUtil.CHARSET_UTF_8;
		}
		String content = new String(contentBytes, charset);
		if (isGetCharsetFromContent) {
			final String charsetInContentStr = ReUtil.get(META_CHARSET_PATTERN, content, 1);
			if (StrUtil.isNotBlank(charsetInContentStr)) {
				Charset charsetInContent = null;
				try {
					charsetInContent = Charset.forName(charsetInContentStr);
				} catch (Exception e) {
					if (StrUtil.containsIgnoreCase(charsetInContentStr, "utf-8") || StrUtil.containsIgnoreCase(charsetInContentStr, "utf8")) {
						charsetInContent = CharsetUtil.CHARSET_UTF_8;
					} else if (StrUtil.containsIgnoreCase(charsetInContentStr, "gbk")) {
						charsetInContent = CharsetUtil.CHARSET_GBK;
					}
					// ignore
				}
				if (null != charsetInContent && false == charset.equals(charsetInContent)) {
					content = new String(contentBytes, charsetInContent);
				}
			}
		}
		return content;
	}
569754
public static Charset forName(String charsetName) { // NOPMD
    if (charsetName == null) {
      throw new IllegalArgumentException("Null charset name");
    }

    charsetName = charsetName.toUpperCase();
    if (EmulatedCharset.ISO_8859_1.name().equals(charsetName)) {
      return EmulatedCharset.ISO_8859_1;
    } else if (EmulatedCharset.ISO_LATIN_1.name().equals(charsetName)) {
      return EmulatedCharset.ISO_LATIN_1;
    } else if (EmulatedCharset.UTF_8.name().equals(charsetName)) {
      return EmulatedCharset.UTF_8;
    }

    if (!charsetName.matches("^[A-Za-z0-9][\\w-:\\.\\+]*$")) { // NOPMD
      throw new IllegalCharsetNameException(charsetName);
    } else {
      throw new UnsupportedCharsetException(charsetName);
    }
  }
546646
@Nonnull
  public static String ensureEncodingName(@Nullable final String charsetName) {
    final Charset defaultCharset = Charset.defaultCharset();
    try {
      return (charsetName == null) ? defaultCharset.name() : Charset.forName(charsetName.trim()).name();
    } catch (IllegalCharsetNameException ex) {
      throw new IllegalArgumentException("Can't recognize charset for name '" + charsetName + '\'');
    }
  }
1020696
public static String encode(String source, String charset) {
		return encode(StrUtil.bytes(source, charset), charset);
	}
1158332
public static TextLoader getInstance(Encoding encoding)
	{
		if (null == encoding)
		{
			return getInstance();
		}
		else
		{
			if (!INSTANCE_BY_ENCODING.containsKey(encoding.getIsoName()))
			{
				TextLoader _newInstance = new TextLoader(encoding);
				INSTANCE_BY_ENCODING.put(encoding.getIsoName(), _newInstance);
			}
			return INSTANCE_BY_ENCODING.get(encoding.getIsoName());
		}
	}