Java – how do I subclass ByteBuffer?

Therefore, the Java NiO architect does not create a ByteBuffer interface, but a ByteBuffer class. It is not a final class, but it does not have a package common constructor, so it cannot be subclassed outside its package Alfred: P

I have a program that uses a memory mapped file byte buffer (obtained through filechannel. Map()) in a pile of places. I am trying to track a annoying error. The problem in the file is to keep it open because there is at least one ByteBuffer that is not released to garbage collection

I want to create an instrumentedbytebuffer class that looks like a byte buffer, but decorate a regular ByteBuffer (or its subclass, such as mappedbytebuffer) and track its existence (including the new buffer created by duplicate() and slice()) – in this way, I can keep my code complete. Using ByteBuffer, I only need to decorate the original byte buffer

Is there any way to bypass private constructors by reflection or proxy or in any way? I don't need to ship it to the final product. I just need to use it temporarily to solve this error

Solution

This makes no sense An unreceived ByteBuffer will not prevent the file from being closed You are the wrong tree in the tree However, mappedbytebuffer has a well-known problem, so it does not garbage collect and keeps files open effectively This is really a design problem: it has been known for many years, but there is no real solution The moral is not to use a lot of mappedbytebuffers

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>