encoding.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 586 行 · 第 1/2 页
JAVA
586 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.vfs;import com.caucho.util.CharBuffer;import com.caucho.vfs.i18n.EncodingReader;import com.caucho.vfs.i18n.EncodingWriter;import com.caucho.vfs.i18n.ISO8859_1Writer;import com.caucho.vfs.i18n.JDKReader;import com.caucho.vfs.i18n.JDKWriter;import java.io.InputStream;import java.io.Reader;import java.io.UnsupportedEncodingException;import java.util.HashMap;import java.util.Hashtable;import java.util.Locale;/** * Converts between the mime encoding names and Java encoding names. */public class Encoding { static HashMap<String,String> _javaName; static Hashtable<String,String> _mimeName; static HashMap<String,String> _localeName; // map from an encoding name to its EncodingReader factory. static final HashMap<String,EncodingReader> _readEncodingFactories = new HashMap<String,EncodingReader>(); // map from an encoding name to its EncodingWriter factory. static final HashMap<String,EncodingWriter> _writeEncodingFactories = new HashMap<String,EncodingWriter>(); static final EncodingWriter _latin1Writer = new ISO8859_1Writer(); /** * Can't create an instance of the encoding class. */ private Encoding() {} /** * Returns the canonical mime name for the given character encoding. * * @param encoding character encoding name, possibly an alias * * @return canonical mime name for the encoding. */ public static String getMimeName(String encoding) { if (encoding == null) return null; String value = _mimeName.get(encoding); if (value != null) return value; String upper = normalize(encoding); String lookup = _mimeName.get(upper); value = lookup == null ? upper : lookup; _mimeName.put(encoding, value); return value; } /** * Returns the canonical mime name for the given locale. * * @param locale locale to use. * * @return canonical mime name for the encoding. */ public static String getMimeName(Locale locale) { if (locale == null) return "ISO-8859-1"; String mimeName = _localeName.get(locale.toString()); if (mimeName == null) mimeName = _localeName.get(locale.getLanguage()); if (mimeName == null) return "ISO-8859-1"; else return mimeName; } /** * Returns a Reader to translate bytes to characters. If a specialized * reader exists in com.caucho.vfs.i18n, use it. * * @param is the input stream. * @param encoding the encoding name. * * @return a reader for the translation */ public static Reader getReadEncoding(InputStream is, String encoding) throws UnsupportedEncodingException { return getReadFactory(encoding).create(is); } /** * Returns a Reader to translate bytes to characters. If a specialized * reader exists in com.caucho.vfs.i18n, use it. * * @param is the input stream. * @param encoding the encoding name. * * @return a reader for the translation */ public static EncodingReader getReadFactory(String encoding) throws UnsupportedEncodingException { EncodingReader factory = null; synchronized (_readEncodingFactories) { factory = _readEncodingFactories.get(encoding); if (factory == null) { try { String javaEncoding = Encoding.getJavaName(encoding); if (javaEncoding == null) javaEncoding = "ISO8859_1"; String className = "com.caucho.vfs.i18n." + javaEncoding + "Reader"; Class cl = Class.forName(className); factory = (EncodingReader) cl.newInstance(); factory.setJavaEncoding(javaEncoding); } catch (Throwable e) { } if (factory == null) { String javaEncoding = Encoding.getJavaName(encoding); if (javaEncoding == null) javaEncoding = "ISO8859_1"; factory = new JDKReader(); factory.setJavaEncoding(javaEncoding); } _readEncodingFactories.put(encoding, factory); } } return factory; } /** * Returns an EncodingWriter to translate characters to bytes. * * @param encoding the encoding name. * * @return a writer for the translation */ public static EncodingWriter getWriteEncoding(String encoding) { EncodingWriter factory = _writeEncodingFactories.get(encoding); if (factory != null) return factory.create(); synchronized (_writeEncodingFactories) { factory = _writeEncodingFactories.get(encoding); if (factory == null) { try { String javaEncoding = Encoding.getJavaName(encoding); if (javaEncoding == null) javaEncoding = "ISO8859_1"; String className = "com.caucho.vfs.i18n." + javaEncoding + "Writer"; Class cl = Class.forName(className); factory = (EncodingWriter) cl.newInstance(); factory.setJavaEncoding(javaEncoding); } catch (Throwable e) { } if (factory == null) { factory = new JDKWriter(); String javaEncoding = Encoding.getJavaName(encoding); if (javaEncoding == null) javaEncoding = "ISO8859_1"; factory.setJavaEncoding(javaEncoding); } _writeEncodingFactories.put(encoding, factory); } } // return factory.create(factory.getJavaEncoding()); // charset uses the original encoding, not the java encoding return factory.create(encoding); } /** * Returns the latin 1 writer. */ public static EncodingWriter getLatin1Writer() { return _latin1Writer; } /** * Returns the Java name for the given encoding. * * @param encoding character encoding name * * @return Java encoding name */ public static String getJavaName(String encoding) { if (encoding == null) return null; String upper = normalize(encoding); String javaName = null; javaName = _javaName.get(upper); if (javaName != null) return javaName; String lookup = _mimeName.get(upper); if (lookup != null) javaName = _javaName.get(lookup); return javaName == null ? upper : javaName; } /** * Returns the Java name for the given locale. * * @param locale the locale to use * * @return Java encoding name */ public static String getJavaName(Locale locale) { if (locale == null) return null; return getJavaName(getMimeName(locale)); } /** * Normalize the user's encoding name to avoid case issues. */ private static String normalize(String name) { CharBuffer cb = CharBuffer.allocate(); int len = name.length(); for (int i = 0; i < len; i++) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?