Java tutorial
//package com.java2s; /** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.lang.reflect.Method; import java.nio.charset.Charset; import java.nio.charset.IllegalCharsetNameException; import java.util.HashMap; import java.util.Locale; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { private static final Pattern CHARSET_NAME_PATTERN = Pattern.compile("[ \\\"]*([^ >,;\\\"]+).*"); private static final Pattern ISO_NAME_PATTERN = Pattern.compile(".*8859-(\\d+)"); private static final Pattern CP_NAME_PATTERN = Pattern.compile("cp-(\\d+)"); private static final Pattern WIN_NAME_PATTERN = Pattern.compile("win-?(\\d+)"); private static final Map<String, Charset> COMMON_CHARSETS = new HashMap<String, Charset>(); private static Method getCharsetICU = null; /** Returns Charset impl, if one exists. This method * optionally uses ICU4J's CharsetICU.forNameICU, * if it is found on the classpath, else only uses * JDK's builtin Charset.forName. */ public static Charset forName(String name) { if (name == null) { throw new IllegalArgumentException(); } // Get rid of cruft around names, like <>, trailing commas, etc. Matcher m = CHARSET_NAME_PATTERN.matcher(name); if (!m.matches()) { throw new IllegalCharsetNameException(name); } name = m.group(1); String lower = name.toLowerCase(Locale.ENGLISH); Charset charset = COMMON_CHARSETS.get(lower); if (charset != null) { return charset; } else if ("none".equals(lower) || "no".equals(lower)) { throw new IllegalCharsetNameException(name); } else { Matcher iso = ISO_NAME_PATTERN.matcher(lower); Matcher cp = CP_NAME_PATTERN.matcher(lower); Matcher win = WIN_NAME_PATTERN.matcher(lower); if (iso.matches()) { // Handle "iso 8859-x" error name = "iso-8859-" + iso.group(1); charset = COMMON_CHARSETS.get(name); } else if (cp.matches()) { // Handle "cp-xxx" error name = "cp" + cp.group(1); charset = COMMON_CHARSETS.get(name); } else if (win.matches()) { // Handle "winxxx" and "win-xxx" errors name = "windows-" + win.group(1); charset = COMMON_CHARSETS.get(name); } if (charset != null) { return charset; } } if (getCharsetICU != null) { try { Charset cs = (Charset) getCharsetICU.invoke(null, name); if (cs != null) { return cs; } } catch (Exception e) { // ignore } } return Charset.forName(name); } }