Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
814 views
in Technique[技术] by (71.8m points)

utf 8 - Why does Java's String.getBytes() uses "ISO-8859-1"

from java.lang.StringCoding :

String csn = (charsetName == null) ? "ISO-8859-1" : charsetName;

This is what is used from Java.lang.getBytes() , in linux jdk 7 I was always under the impression that UTF-8 is the default charset ?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It is a bit complicated ...

Java tries to use the default character encoding to return bytes using String.getBytes().

  • The default charset is provided by the system file.encoding property.
  • This is cached and there is no use in changing it via the System.setProperty(..) after the JVM starts.
  • If the file.encoding property does not map to a known charset, then the UTF-8 is specified.

.... Here is the tricky part (which is probably never going to come into play) ....

If the system cannot decode or encode strings using the default charset (UTF-8 or another one), then there will be a fallback to ISO-8859-1. If the fallback does not work ... the system will fail!

.... Really ... (gasp!) ... Could it crash if my specified charset cannot be used, and UTF-8 or ISO-8859-1 are also unusable?

Yes. The Java source comments state in the StringCoding.encode(...) method:

// If we can not find ISO-8859-1 (a required encoding) then things are seriously wrong with the installation.

... and then it calls System.exit(1)


So, why is there an intentional fallback to ISO-8859-1 in the getBytes() method?

It is possible, although not probable, that the users JVM may not support decoding and encoding in UTF-8 or the charset specified on JVM startup.

Then, is the default charset used properly in the String class during getBytes()?

No. However, the better question is ...


Does String.getBytes() deliver what it promises?

The contract as defined in the Javadoc is correct.

The behavior of this method when this string cannot be encoded in the default charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required.


The good news (and better way of doing things)

It is always advised to explicitly specify "ISO-8859-1" or "US-ASCII" or "UTF-8" or whatever character set you want when converting bytes into Strings of vice-versa -- unless -- you have previously obtained the default charset and made 100% sure it is the one you need.

Use this method instead:

public byte[] getBytes(String charsetName)

To find the default for your system, just use:

Charset.defaultCharset()

Hope that helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...