为什么charset名称不是常量? [英] Why charset names are not constants?

查看:144
本文介绍了为什么charset名称不是常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Charset问题本身令人困惑和复杂,但最重要的是你必须记住你的字符集的确切名称。是utf8?或utf-8?或者UTF-8?在互联网上搜索代码示例时,您将看到以上所有内容。为什么不让它们命名为常量并使用 Charset.UTF8

Charset issues are confusing and complicated by themselves, but on top of that you have to remember exact names of your charsets. Is it "utf8"? Or "utf-8"? Or maybe "UTF-8"? When searching internet for code samples you will see all of the above. Why not just make them named constants and use Charset.UTF8?

推荐答案

问题的简单答案是,可用的字符串字符串因平台而异。

The simple answer to the question asked is that the available charset strings vary from platform to platform.

但是,有六个必须存在,所以常量可以已经为很久以前的人做过了。我不知道他们为什么不这样做。

However, there are six that are required to be present, so constants could have been made for those long ago. I don't know why they weren't.

JDK 1.4通过引入Charset类型做了一件好事。此时,他们不再需要提供String常量,因为目标是让每个人都使用Charset实例。那么为什么不提供六个标准的Charset常量呢?我问Martin Buchholz,因为他正好坐在我旁边,他说没有一个特别好的理由,除了当时的事情仍然是半生不熟的 - 很少有JDK API改装到接受字符集,和那名者中,字符集重载通常进行略差。

JDK 1.4 did a great thing by introducing the Charset type. At this point, they wouldn't have wanted to provide String constants anymore, since the goal is to get everyone using Charset instances. So why not provide the six standard Charset constants, then? I asked Martin Buchholz since he happens to be sitting right next to me, and he said there wasn't a really particularly great reason, except that at the time, things were still half-baked -- too few JDK APIs had been retrofitted to accept Charset, and of the ones that were, the Charset overloads usually performed slightly worse.

这是可悲的,这只是在JDK 1.6,他们终于用字符集重载完成舾装家居。并且这种倒退的表现情况仍然存在(之所以令人难以置信的奇怪,我无法解释它,但与安全有关!)。

It's sad that it's only in JDK 1.6 that they finally finished outfitting everything with Charset overloads. And that this backwards performance situation still exists (the reason why is incredibly weird and I can't explain it, but is related to security!).

长话短说 - - 只需定义自己的常量,或使用Tony Pony链接到的Guava的Charsets类(虽然该库尚未实际发布)。

Long story short -- just define your own constants, or use Guava's Charsets class which Tony the Pony linked to (though that library is not really actually released yet).

更新: StandardCharsets 类在JDK 7中。

Update: a StandardCharsets class is in JDK 7.

这篇关于为什么charset名称不是常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆