我们学习java基础的时候.我们都知道在main方法中String的getBytes()方法如果不指定编码格式,默认是UTF-8的方法进行的编码.我们一直认为默认的编码格式就是UTF-8.直到我们学习了javaWeb.在Servlet中.我们通过getBytes()获取的是按照GBK进行编码的.至此我们就有了疑惑.这个getBytes()方法到底是怎么选择默认编码的.我们带着疑惑,去翻一下String的源代码.
[AppleScript] 纯文本查看 复制代码
1
2
3
|
public byte[] getBytes ( ) {
return StringCoding.encode ( value , 0 , value .length ) ;
}
|
继续跟进StringCoding的encode方法
[AppleScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
|
static byte[] encode ( char[] ca , int off , int len ) {
String csn = Charset.defaultCharset ( ) . name ( ) ;
try {
/ / use charset name encode ( ) variant which provides caching.
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;
}
}
|
从以上源码中可以看出是通过
[AppleScript] 纯文本查看 复制代码
1
|
String csn = Charset.defaultCharset ( ) . name ( ) ;
|
来获取默认编码方式的.我们继续跟进.查看Charset的defaultCharSet()方法.
[AppleScript] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
public static Charset defaultCharset ( ) {
if ( defaultCharset = = null ) {
synchronized ( Charset. class ) {
String csn = AccessController.doPrivileged (
new GetPropertyAction ( "file.encoding" ) ) ;
Charset cs = lookup ( csn ) ;
if ( cs ! = null )
defaultCharset = cs;
else
defaultCharset </
|