EncodingInfo.java revision 286
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews/*
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * reserved comment block
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * DO NOT REMOVE OR ALTER!
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews */
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews/*
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * Copyright 2000-2002,2004,2005 The Apache Software Foundation.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * Licensed under the Apache License, Version 2.0 (the "License");
15a44745412679c30a6d022733925af70a38b715David Lawrence * you may not use this file except in compliance with the License.
15a44745412679c30a6d022733925af70a38b715David Lawrence * You may obtain a copy of the License at
15a44745412679c30a6d022733925af70a38b715David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * http://www.apache.org/licenses/LICENSE-2.0
15a44745412679c30a6d022733925af70a38b715David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * Unless required by applicable law or agreed to in writing, software
15a44745412679c30a6d022733925af70a38b715David Lawrence * distributed under the License is distributed on an "AS IS" BASIS,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * See the License for the specific language governing permissions and
ae114ded82e773a4d9058f833f964a17514712a8Brian Wellington * limitations under the License.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews */
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halleypackage com.sun.org.apache.xml.internal.serialize;
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halleyimport java.io.OutputStream;
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halleyimport java.io.OutputStreamWriter;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrewsimport java.io.UnsupportedEncodingException;
854d0238dbc2908490197984b3b9d558008a53dfMark Andrewsimport java.io.Writer;
854d0238dbc2908490197984b3b9d558008a53dfMark Andrewsimport com.sun.org.apache.xerces.internal.util.EncodingMap;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff/**
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff * This class represents an encoding.
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff *
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * @version $Id: EncodingInfo.java,v 1.6 2007/10/18 03:39:08 joehw Exp $
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews */
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrewspublic class EncodingInfo {
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews // An array to hold the argument for a method of Charset, CharsetEncoder or CharToByteConverter.
63430de3450a99b7ae0cb05a95a26cff8cc66358David Lawrence private Object [] fArgsForMethod = null;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews // name of encoding as registered with IANA;
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley // preferably a MIME name, but aliases are fine too.
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews String ianaName;
ae114ded82e773a4d9058f833f964a17514712a8Brian Wellington String javaName;
ae114ded82e773a4d9058f833f964a17514712a8Brian Wellington int lastPrintable;
ae114ded82e773a4d9058f833f964a17514712a8Brian Wellington
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence // The CharsetEncoder with which we test unusual characters.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence Object fCharsetEncoder = null;
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington // The CharToByteConverter with which we test unusual characters.
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington Object fCharToByteConverter = null;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence // Is the converter null because it can't be instantiated
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews // for some reason (perhaps we're running with insufficient authority as
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews // an applet?
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews boolean fHaveTriedCToB = false;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence // Is the charset encoder usable or available.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence boolean fHaveTriedCharsetEncoder = false;
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington /**
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington * Creates new <code>EncodingInfo</code> instance.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews */
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews public EncodingInfo(String ianaName, String javaName, int lastPrintable) {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence this.ianaName = ianaName;
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence this.javaName = EncodingMap.getIANA2JavaMapping(ianaName);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence this.lastPrintable = lastPrintable;
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington }
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews /**
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * Returns a MIME charset name of this encoding.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence */
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence public String getIANAName() {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence return this.ianaName;
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington }
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews /**
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews * Returns a writer for this encoding based on
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * an output stream.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews *
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * @return A suitable writer
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * @exception UnsupportedEncodingException There is no convertor
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * to support this encoding
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington */
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington public Writer getWriter(OutputStream output)
5d83b561ad7eb84885a8ec63dee4c51b335f067aBrian Wellington throws UnsupportedEncodingException {
5d83b561ad7eb84885a8ec63dee4c51b335f067aBrian Wellington // this should always be true!
5d83b561ad7eb84885a8ec63dee4c51b335f067aBrian Wellington if (javaName != null)
63430de3450a99b7ae0cb05a95a26cff8cc66358David Lawrence return new OutputStreamWriter(output, javaName);
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews javaName = EncodingMap.getIANA2JavaMapping(ianaName);
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews if(javaName == null)
63430de3450a99b7ae0cb05a95a26cff8cc66358David Lawrence // use UTF-8 as preferred encoding
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews return new OutputStreamWriter(output, "UTF8");
63430de3450a99b7ae0cb05a95a26cff8cc66358David Lawrence return new OutputStreamWriter(output, javaName);
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews }
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews /**
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * Checks whether the specified character is printable or not in this encoding.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence *
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * @param ch a code point (0-0x10ffff)
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington */
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington public boolean isPrintable(char ch) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (ch <= this.lastPrintable) {
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews return true;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews return isPrintable0(ch);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence }
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence /**
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * Checks whether the specified character is printable or not in this encoding.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * This method accomplishes this using a java.nio.CharsetEncoder. If NIO isn't
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * available it will attempt use a sun.io.CharToByteConverter.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence *
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * @param ch a code point (0-0x10ffff)
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington */
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington private boolean isPrintable0(char ch) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews // Attempt to get a CharsetEncoder for this encoding.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (fCharsetEncoder == null && CharsetMethods.fgNIOCharsetAvailable && !fHaveTriedCharsetEncoder) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (fArgsForMethod == null) {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence fArgsForMethod = new Object [1];
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence }
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence // try and create the CharsetEncoder
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence try {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews fArgsForMethod[0] = javaName;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews Object charset = CharsetMethods.fgCharsetForNameMethod.invoke(null, fArgsForMethod);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff if (((Boolean) CharsetMethods.fgCharsetCanEncodeMethod.invoke(charset, (Object[]) null)).booleanValue()) {
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence fCharsetEncoder = CharsetMethods.fgCharsetNewEncoderMethod.invoke(charset, (Object[]) null);
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence // This charset cannot be used for encoding, don't try it again...
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews else {
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews fHaveTriedCharsetEncoder = true;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews }
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews catch (Exception e) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews // don't try it again...
b589e90689c6e87bf9608424ca8d99571c18bc61Mark Andrews fHaveTriedCharsetEncoder = true;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews // Attempt to use the CharsetEncoder to determine whether the character is printable.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence if (fCharsetEncoder != null) {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence try {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence fArgsForMethod[0] = new Character(ch);
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews return ((Boolean) CharsetMethods.fgCharsetEncoderCanEncodeMethod.invoke(fCharsetEncoder, fArgsForMethod)).booleanValue();
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews }
5901928ef856543882f38b2318e8991ce2644d2bMark Andrews catch (Exception e) {
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson // obviously can't use this charset encoder; possibly a JDK bug
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews fCharsetEncoder = null;
2cd8a160b9e2c0c7a016b534652b5c909f36ed4aMark Andrews fHaveTriedCharsetEncoder = false;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews }
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews }
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence // As a last resort try to use a sun.io.CharToByteConverter to
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence // determine whether this character is printable. We will always
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews // reach here on JDK 1.3 or below.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (fCharToByteConverter == null) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (fHaveTriedCToB || !CharToByteConverterMethods.fgConvertersAvailable) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews // forget it; nothing we can do...
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews return false;
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence }
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence if (fArgsForMethod == null) {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence fArgsForMethod = new Object [1];
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews // try and create the CharToByteConverter
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews try {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews fArgsForMethod[0] = javaName;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews fCharToByteConverter = CharToByteConverterMethods.fgGetConverterMethod.invoke(null, fArgsForMethod);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence }
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence catch (Exception e) {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence // don't try it again...
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews fHaveTriedCToB = true;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews return false;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews try {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence fArgsForMethod[0] = new Character(ch);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence return ((Boolean) CharToByteConverterMethods.fgCanConvertMethod.invoke(fCharToByteConverter, fArgsForMethod)).booleanValue();
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews catch (Exception e) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews // obviously can't use this converter; probably some kind of
5d83b561ad7eb84885a8ec63dee4c51b335f067aBrian Wellington // security restriction
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews fCharToByteConverter = null;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews fHaveTriedCToB = false;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews return false;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews }
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence // is this an encoding name recognized by this JDK?
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence // if not, will throw UnsupportedEncodingException
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence public static void testJavaEncodingName(String name) throws UnsupportedEncodingException {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews final byte [] bTest = {(byte)'v', (byte)'a', (byte)'l', (byte)'i', (byte)'d'};
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews String s = new String(bTest, name);
fad44a20eede1bbc66716241dede225500c91caaAndreas Gustafsson }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews /**
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * Holder of methods from java.nio.charset.Charset and java.nio.charset.CharsetEncoder.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence */
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence static class CharsetMethods {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley // Method: java.nio.charset.Charset.forName(java.lang.String)
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley private static java.lang.reflect.Method fgCharsetForNameMethod = null;
fad44a20eede1bbc66716241dede225500c91caaAndreas Gustafsson
fad44a20eede1bbc66716241dede225500c91caaAndreas Gustafsson // Method: java.nio.charset.Charset.canEncode()
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson private static java.lang.reflect.Method fgCharsetCanEncodeMethod = null;
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson // Method: java.nio.charset.Charset.newEncoder()
fad44a20eede1bbc66716241dede225500c91caaAndreas Gustafsson private static java.lang.reflect.Method fgCharsetNewEncoderMethod = null;
fad44a20eede1bbc66716241dede225500c91caaAndreas Gustafsson
fad44a20eede1bbc66716241dede225500c91caaAndreas Gustafsson // Method: java.nio.charset.CharsetEncoder.canEncode(char)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence private static java.lang.reflect.Method fgCharsetEncoderCanEncodeMethod = null;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews // Flag indicating whether or not java.nio.charset.* is available.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence private static boolean fgNIOCharsetAvailable = false;
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence private CharsetMethods() {}
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews // Attempt to get methods for Charset and CharsetEncoder on class initialization.
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson static {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews try {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews Class charsetClass = Class.forName("java.nio.charset.Charset");
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence Class charsetEncoderClass = Class.forName("java.nio.charset.CharsetEncoder");
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence fgCharsetForNameMethod = charsetClass.getMethod("forName", new Class [] {String.class});
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence fgCharsetCanEncodeMethod = charsetClass.getMethod("canEncode", new Class [] {});
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley fgCharsetNewEncoderMethod = charsetClass.getMethod("newEncoder", new Class [] {});
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson fgCharsetEncoderCanEncodeMethod = charsetEncoderClass.getMethod("canEncode", new Class [] {Character.TYPE});
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson fgNIOCharsetAvailable = true;
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson }
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson // ClassNotFoundException, NoSuchMethodException or SecurityException
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson // Whatever the case, we cannot use java.nio.charset.*.
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson catch (Exception exc) {
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson fgCharsetForNameMethod = null;
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson fgCharsetCanEncodeMethod = null;
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson fgCharsetEncoderCanEncodeMethod = null;
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson fgCharsetNewEncoderMethod = null;
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson fgNIOCharsetAvailable = false;
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews }
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence /**
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * Holder of methods from sun.io.CharToByteConverter.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews */
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews static class CharToByteConverterMethods {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews // Method: sun.io.CharToByteConverter.getConverter(java.lang.String)
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews private static java.lang.reflect.Method fgGetConverterMethod = null;
ae114ded82e773a4d9058f833f964a17514712a8Brian Wellington
ae114ded82e773a4d9058f833f964a17514712a8Brian Wellington // Method: sun.io.CharToByteConverter.canConvert(char)
ae114ded82e773a4d9058f833f964a17514712a8Brian Wellington private static java.lang.reflect.Method fgCanConvertMethod = null;
94a08e09db3dc844b6ee4841c368a2d7074a9c3fAndreas Gustafsson
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence // Flag indicating whether or not sun.io.CharToByteConverter is available.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence private static boolean fgConvertersAvailable = false;
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence private CharToByteConverterMethods() {}
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews // Attempt to get methods for char to byte converter on class initialization.
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews static {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence try {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews Class clazz = Class.forName("sun.io.CharToByteConverter");
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews fgGetConverterMethod = clazz.getMethod("getConverter", new Class [] {String.class});
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews fgCanConvertMethod = clazz.getMethod("canConvert", new Class [] {Character.TYPE});
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews fgConvertersAvailable = true;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews }
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence // ClassNotFoundException, NoSuchMethodException or SecurityException
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews // Whatever the case, we cannot use sun.io.CharToByteConverter.
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff catch (Exception exc) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews fgGetConverterMethod = null;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews fgCanConvertMethod = null;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews fgConvertersAvailable = false;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews }
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence }
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence }
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence}
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews