1 57 58 59 package com.sun.org.apache.xml.internal.serialize; 60 61 import java.io.OutputStream ; 62 import java.io.OutputStreamWriter ; 63 import java.io.UnsupportedEncodingException ; 64 import java.io.Writer ; 65 import com.sun.org.apache.xerces.internal.util.EncodingMap; 66 import java.nio.charset.Charset ; 67 import java.nio.charset.CharsetEncoder ; 68 import java.nio.charset.UnsupportedCharsetException ; 69 import java.nio.charset.IllegalCharsetNameException ; 70 75 public class EncodingInfo { 76 77 private static java.lang.reflect.Method fgGetConverterMethod = null; 79 80 private static java.lang.reflect.Method fgCanConvertMethod = null; 82 83 private static boolean fgConvertersAvailable = false; 85 86 private Object [] fArgsForMethod = null; 88 89 String ianaName; 92 String javaName; 93 int lastPrintable; 94 95 Object fCharToByteConverter = null; 97 98 boolean fHaveTriedCToB = false; 102 Charset nioCharset = null; 103 CharsetEncoder nioCharEncoder = null; 104 107 public EncodingInfo(String ianaName, String javaName, int lastPrintable) { 108 this.ianaName = ianaName; 109 this.javaName = EncodingMap.getIANA2JavaMapping(ianaName); 110 this.lastPrintable = lastPrintable; 111 try{ 112 nioCharset = Charset.forName(this.javaName); 113 if(nioCharset.canEncode()) 114 nioCharEncoder = nioCharset.newEncoder(); 115 }catch(IllegalCharsetNameException ie){ 116 nioCharset = null; 117 nioCharEncoder = null; 118 }catch(UnsupportedCharsetException ue){ 119 nioCharset = null; 120 nioCharEncoder = null; 121 } 122 } 123 124 127 public String getIANAName() { 128 return this.ianaName; 129 } 130 131 139 public Writer getWriter(OutputStream output) 140 throws UnsupportedEncodingException { 141 if (javaName != null) 143 return new OutputStreamWriter (output, javaName); 144 javaName = EncodingMap.getIANA2JavaMapping(ianaName); 145 if(javaName == null) 146 return new OutputStreamWriter (output, "UTF8"); 148 return new OutputStreamWriter (output, javaName); 149 } 150 156 public boolean isPrintable(char ch) { 157 if(ch <= this.lastPrintable) 158 return true; 159 if(nioCharEncoder != null) 160 return nioCharEncoder.canEncode(ch); 161 162 166 if(fCharToByteConverter == null) { 167 if(fHaveTriedCToB || !fgConvertersAvailable) { 168 return false; 170 } 171 if (fArgsForMethod == null) { 172 fArgsForMethod = new Object [1]; 173 } 174 try { 176 fArgsForMethod[0] = javaName; 177 fCharToByteConverter = fgGetConverterMethod.invoke(null, fArgsForMethod); 178 } catch(Exception e) { 179 fHaveTriedCToB = true; 181 return false; 182 } 183 } 184 try { 185 fArgsForMethod[0] = new Character (ch); 186 return ((Boolean ) fgCanConvertMethod.invoke(fCharToByteConverter, fArgsForMethod)).booleanValue(); 187 } catch (Exception e) { 188 fCharToByteConverter = null; 191 fHaveTriedCToB = false; 192 return false; 193 } 194 } 195 196 public static void testJavaEncodingName(String name) throws UnsupportedEncodingException { 199 final byte [] bTest = {(byte)'v', (byte)'a', (byte)'l', (byte)'i', (byte)'d'}; 200 String s = new String (bTest, name); 201 } 202 203 static { 206 try { 207 Class clazz = Class.forName("sun.io.CharToByteConverter"); 208 fgGetConverterMethod = clazz.getMethod("getConverter", new Class [] {String .class}); 209 fgCanConvertMethod = clazz.getMethod("canConvert", new Class [] {Character.TYPE}); 210 fgConvertersAvailable = true; 211 } 212 catch (Exception exc) { 215 fgGetConverterMethod = null; 216 fgCanConvertMethod = null; 217 fgConvertersAvailable = false; 218 } 219 } 220 } 221 | Popular Tags |