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(java.nio.charset.Charset cs, byte[] ba, int off, int len)
StringDecoder sd = new StringDecoder(cs, cs.name());
byte[] b = Arrays.copyOf(ba, ba.length);
return sd.decode(b, off, len);
| static char[] | decode(byte[] ba, int off, int len)
String csn = Charset.defaultCharset().name();
try {
return decode(csn, ba, off, len);
} catch (UnsupportedEncodingException x) {
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 StringDecoder(cs, csn);
} catch (IllegalCharsetNameException x) {}
if (sd == null)
throw new UnsupportedEncodingException(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 StringEncoder(cs, csn);
} catch (IllegalCharsetNameException x) {}
if (se == null)
throw new UnsupportedEncodingException (csn);
set(encoder, se);
}
return se.encode(ca, off, len);
| static byte[] | encode(java.nio.charset.Charset cs, char[] ca, int off, int len)
StringEncoder se = new StringEncoder(cs, cs.name());
char[] c = Arrays.copyOf(ca, ca.length);
return se.encode(c, off, len);
| static byte[] | encode(char[] ca, int off, int len)
String csn = Charset.defaultCharset().name();
try {
return encode(csn, ca, off, len);
} catch (UnsupportedEncodingException x) {
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 byte[] | safeTrim(byte[] ba, int len, java.nio.charset.Charset cs)
if (len == ba.length
&& (System.getSecurityManager() == null
|| cs.getClass().getClassLoader0() == null))
return ba;
else
return Arrays.copyOf(ba, len);
| private static char[] | safeTrim(char[] ca, int len, java.nio.charset.Charset cs)
if (len == ca.length
&& (System.getSecurityManager() == null
|| cs.getClass().getClassLoader0() == null))
return ca;
else
return Arrays.copyOf(ca, len);
| 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 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;
}
|
|