在Java中,为什么WindowsPreferences使用斜杠作为大写字母? [英] In Java, why does WindowsPreferences use slashes for capital letters?

查看:119
本文介绍了在Java中,为什么WindowsPreferences使用斜杠作为大写字母?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用java.util.prefs.Preferences功能(在Java 8中,在Windows机器上)。它可以工作,我可以在其中写入Windows注册表的新密钥。因此,我使用Preferences.systemRoot()来获取系统的Preferences对象,然后使用node()方法获取映射到Windows注册表中的节点的Preferences对象。而且它创造了很好的东西。

I've been working with the java.util.prefs.Preferences functionality (in Java 8, on a Windows machine). And it works, where I can write new keys to the Windows Registry. So, I use Preferences.systemRoot() to get the Preferences object for the system, and then use the node() method to get a Preferences object that maps to a node in the Windows Registry. And it's creating things fine.

我用于节点的密钥是所有大写字母的字符串(RBI)。当我查看Windows注册表中的节点时,它显示为/ R / B / I,名称中带有正斜杠。

The Key I'm using for the Node is a String in all capital letters ("RBI"). When I look at the node in the Windows Registry, it comes up as "/R/B/I", with forward slashes in the name.

我认为这是奇怪,所以我挖了一下而且看起来这是故意的。我找到了在Windows环境中提供Preferences实现的类(java.util.prefs.WindowsPreferences),该方法用于构建发送到Windows注册表的值是一个静态方法toWindowsName。在JavaDoc中....

I thought this was odd, so I've dug around a bit And it looks like this is intentional. I found the class that provides the implementation of Preferences on a Windows environment (java.util.prefs.WindowsPreferences) and the method is uses for building the values sent to the windows registry is a static method toWindowsName. In the JavaDoc for that....


/**
 * Converts value's or node's name to its Windows representation
 * as a byte-encoded string.
 * Two encodings, simple and altBase64 are used.
 * <p>
 * <i>Simple</i> encoding is used, if java string does not contain
 * any characters less, than 0x0020, or greater, than 0x007f.
 * Simple encoding adds "/" character to capital letters, i.e.
 * "A" is encoded as "/A". Character '\' is encoded as '//',
 * '/' is encoded as '\'.
 * The constructed string is converted to byte array by truncating the
 * highest byte and adding the terminating <tt>null</tt> character.
 * <p>
 * <i>altBase64</i>  encoding is used, if java string does contain at least
 * one character less, than 0x0020, or greater, than 0x007f.
 * This encoding is marked by setting first two bytes of the
 * Windows string to '/!'. The java name is then encoded using
 * byteArrayToAltBase64() method from
 * Base64 class.
 */


因此,简单编码将,对于大写字母,请添加正斜杠。

So, the Simple encoding will, for capital letters, add a forward slash.

有谁知道为什么需要这样做?我以为注册表可以处理区分大小写的值,但这似乎表明它不能?

Does anyone know why this is required? I had thought that the Registry could handle case-sensitive values, but this seems to indicate that it can't?

我可以解决这个问题,我只是好奇为什么要这样做。

I can work around this, I'm just curious why this was done.

推荐答案

我很好奇你和我发现了以下解释:

I was curious as you and I found the following explanation:

Registry-Keys是保留大小写的,但不区分大小写。例如,如果您有一个键Rbi,则无法创建另一个名为RBi的键。案例已保存但被忽略。 Sun的区分大小写的解决方案是在密钥中添加斜杠。

Registry-Keys are case-preserving, but case-insensitive. For example if you have a key "Rbi" you cant make another key named "RBi". The case is saved but ignored. Sun's solution for case-sensitivity was to add slashes to the key.

注册表 - 值区分大小写(当然,还有大小写保留)。我不认为Sun也打算在数值中添加斜杠,但不知怎的,它已经进入了代码。在我看来,这个错误很长一段时间都没有找到。当发现错误时,许多系统已经依赖于错误的实现,因此他们从未删除它以保持兼容性。

Registry-Values are case-sensitive (and, of course, case-preserving). I don't think it was Sun's intention to add the slashes to the values as well, but somehow it slipped into the code. It seems to me, that this bug wasn't found for a long time. When the bug was discovered, many systems already depended on the wrong implementation, so they never removed it to retain compatibility.

如果您不喜欢注册表中的斜杠-Values,您可能对感兴趣这个实现

If you don't like the slashes in your Registry-Values, you may be interested in this implementation.

这篇关于在Java中,为什么WindowsPreferences使用斜杠作为大写字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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