chartobyteconverter.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 415 行 · 第 1/2 页

JAVA
415
字号
/* * @(#)CharToByteConverter.java	1.47 06/10/10 * * Copyright  1990-2008 Sun Microsystems, Inc. All Rights Reserved.   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER   *    * This program is free software; you can redistribute it and/or   * modify it under the terms of the GNU General Public License version   * 2 only, as published by the Free Software Foundation.    *    * This program 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 version 2 for more details (a copy is   * included at /legal/license.txt).    *    * You should have received a copy of the GNU General Public License   * version 2 along with this work; if not, write to the Free Software   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA   * 02110-1301 USA    *    * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa   * Clara, CA 95054 or visit www.sun.com if you need additional   * information or have any questions.  * */package sun.io;import java.io.*;/** * An abstract base class for subclasses which convert Unicode * characters into an external encoding. * * @author Asmus Freytag * @author Lloyd Honomichl, Novell, Inc. */public abstract class CharToByteConverter {    /**     * Substitution mode flag.     */    protected boolean subMode = true;	    /**     * Bytes to substitute for unmappable input.     */    protected byte[] subBytes = { (byte)'?' };    /**     * Offset of next character to be converted.     */    protected int charOff;    /**     * Offset of next byte to be output.     */    protected int byteOff;    /**     * Length of bad input that caused conversion to stop.     */    protected int badInputLength;    /**     * Create an instance of the default CharToByteConverter subclass.     */    public static CharToByteConverter getDefault() {	Object cvt;	cvt = Converters.newDefaultConverter(Converters.CHAR_TO_BYTE);	return (CharToByteConverter)cvt;    }    /**     * Returns appropriate CharToByteConverter subclass instance.     * @param string represets encoding     */    public static CharToByteConverter getConverter(String encoding)	throws UnsupportedEncodingException    {	Object cvt;	cvt = Converters.newConverter(Converters.CHAR_TO_BYTE, encoding);	return (CharToByteConverter)cvt;    }    /**     * Returns the character set id for the conversion.     */    public abstract String getCharacterEncoding();    /**     * Converts an array of Unicode characters into an array of bytes     * in the target character encoding.  This method allows a buffer by     * buffer conversion of a data stream.  The state of the conversion is     * saved between calls to convert.  If a call to convert results in     * an exception, the conversion may be continued by calling convert again     * with suitably modified parameters.  All conversions should be finished     * with a call to the flush method.     *     * @return the number of bytes written to output.     * @param input array containing Unicode characters to be converted.     * @param inStart begin conversion at this offset in input array.     * @param inEnd stop conversion at this offset in input array (exclusive).     * @param output byte array to receive conversion result.     * @param outStart start writing to output array at this offset.     * @param outEnd stop writing to output array at this offset (exclusive).     * @exception MalformedInputException if the input buffer contains any     * sequence of chars that is illegal in Unicode (principally unpaired     * surrogates and \uFFFF or \uFFFE). After this exception is thrown,     * the method nextCharIndex can be called to obtain the index of the     * first invalid input character.  The MalformedInputException can     * be queried for the length of the invalid input.     * @exception UnknownCharacterException for any character that     * that cannot be converted to the external character encoding. Thrown     * only when converter is not in substitution mode.     * @exception ConversionBufferFullException if output array is filled prior     * to converting all the input.     */    public abstract int convert(char[] input, int inStart, int inEnd,				byte[] output, int outStart, int outEnd)	throws MalformedInputException,               UnknownCharacterException,               ConversionBufferFullException;    /*      * Converts any array of characters, including malformed surrogate     * pairs, into an array of bytes in the target character encoding.      * A precondition is that substitution mode is turned on. This method     * allows a buffer by buffer conversion of a data stream.       * The state of the conversion is saved between calls to convert.      * All conversions should be finished with a call to the flushAny method.     *     * @return the number of bytes written to output.     * @param input array containing Unicode characters to be converted.     * @param inStart begin conversion at this offset in input array.     * @param inEnd stop conversion at this offset in input array (exclusive).     * @param output byte array to receive conversion result.     * @param outStart start writing to output array at this offset.     * @param outEnd stop writing to output array at this offset (exclusive).     * @exception ConversionBufferFullException if output array is filled prior     * to converting all the input.     */    public int convertAny(char[] input, int inStart, int inEnd,			  byte[] output, int outStart, int outEnd)	throws ConversionBufferFullException     {	if (!subMode) { 	    /* Precondition: subMode == true */	    throw new IllegalStateException("Substitution mode is not on");	}	/* Rely on the untested precondition that the indices are meaningful */	/* For safety, use the public interface to charOff and byteOff, but	   badInputLength is directly modified.*/	int localInOff = inStart;	int localOutOff = outStart;	while(localInOff < inEnd) {	    try {		int discard = convert(input, localInOff, inEnd, 				      output, localOutOff, outEnd);		return (nextByteIndex() - outStart);	    } catch (MalformedInputException e) {		int subSize = subBytes.length;		localOutOff = nextByteIndex();				if ((localOutOff + subSize) > outEnd)		    throw new ConversionBufferFullException();		for (int i = 0; i < subSize; i++) 		    output[localOutOff++] = subBytes[i];		localInOff = nextCharIndex();		localInOff += badInputLength;  		badInputLength = 0;		if (localInOff >= inEnd){                    byteOff = localOutOff;                    return (byteOff - outStart);		}		continue;	    }catch (UnknownCharacterException e) {		/* Should never occur, since subMode == true */		throw new InternalError("UnknownCharacterException thrown " + 					"in substititution mode");	    }	}	return (nextByteIndex() - outStart);    }    /**     * Converts an array of Unicode characters into an array of bytes     * in the target character encoding.  Unlike convert, this method     * does not do incremental conversion.  It assumes that the given     * input array contains all the characters to be converted. The     * state of the converter is reset at the beginning of this method     * and is left in the reset state on successful termination.     * The converter is not reset if an exception is thrown.     * This allows the caller to determine where the bad input     * was encountered by calling nextCharIndex.     * <p>     * This method uses substitution mode when performing the conversion.     * The method setSubstitutionBytes may be used to determine what     * bytes are substituted.  Even though substitution mode is used,     * the state of the converter's substitution mode is not changed     * at the end of this method.     *     * @return an array of bytes containing the converted characters.     * @param input array containing Unicode characters to be converted.     * @exception MalformedInputException if the input buffer contains any     * sequence of chars that is illegal in Unicode (principally unpaired

⌨️ 快捷键说明

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