Java非托管内存流 [英] Java unmanaged memory stream

查看:72
本文介绍了Java非托管内存流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设计一个API,以允许我的Java应用程序连接到专有的进程间消息传递环境.消息预先存在于非托管内存中.收到消息后,我希望有一个流可以读取和写入消息.虽然我了解JNI的基础知识,但仍在努力了解哪些标准Java类可以帮助创建到非托管内存的流.

I am trying to design an API to allow my Java app to interface to a proprietary inter-process message passing environment. The messages pre-exist in unmanaged memory. Once I have received a message I want to have a stream to read from and write to the message. While I understand the basics of JNI I am struggling to understand which standard Java Classes can help to create a stream to the unmanaged memory.

对于任何指针,我将不胜感激

I would be grateful for any pointers

致谢

推荐答案

我将使用直接的ByteBuffer.您可以通过JNI更改地址和限制.一旦完成此操作,您就可以读取或更改此ByteBuffer中的任何内容,并且它将更改为非托管"大小.

I would use a direct ByteBuffer. You can change the address and limit via JNI. Once this is does you can read or change anything in this ByteBuffer and it will change on the "unmanaged" size.

ByteBuffer支持小的字节序和大字节序,并且可以读取和写入所有原始类型.

ByteBuffer support little and big endian and read and write of all the primitive types.

执行此操作的原始方法是使用Unsafe类.它支持在内存的随机区域(就像指针一样)访问原语.在许多情况下,它也简化为单个机器代码指令.不安全不是安全或可移植的,如果可以使用ByteBuffer,它是一个更好的选择.

A raw way of doing this is to use the Unsafe class. It supports accessing primitives at a random area of memory (just a like a pointer) It also reduces to a single machine code instruction in many cases. Unsafe isn't safe or portable, and if you can use ByteBuffer, its a better choice.

这篇关于Java非托管内存流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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