| String类源码: private final char value[];//String对应的字符数组容器
 public String(byte bytes[]) {
 this(bytes, 0, bytes.length);//继续调用本类构造器
 }
 public String(byte bytes[], int offset, int length) {
 checkBounds(bytes, offset, length);//检查是否越界
 this.value = StringCoding.decode(bytes, offset, length);//调用StringCoding类方法
 }
 StringCoding类源码:
 static char[] decode(byte[] ba, int off, int len) {
 String csn = Charset.defaultCharset().name();//字符集
 try {
 // use charset name decode() variant which provides caching.
 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(Charset cs, byte[] ba, int off, int len) {    //编码阶段
 CharsetDecoder cd = cs.newDecoder();
 int en = scale(len, cd.maxCharsPerByte());
 char[] ca = new char[en];
 if (len == 0)
 return ca;
 boolean isTrusted = false;
 if (System.getSecurityManager() != null) {
 if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
 ba =  Arrays.copyOfRange(ba, off, off + len);
 off = 0;
 }
 }
 cd.onMalformedInput(CodingErrorAction.REPLACE)
 .onUnmappableCharacter(CodingErrorAction.REPLACE)
 .reset();
 if (cd instanceof ArrayDecoder) {
 int clen = ((ArrayDecoder)cd).decode(ba, off, len, ca);
 return safeTrim(ca, clen, cs, isTrusted);
 } else {
 ByteBuffer bb = ByteBuffer.wrap(ba, off, len);
 CharBuffer cb = CharBuffer.wrap(ca);
 try {
 CoderResult cr = cd.decode(bb, cb, true);
 if (!cr.isUnderflow())
 cr.throwException();
 cr = cd.flush(cb);
 if (!cr.isUnderflow())
 cr.throwException();
 } catch (CharacterCodingException x) {
 // Substitution is always enabled,
 // so this shouldn't happen
 throw new Error(x);
 }
 return safeTrim(ca, cb.position(), cs, isTrusted);
 }
 }
 private static char[] safeTrim(char[] ca, int len,            //通过编码确定安全的截取长度
 Charset cs, boolean isTrusted) {
 if (len == ca.length && (isTrusted || System.getSecurityManager() == null))
 return ca;
 else
 return Arrays.copyOf(ca, len);
 }
 |