FileDocCategorySizeDatePackage
EncodingInfo.javaAPI DocJava SE 6 API10844Tue Jun 10 00:23:06 BST 2008com.sun.org.apache.xml.internal.serialize

EncodingInfo

public class EncodingInfo extends Object
This class represents an encoding.
version
$Id: EncodingInfo.java,v 1.2.6.1 2005/09/09 07:26:14 neerajbj Exp $

Fields Summary
private Object[]
fArgsForMethod
String
ianaName
String
javaName
int
lastPrintable
Object
fCharsetEncoder
Object
fCharToByteConverter
boolean
fHaveTriedCToB
boolean
fHaveTriedCharsetEncoder
Constructors Summary
public EncodingInfo(String ianaName, String javaName, int lastPrintable)
Creates new EncodingInfo instance.


             
           
        this.ianaName = ianaName;
        this.javaName = EncodingMap.getIANA2JavaMapping(ianaName);
        this.lastPrintable = lastPrintable;
    
Methods Summary
public java.lang.StringgetIANAName()
Returns a MIME charset name of this encoding.

        return this.ianaName;
    
public java.io.WritergetWriter(java.io.OutputStream output)
Returns a writer for this encoding based on an output stream.

return
A suitable writer
exception
UnsupportedEncodingException There is no convertor to support this encoding

        // this should always be true!
        if (javaName != null) 
            return new OutputStreamWriter(output, javaName);
        javaName = EncodingMap.getIANA2JavaMapping(ianaName);
        if(javaName == null) 
            // use UTF-8 as preferred encoding
            return new OutputStreamWriter(output, "UTF8");
        return new OutputStreamWriter(output, javaName);
    
public booleanisPrintable(char ch)
Checks whether the specified character is printable or not in this encoding.

param
ch a code point (0-0x10ffff)

        if (ch <= this.lastPrintable) {
            return true;
        }
        return isPrintable0(ch);
    
private booleanisPrintable0(char ch)
Checks whether the specified character is printable or not in this encoding. This method accomplishes this using a java.nio.CharsetEncoder. If NIO isn't available it will attempt use a sun.io.CharToByteConverter.

param
ch a code point (0-0x10ffff)

        
        // Attempt to get a CharsetEncoder for this encoding.
        if (fCharsetEncoder == null && CharsetMethods.fgNIOCharsetAvailable && !fHaveTriedCharsetEncoder) {
            if (fArgsForMethod == null) {
                fArgsForMethod = new Object [1];
            }
            // try and create the CharsetEncoder
            try {
                fArgsForMethod[0] = javaName;
                Object charset = CharsetMethods.fgCharsetForNameMethod.invoke(null, fArgsForMethod);
                if (((Boolean) CharsetMethods.fgCharsetCanEncodeMethod.invoke(charset, (Object[]) null)).booleanValue()) {
                    fCharsetEncoder = CharsetMethods.fgCharsetNewEncoderMethod.invoke(charset, (Object[]) null);
                }
                // This charset cannot be used for encoding, don't try it again...
                else {
                    fHaveTriedCharsetEncoder = true;
                } 
            } 
            catch (Exception e) {   
                // don't try it again...
                fHaveTriedCharsetEncoder = true;
            }
        }
        // Attempt to use the CharsetEncoder to determine whether the character is printable.
        if (fCharsetEncoder != null) {
            try {
                fArgsForMethod[0] = new Character(ch);
                return ((Boolean) CharsetMethods.fgCharsetEncoderCanEncodeMethod.invoke(fCharsetEncoder, fArgsForMethod)).booleanValue();
            } 
            catch (Exception e) {
                // obviously can't use this charset encoder; possibly a JDK bug
                fCharsetEncoder = null;
                fHaveTriedCharsetEncoder = false;
            }
        }
        
        // As a last resort try to use a sun.io.CharToByteConverter to
        // determine whether this character is printable. We will always
        // reach here on JDK 1.3 or below.
        if (fCharToByteConverter == null) {
            if (fHaveTriedCToB || !CharToByteConverterMethods.fgConvertersAvailable) {
                // forget it; nothing we can do...
                return false;
            }
            if (fArgsForMethod == null) {
                fArgsForMethod = new Object [1];
            }
            // try and create the CharToByteConverter
            try {
                fArgsForMethod[0] = javaName;
                fCharToByteConverter = CharToByteConverterMethods.fgGetConverterMethod.invoke(null, fArgsForMethod);
            } 
            catch (Exception e) {   
                // don't try it again...
                fHaveTriedCToB = true;
                return false;
            }
        }
        try {
            fArgsForMethod[0] = new Character(ch);
            return ((Boolean) CharToByteConverterMethods.fgCanConvertMethod.invoke(fCharToByteConverter, fArgsForMethod)).booleanValue();
        } 
        catch (Exception e) {
            // obviously can't use this converter; probably some kind of
            // security restriction
            fCharToByteConverter = null;
            fHaveTriedCToB = false;
            return false;
        }
    
public static voidtestJavaEncodingName(java.lang.String name)

        final byte [] bTest = {(byte)'v", (byte)'a", (byte)'l", (byte)'i", (byte)'d"};
        String s = new String(bTest, name);