String构造方法String(byte bytes[], int offset, int length, String charsetName)中,
有这样一句:
char[] v = StringCoding.decode(charsetName, bytes, offset, length);
StringCoding的decode方法如下:
static char[] decode(String charsetName, byte[] ba, int off, int len)
throws UnsupportedEncodingException
{
StringDecoder sd = deref(decoder);
String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;
if ((sd == null) || !(csn.equals(sd.requestedCharsetName())
|| csn.equals(sd.charsetName()))) {
/*
问题在这里:sd不为null,则sd的requestedCharsetName和cs两个字段都与csn相同,那么为何还要用csn new一个StringDecoder?
如果要每次都想new一个,为何在下面还要用set(decoder, sd)将sd保存起来?
咋感觉把上面的条件csn.equals(sd.charsetName())改成!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);
}