FileDocCategorySizeDatePackage
EncodingInfo.javaAPI DocJava SE 5 API8558Fri Aug 26 14:56:02 BST 2005com.sun.org.apache.xml.internal.serialize

EncodingInfo

public class EncodingInfo extends Object
This class represents an encoding.
version
$Id: EncodingInfo.java,v 1.2 2003/12/05 10:08:55 vk112360 Exp $

Fields Summary
private static Method
fgGetConverterMethod
private static Method
fgCanConvertMethod
private static boolean
fgConvertersAvailable
private Object[]
fArgsForMethod
String
ianaName
String
javaName
int
lastPrintable
Object
fCharToByteConverter
boolean
fHaveTriedCToB
Charset
nioCharset
CharsetEncoder
nioCharEncoder
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;
		try{
			 nioCharset = Charset.forName(this.javaName);
			 if(nioCharset.canEncode())
			 	nioCharEncoder = nioCharset.newEncoder();
		}catch(IllegalCharsetNameException ie){
			nioCharset = null;
			nioCharEncoder = null;
		}catch(UnsupportedCharsetException ue){
			nioCharset = null;
			nioCharEncoder = null;
		}
    
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;
		if(nioCharEncoder != null)
			return nioCharEncoder.canEncode(ch);

		//We should not reach here , if we reach due to 
		//charset not supporting encoding then fgConvertersAvailable 
		//should take care of returning false.

        if(fCharToByteConverter == null) {
            if(fHaveTriedCToB || !fgConvertersAvailable) {
                // forget it; nothing we can do...
                return false;
            }
            if (fArgsForMethod == null) {
                fArgsForMethod = new Object [1];
            }
            // try and create it:
            try {
                fArgsForMethod[0] = javaName;
                fCharToByteConverter = fgGetConverterMethod.invoke(null, fArgsForMethod);
            } catch(Exception e) {   
                // don't try it again...
                fHaveTriedCToB = true;
                return false;
            }
        }
        try {
            fArgsForMethod[0] = new Character(ch);
            return ((Boolean) 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);