encodingmanager.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 532 行 · 第 1/2 页

JAVA
532
字号
/* EncodingManager.java -- Manages character encoding translators
   Copyright (C) 1998, 1999 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath 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, or (at your option)
any later version.
 
GNU Classpath 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.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package gnu.java.io;

import gnu.java.io.decode.Decoder;
import gnu.java.io.encode.Encoder;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.util.Hashtable;
import java.util.StringTokenizer;

/**
  * This class is used to create new instances of Decoders for a specified
  * encoding scheme.  These instances are cache for fast subsequent retrieval 
  * if necessary.
  *
  * @version 0.0
  *
  * @author Aaron M. Renn (arenn@urbanophile.com)
  */
public class EncodingManager {

	/*************************************************************************/

	/*
	 * Class Variables
	 */

	/**
	  * This is the encoding class search path
	  */
	private static String encoding_path;

	/**
	  * This is the system default character encoding
	  */
	//private static String default_encoding;

	/**
	  * This is the <code>Constructor</code> for the default <code>Decoder</code>
	  */
	private static Constructor default_decoder_cons;

	/**
	  * This is the <code>Constructor</code> for the default <code>Encoder</code>
	  */
	private static Constructor default_encoder_cons;

	/**
	  * This is the default instance of the default <code>Decoder</code>, put
	  * here to make access even faster than through the Hashtable
	  */
	private static Decoder default_decoder_instance;

	/**
	  * This is the default instance of the default <code>Encoder</code>, put
	  * here to make access even faster than through the Hashtable
	  */
	private static Encoder default_encoder_instance;

	/**
	  * This is our hash table of previously loaded <code>Decoder</code> classes
	  */
	private static Hashtable decoder_cons;

	/**
	  * This is hash table of cached instances of <code>Decoder</code> objects
	  */
	private static Hashtable decoder_instances;

	/**
	  * This is our hash table of previously loaded <code>Encoder</code> classes
	  */
	private static Hashtable encoder_cons;

	/**
	  * This is hash table of cached instances of <code>Encoder</code> objects
	  */
	private static Hashtable encoder_instances;

	static {
		// Initialize hashtables
		decoder_cons = new Hashtable();
		encoder_cons = new Hashtable();
		decoder_instances = new Hashtable();
		encoder_instances = new Hashtable();

		// Find the system default decoder search path
		encoding_path = System.getProperty("file.encoding.pkg");
		if (encoding_path == null)
			encoding_path = "gnu.java.io";
		else
			encoding_path = encoding_path + ":gnu.java.io";

		// Find the system default encoding name
		String default_encoding = System.getProperty("file.encoding", "8859_1");

		// Load the class
		try {
			// First the Decoder side
			default_decoder_cons = findDecoderConstructor(default_encoding, true);

			Object[] objs = new Object[1];
			objs[0] = null;

			default_decoder_instance = (Decoder) default_decoder_cons.newInstance(objs);

			// Now the Encoder side
			default_encoder_cons = findEncoderConstructor(default_encoding, true);

			objs = new Object[1];
			objs[0] = null;

			default_encoder_instance = (Encoder) default_encoder_cons.newInstance(objs);

			// Add items to the hashtable;
			decoder_cons.put(default_encoding, default_decoder_cons);
			encoder_cons.put(default_encoding, default_encoder_cons);
			decoder_instances.put(default_encoding, default_decoder_instance);
			encoder_instances.put(default_encoding, default_encoder_instance);
		} catch (Exception e) {
			if (System.getProperty("org.jnode.buildtime") != null) {
				System.out.println("Warning: cannot load system default encoding '" + default_encoding + "': " + e.getMessage());
			} else {
				throw new Error("Cannot load system default encoding '" + default_encoding + "': " + e.getMessage(), e);
			}
		}
	}

	/*************************************************************************/

	/*
	 * Class Methods
	 */

	/**
	  * This method loads a <code>Decoder</code> class for the given
	  * encoding name.
	  *
	  * @exception UnsupportedEncodingException If a <code>Decoder</code> for this encoding cannot be found.
	  */
	private static Constructor findDecoderConstructor(String encoding, boolean cache) throws UnsupportedEncodingException {
		// First check for an aliased encoding name
		String alias = System.getProperty("gnu.java.io.encoding_scheme_alias." + encoding);
		if (alias != null) {
			encoding = alias;
		}

		StringTokenizer st = new StringTokenizer(encoding_path, ":");

		while (st.hasMoreTokens()) {
			String classname = st.nextToken() + ".decode.Decoder" + cleanEncoding(encoding);
			try {
				Class cls = Class.forName(classname);

				Class[] params = new Class[1];
				params[0] = Class.forName("java.io.InputStream");

				Constructor cons = cls.getConstructor(params);

				if (cache) {
					decoder_cons.put(encoding, cons);
				}

				return (cons);
			} catch (Exception e) {
				throw (UnsupportedEncodingException)new UnsupportedEncodingException(encoding).initCause(e);
			}
		}
		throw new UnsupportedEncodingException(encoding);
	}

	/*************************************************************************/

	/**
	  * This method loads an <code>Encoder</code> class for the given
	  * encoding name.
	  *
	  * @exception UnsupportedEncodingException If a <code>Encoder</code> for this encoding cannot be found.
	  */
	private static Constructor findEncoderConstructor(String encoding, boolean cache) throws UnsupportedEncodingException {
		// First check for an aliased encoding name
		String alias = System.getProperty("gnu.java.io.encoding_scheme_alias." + encoding);
		if (alias != null)
			encoding = alias;

		StringTokenizer st = new StringTokenizer(encoding_path, ":");

		while (st.hasMoreTokens()) {
			String classname = st.nextToken() + ".encode.Encoder" + cleanEncoding(encoding);
			try {
				Class cls = Class.forName(classname);

				Class[] params = new Class[1];
				params[0] = Class.forName("java.io.OutputStream");

				Constructor cons = cls.getConstructor(params);

				if (cache)
					encoder_cons.put(encoding, cons);

				return (cons);
			} catch (Exception e) {
				
			}
		}

		throw new UnsupportedEncodingException(encoding);
	}

	private static String cleanEncoding(String encoding) {
		encoding = encoding.replace('-', '_');
		if (encoding.startsWith("ISO")) {
			encoding = encoding.substring(3);
		}
		return encoding;
	}

	/*************************************************************************/

	/**
	  * This method returns the default instance of the default <code>Decoder</code> 
	  * which must be used only for calling the static byte array conversion methods.
	  * Calling any instance methods on this object will result in a 
	  * <code>NullPointerException</code>.
	  *
	  * @return An instance of the default <code>Decoder</code>.
	  */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?