StringCodingpublic class StringCoding extends Object Utility class for string encoding and decoding. |
Fields Summary |
---|
private static ThreadLocal | decoder | private static ThreadLocal | encoder | private static boolean | warnUnsupportedCharset |
Constructors Summary |
---|
private StringCoding()
|
Methods Summary |
---|
static char[] | decode(byte[] ba, int off, int len)
String csn = Converters.getDefaultEncodingName();
try {
return decode(csn, ba, off, len);
} catch (UnsupportedEncodingException x) {
Converters.resetDefaultEncodingName();
warnUnsupportedCharset(csn);
}
try {
return decode("ISO-8859-1", ba, off, len);
} catch (UnsupportedEncodingException x) {
// If this code is hit during VM initialization, MessageUtils is
// the only way we will be able to get any kind of error message.
MessageUtils.err("ISO-8859-1 charset not available: "
+ x.toString());
// If we can not find ISO-8859-1 (a required encoding) then things
// are seriously wrong with the installation.
System.exit(1);
return null;
}
| static char[] | decode(java.lang.String charsetName, byte[] ba, int off, int len)
StringDecoder sd = (StringDecoder)deref(decoder);
String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
if ((sd == null) || !(csn.equals(sd.requestedCharsetName())
|| csn.equals(sd.charsetName()))) {
sd = null;
try {
Charset cs = lookupCharset(csn);
if (cs != null)
sd = new CharsetSD(cs, csn);
else
sd = null;
} catch (IllegalCharsetNameException x) {
// FALL THROUGH to ByteToCharConverter, for compatibility
}
if (sd == null)
sd = new ConverterSD(ByteToCharConverter.getConverter(csn),
csn);
set(decoder, sd);
}
return sd.decode(ba, off, len);
| private static java.lang.Object | deref(java.lang.ThreadLocal tl)
SoftReference sr = (SoftReference)tl.get();
if (sr == null)
return null;
return sr.get();
| static byte[] | encode(java.lang.String charsetName, char[] ca, int off, int len)
StringEncoder se = (StringEncoder)deref(encoder);
String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
if ((se == null) || !(csn.equals(se.requestedCharsetName())
|| csn.equals(se.charsetName()))) {
se = null;
try {
Charset cs = lookupCharset(csn);
if (cs != null)
se = new CharsetSE(cs, csn);
} catch (IllegalCharsetNameException x) {
// FALL THROUGH to CharToByteConverter, for compatibility
}
if (se == null)
se = new ConverterSE(CharToByteConverter.getConverter(csn),
csn);
set(encoder, se);
}
return se.encode(ca, off, len);
| static byte[] | encode(char[] ca, int off, int len)
String csn = Converters.getDefaultEncodingName();
try {
return encode(csn, ca, off, len);
} catch (UnsupportedEncodingException x) {
Converters.resetDefaultEncodingName();
warnUnsupportedCharset(csn);
}
try {
return encode("ISO-8859-1", ca, off, len);
} catch (UnsupportedEncodingException x) {
// If this code is hit during VM initialization, MessageUtils is
// the only way we will be able to get any kind of error message.
MessageUtils.err("ISO-8859-1 charset not available: "
+ x.toString());
// If we can not find ISO-8859-1 (a required encoding) then things
// are seriously wrong with the installation.
System.exit(1);
return null;
}
| private static java.nio.charset.Charset | lookupCharset(java.lang.String csn)
if (Charset.isSupported(csn)) {
try {
return Charset.forName(csn);
} catch (UnsupportedCharsetException x) {
throw new Error(x);
}
}
return null;
| private static int | scale(int len, float expansionFactor)
// We need to perform double, not float, arithmetic; otherwise
// we lose low order bits when len is larger than 2**24.
return (int)(len * (double)expansionFactor);
| private static void | set(java.lang.ThreadLocal tl, java.lang.Object ob)
tl.set(new SoftReference(ob));
| private static byte[] | trim(byte[] ba, int len)
if (len == ba.length)
return ba;
byte[] tba = new byte[len];
System.arraycopy(ba, 0, tba, 0, len);
return tba;
| private static char[] | trim(char[] ca, int len)
if (len == ca.length)
return ca;
char[] tca = new char[len];
System.arraycopy(ca, 0, tca, 0, len);
return tca;
| private static void | warnUnsupportedCharset(java.lang.String csn)
if (warnUnsupportedCharset) {
// Use sun.misc.MessageUtils rather than the Logging API or
// System.err since this method may be called during VM
// initialization before either is available.
MessageUtils.err("WARNING: Default charset " + csn +
" not supported, using ISO-8859-1 instead");
warnUnsupportedCharset = false;
}
|
|