在Java中,为什么WindowsPreferences会使用斜杠作为大写字母?

EdH*_*EdH 13 java windows registry

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

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

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

/**
 * 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.
 */
Run Code Online (Sandbox Code Playgroud)

因此,对于大写字母,Simple编码将添加正斜杠.

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

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

MyP*_*ats 16

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

Registry-Keys是保留大小写的,但不区分大小写.例如,如果你有一个键"Rbi",你就不能再制作一个名为"RBi"的键.案件已保存但被忽略.Sun的区分大小写的解决方案是为密钥添加斜杠.

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

如果您不喜欢Registry-Values中的斜杠,则可能对此实现感兴趣.