EncodingInfopublic class EncodingInfo extends Object This class represents an encoding. |
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.String | getIANAName()Returns a MIME charset name of this encoding.
return this.ianaName;
| public java.io.Writer | getWriter(java.io.OutputStream output)Returns a writer for this encoding based on
an output stream.
// 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 boolean | isPrintable(char ch)Checks whether the specified character is printable or not in this encoding.
if (ch <= this.lastPrintable) {
return true;
}
return isPrintable0(ch);
| private boolean | isPrintable0(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.
// 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 void | testJavaEncodingName(java.lang.String name)
final byte [] bTest = {(byte)'v", (byte)'a", (byte)'l", (byte)'i", (byte)'d"};
String s = new String(bTest, name);
|
|