📄 helper.java
字号:
/* * Copyright (c) 1999-2001 Sun Microsystems, Inc., 901 San Antonio Road, * Palo Alto, CA 94303, U.S.A. All Rights Reserved. * * Sun Microsystems, Inc. has intellectual property rights relating * to the technology embodied in this software. In particular, and * without limitation, these intellectual property rights may include * one or more U.S. patents, foreign patents, or pending * applications. Sun, Sun Microsystems, the Sun logo, Java, KJava, * and all Sun-based and Java-based marks are trademarks or * registered trademarks of Sun Microsystems, Inc. in the United * States and other countries. * * This software is distributed under licenses restricting its use, * copying, distribution, and decompilation. No part of this * software may be reproduced in any form by any means without prior * written authorization of Sun and its licensors, if any. * * FEDERAL ACQUISITIONS: Commercial Software -- Government Users * Subject to Standard License Terms and Conditions */package com.sun.cldc.i18n;import java.io.*;/** * This class provides general helper functions for the J2ME environment. * <p> * <em>No application code should reference this class directly.</em> * * @version 2.0 05/04/01 */public class Helper { /** * The name of the default character encoding */ private static String defaultEncoding; /** * Default path to the J2ME classes */ private static String defaultMEPath; /** * True if we are running on a J2ME system */ private static boolean j2me = false; /** * Class initializer */ static { /* Get the default encoding name */ defaultEncoding = System.getProperty("microedition.encoding"); if (defaultEncoding == null) { defaultEncoding = "ISO8859_1"; } /* Work out if we are running on a J2ME system */ if (System.getProperty("microedition.configuration") != null) { j2me = true; } /* Get the default encoding name */ defaultMEPath = System.getProperty("com.sun.cldc.i18n.Helper.i18npath"); if (defaultMEPath == null) { defaultMEPath = "com.sun.cldc.i18n.j2me"; } }/*------------------------------------------------------------------------------*//* Character encoding *//*------------------------------------------------------------------------------*/ /** * Get a reader for an InputStream * * @param is The input stream the reader is for * @return A new reader for the stream */ public static Reader getStreamReader(InputStream is) { try { return getStreamReader(is, defaultEncoding); } catch(UnsupportedEncodingException x) { try { defaultEncoding = "ISO8859_1"; return getStreamReader(is, defaultEncoding); } catch(UnsupportedEncodingException e) { throw new RuntimeException("Missing default encoding "+defaultEncoding); } } } /** * Get a reader for an InputStream * * @param is The input stream the reader is for * @param name The name of the decoder * @return A new reader for the stream * @exception UnsupportedEncodingException If the encoding is not known */ public static Reader getStreamReader(InputStream is, String name) throws UnsupportedEncodingException { /* Test for null arguments */ if(is == null || name == null) { throw new NullPointerException(); } /* Get the reader from the encoding */ StreamReader fr = getStreamReaderPrim(name); /* Open the connection and return*/ return fr.open(is, name); } /** * Get a reader for an InputStream * * @param is The input stream the reader is for * @param name The name of the decoder * @return A new reader for the stream * @exception UnsupportedEncodingException If the encoding is not known */ private static StreamReader getStreamReaderPrim(String name) throws UnsupportedEncodingException { if(name == null) { throw new NullPointerException(); } name = internalNameForEncoding(name); try { String className; /* Get the reader class name */ if(j2me) { className = defaultMEPath + '.' + name + "_Reader"; } else { className = "com.sun.cldc.i18n.j2se.Default_Reader"; } /* Using the decoder names lookup a class to implement the reader */ Class clazz = Class.forName(className); /* Return a new instance */ return (StreamReader)clazz.newInstance(); } catch(ClassNotFoundException x) { throw new UnsupportedEncodingException("Encoding "+name+" not found"); } catch(InstantiationException x) { throw new RuntimeException("InstantiationException "+x.getMessage()); } catch(IllegalAccessException x) { throw new RuntimeException("IllegalAccessException "+x.getMessage()); } catch(ClassCastException x) { throw new RuntimeException("ClassCastException "+x.getMessage()); } } /** * Get a writer for an OutputStream * * @param os The output stream the reader is for * @return A new writer for the stream */ public static Writer getStreamWriter(OutputStream os) { try { return getStreamWriter(os, defaultEncoding); } catch(UnsupportedEncodingException x) { try { defaultEncoding = "ISO8859_1"; return getStreamWriter(os, defaultEncoding); } catch(UnsupportedEncodingException e) { throw new RuntimeException("Missing default encoding "+defaultEncoding); } } } /** * Get a writer for an OutputStream * * @param os The output stream the reader is for * @param name The name of the decoder * @return A new writer for the stream * @exception UnsupportedEncodingException If the encoding is not known */ public static Writer getStreamWriter(OutputStream os, String name) throws UnsupportedEncodingException { /* Test for null arguments */ if(os == null || name == null) { throw new NullPointerException(); } /* Get the writer from the encoding */ StreamWriter sw = getStreamWriterPrim(name); /* Open it on the output stream and return */ return sw.open(os, name); } /** * Get a writer for an OutputStream * * @param os The output stream the reader is for * @param name The name of the decoder * @return A new writer for the stream * @exception UnsupportedEncodingException If the encoding is not known */ private static StreamWriter getStreamWriterPrim(String name) throws UnsupportedEncodingException { if(name == null) { throw new NullPointerException(); } name = internalNameForEncoding(name); try { String className; /* Get the writer class name */ if(j2me) { className = defaultMEPath + '.' + name +"_Writer"; } else { className = "com.sun.cldc.i18n.j2se.Default_Writer"; } /* Using the decoder names lookup a class to implement the writer */ Class clazz = Class.forName(className);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -