/*
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 2000-2002,2004,2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This class represents an encoding.
*
* @version $Id: EncodingInfo.java,v 1.6 2007/10/18 03:39:08 joehw Exp $
*/
public class EncodingInfo {
// An array to hold the argument for a method of Charset, CharsetEncoder or CharToByteConverter.
// name of encoding as registered with IANA;
// preferably a MIME name, but aliases are fine too.
int lastPrintable;
// The CharsetEncoder with which we test unusual characters.
// The CharToByteConverter with which we test unusual characters.
// Is the converter null because it can't be instantiated
// for some reason (perhaps we're running with insufficient authority as
// an applet?
boolean fHaveTriedCToB = false;
// Is the charset encoder usable or available.
boolean fHaveTriedCharsetEncoder = false;
/**
* Creates new <code>EncodingInfo</code> instance.
*/
this.lastPrintable = lastPrintable;
}
/**
* Returns a MIME charset name of this encoding.
*/
return this.ianaName;
}
/**
* 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
*/
throws UnsupportedEncodingException {
// this should always be true!
// use UTF-8 as preferred encoding
}
/**
* 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);
}
/**
* 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 (fArgsForMethod == null) {
}
// try and create the CharsetEncoder
try {
if (((Boolean) CharsetMethods.fgCharsetCanEncodeMethod.invoke(charset, (Object[]) null)).booleanValue()) {
}
// 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 {
return ((Boolean) CharsetMethods.fgCharsetEncoderCanEncodeMethod.invoke(fCharsetEncoder, fArgsForMethod)).booleanValue();
}
catch (Exception e) {
// obviously can't use this charset encoder; possibly a JDK bug
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) {
// forget it; nothing we can do...
return false;
}
if (fArgsForMethod == null) {
}
// try and create the CharToByteConverter
try {
fCharToByteConverter = CharToByteConverterMethods.fgGetConverterMethod.invoke(null, fArgsForMethod);
}
catch (Exception e) {
// don't try it again...
fHaveTriedCToB = true;
return false;
}
}
try {
return ((Boolean) CharToByteConverterMethods.fgCanConvertMethod.invoke(fCharToByteConverter, fArgsForMethod)).booleanValue();
}
catch (Exception e) {
// obviously can't use this converter; probably some kind of
// security restriction
fHaveTriedCToB = false;
return false;
}
}
// is this an encoding name recognized by this JDK?
// if not, will throw UnsupportedEncodingException
}
/**
* Holder of methods from java.nio.charset.Charset and java.nio.charset.CharsetEncoder.
*/
static class CharsetMethods {
// Method: java.nio.charset.Charset.forName(java.lang.String)
// Method: java.nio.charset.Charset.canEncode()
// Method: java.nio.charset.Charset.newEncoder()
// Method: java.nio.charset.CharsetEncoder.canEncode(char)
// Flag indicating whether or not java.nio.charset.* is available.
private static boolean fgNIOCharsetAvailable = false;
private CharsetMethods() {}
// Attempt to get methods for Charset and CharsetEncoder on class initialization.
static {
try {
fgCharsetEncoderCanEncodeMethod = charsetEncoderClass.getMethod("canEncode", new Class [] {Character.TYPE});
fgNIOCharsetAvailable = true;
}
// ClassNotFoundException, NoSuchMethodException or SecurityException
// Whatever the case, we cannot use java.nio.charset.*.
fgNIOCharsetAvailable = false;
}
}
}
/**
* Holder of methods from sun.io.CharToByteConverter.
*/
static class CharToByteConverterMethods {
// Method: sun.io.CharToByteConverter.getConverter(java.lang.String)
// Method: sun.io.CharToByteConverter.canConvert(char)
// Flag indicating whether or not sun.io.CharToByteConverter is available.
private static boolean fgConvertersAvailable = false;
private CharToByteConverterMethods() {}
// Attempt to get methods for char to byte converter on class initialization.
static {
try {
fgConvertersAvailable = true;
}
// ClassNotFoundException, NoSuchMethodException or SecurityException
// Whatever the case, we cannot use sun.io.CharToByteConverter.
fgConvertersAvailable = false;
}
}
}
}