开发者

What is the most efficient way to get java buffer in native code under JNI?

开发者 https://www.devze.com 2023-01-17 01:00 出处:网络
Problem: My code native code (c/c++) is called via JNI. Once it\'s started it needs to get byte[] buffer from Java object several times per second for further heavy processing. Native code should re

Problem:

My code native code (c/c++) is called via JNI.

Once it's started it needs to get byte[] buffer from Java object several times per second for further heavy processing. Native code should request buffer when processing of previous buffer is completed.

The orginal idea was to pass buffer via AF_INET socket to avoid JNI stuff, but that solution did not work efficiently enough.

Possible solutions:

There are following ways to improve p开发者_运维知识库assing buffer:

  1. JNI stuff
  2. AF_LOCAL
  3. ???

What is the most efficient way to get java byte [] buffer from native code?


You should be able to use JNI for this.

Take a look at the JNI GetPrimitiveArrayCritical() function. Whether or not that will do a copy is JVM implementation dependent according to the docs, but in practice that has worked well for me.

Alternatively, you can use ByteBuffer, which was designed for this. Note that you will want to create your ByteBuffer using its allocateDirect() method and then get the pointer to the buffer in JNI using GetDirectBufferAddress().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号