Java Magic. Part 2: 0xCAFEBABE

Java Magic. Part 2: 0xCAFEBABE

@(Base)[JDK, magic, 黑魔法]

转载请写明:原文地址

英文原文

系列文章:

-Java Magic. Part 1: java.net.URL
-Java Magic. Part 2: 0xCAFEBABE
-Java Magic. Part 3: Finally
-Java Magic. Part 4: sun.misc.Unsafe

你知道所有的java class文件都有一个相同的4字节串吗。这个4字节串的16进制是CAFEBABE

我们可以简单编写一个Hello.java来验证一下:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hell, O'World!");
    }
}

我们用javac编译该文件。然后用Emacs打开这个class文件(使用M-x hexl-mode)来查看,我们得到如下信息:

00000000: cafe babe 0000 0033 001d 0a00 0600 0f09  .......3........
00000010: 0010 0011 0800 120a 0013 0014 0700 1507  ................
00000020: 0016 0100 063c 696e 6974 3e01 0003 2829  .....<init>...()
00000030: 5601 0004 436f 6465 0100 0f4c 696e 654e  V...Code...LineN
00000040: 756d 6265 7254 6162 6c65 0100 046d 6169  umberTable...mai
00000050: 6e01 0016 285b 4c6a 6176 612f 6c61 6e67  n...([Ljava/lang
// others..

根据白皮书的规范,前四个字节(u4)cafebabe就是Class文件的魔数,第5、6字节(u2)是Class文件的次版本号,第7、8字节(u2)是主版本号。十六进制0和33,也就是版本号为51.0。

有趣的就是这个4字节魔数,根据James Gosling的博客,他是这么解释的:

We used to go to lunch at a place called St Michael's Alley. According to local legend, in the deep dark past, the Grateful Dead used to perform there before they made it big. It was a pretty funky place that was definitely a Grateful Dead Kinda Place. When Jerry died, they even put up a little Buddhist-esque shrine. When we used to go there, we referred to the place as Cafe Dead. Somewhere along the line it was noticed that this was a HEX number. I was re-vamping some file format code and needed a couple of magic numbers: one for the persistent object file, and one for classes. I used CAFEDEAD for the object file format, and in grepping for 4 character hex words that fit after "CAFE" (it seemed to be a good theme) I hit on BABE and decided to use it. At that time, it didn't seem terribly important or destined to go anywhere but the trash-can of history. So CAFEBABE became the class file format, and CAFEDEAD was the persistent object format. But the persistent object facility went away, and along with it went the use of CAFEDEAD - it was eventually replaced by RMI.

以前我们曾去过一个叫StMichael's Alley的地方吃中午饭。根据当地一个传说,在很久很久以前,一个叫做Grateful Dead的乐队在成名之前一直都在这里表演。当乐队成员Jerry死了之后,这里还一度变成纪念他的地方。我注意到这个地方写着一串16进制的魔术(这尼玛明明是个咖啡馆的名字好吗)。当时我正在改造一些文件的格式,我当时需要2个魔术:一个是用来表示object file,另外一个用来表示class文件。我选择使用CAFEDEAD来表示object file,然后把后面2个字节换掉之后用来表示class文件。我突然想到BABE(小孩的意思)这个放在CAFE的后面应该不错。当时看来这个东西好像并不是特别重要啦~所以,结果就是你们现在看到的CAFEDEAD用来表示持久化的object format(也就是object序列化之后的结果),然后CAFEBABE就用来表示class文件。当时persistence object的最终被RMI(java远程调用)替换掉了,CAFEDEAD也就不复存在了。

0xCAFEBABE的十进制是3405691582。如果我们把所有位加起来得到43。恰好大于42-Ultimate Answer to the Life, the Universe, and Everything。另外43也是一个质数。You see, magic everywhere. Even in the last sentence.

这个作者脑子有点毛病..

posted @ 2016-02-06 11:00  马宇申  阅读(1617)  评论(0编辑  收藏  举报