扩展ByteBuffer类 [英] Extending ByteBuffer class

查看:145
本文介绍了扩展ByteBuffer类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法创建扩展ByteBuffer类的类?

Is there any way to create class that extends ByteBuffer class?

ByteBuffer的一些抽象方法是包私有的,如果我创建包java.nio,安全异常抛出。

Some abstract methods from ByteBuffer are package private, and if I create package java.nio, security exception is thrown.

出于性能原因,我想这样做 - 例如,getInt有大约10个方法调用,以及相当多的if。即使剩下所有检查,只有方法调用被内联并且大/小端检查被删除,我创建的测试表明它可以快4倍。

I would want to do that for performance reasons - getInt for example has about 10 method invocations, as well as quite a few if's. Even if all checks are left, and only method calls are inlined and big/small endian checks are removed, tests that I've created show that it can be about 4 times faster.

推荐答案

你无法扩展ByteBuffer并感谢上帝。

You cant extend ByteBuffer and thanks God for.

你不能延伸b / c没有受保护的c-tors 。为什么感谢上帝?好吧,只有2个真正的子类确保JVM可以重大优化任何涉及ByteBuffer的代码。

You cant extend b/c there are no protected c-tors. Why thank god part? Well, having only 2 real subclasses ensures that the JVM can Heavily optimizes any code involving ByteBuffer.

最后,如果你需要扩展类为了真实,编辑字节代码,只需将c-tor和public属性的protected属性添加到DirectByteBuffer(和DirectByteBufferR)。 扩展HeapBuffer没有任何意义,因为你无论如何都可以访问底层数组

Last, if you need to extend the class for real, edit the byte code, and just add protected attribute the c-tor and public attribute to DirectByteBuffer (and DirectByteBufferR). Extending the HeapBuffer serves no purposes whatsoever since you can access the underlying array anyways

使用 -Xbootclasspath / p 并在那里添加你自己的类,在你需要的包中扩展(在java.nio之外)。这就是它的完成方式。

use -Xbootclasspath/p and add your own classes there, extend in the package you need (outside java.nio). That's how it's done.

另一种方法是使用sun.misc.Unsafe并在 address()

Another way is using sun.misc.Unsafe and do whatever you need w/ direct access to the memory after address().


我想以
的性能原因这样做 -
的getInt示例大约10方法
调用,以及相当多的
if。即使剩下所有检查,并且
仅内联方法调用并且删除了
大/小端检查,我创建的
测试显示它是
可以是关于快4倍。

I would want to do that for performance reasons - getInt for example has about 10 method invocations, as well as quite a few if's. Even if all checks are left, and only method calls are inlined and big/small endian checks are removed, tests that I've created show that it can be about 4 times faster.

现在好的部分,使用gdb并检查真正生成的机器代码,你会惊讶地发现了多少次检查将被删除。

Now the good part, use gdb and check the truly generated machine code, you'd be surprised how many checks would be removed.

我无法想象为什么一个人想要扩展课程。它们的存在是为了获得良好的性能而不仅仅是OO多态执行。

I can't imagine why a person would want to extend the classes. They exist to allow good performance not just OO polymorph execution.

编辑:

On Unsafe:Unsafe有2个方法绕过验证程序,如果你有一个扩展ByteBuffer的类,可以直接打电话给他们。对于编译器,你需要一些带有公共访问和受保护的c-tor的ByteBuffer的黑客版本(但这非常简单)。
方法如下。你可以自己承担风险。在宣布这样的类之后你甚至可以使用w / new关键字(前提是有一个合适的c-tor)

On Unsafe: Unsafe has 2 methods that bypass the verifier and if you have a class that extends ByteBuffer you can just call any of them. You need some hacked version (but that's super easy) of ByteBuffer w/ public access and protected c-tor just for the compiler. The methods are below. You can use 'em on your own risk. After you declare the class like that you can even use it w/ new keyword (provided there is a suitable c-tor)

public native Class defineClass(String name, byte[] b, int off, int len, ClassLoader loader, ProtectionDomain protectionDomain);    
public native Class defineClass(String name, byte[] b, int off, int len);

这篇关于扩展ByteBuffer类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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