Search code examples
javaimagejava-native-interface

Effeciently transfer large amount of byte data from C++ to Java


I have a Java program which is designed to display images in a custom file format, read through a C++ library using JNI. The data is loaded into an char array on the C++ side, and transferred to a BufferedImage on the Java side. Since sections of the image could be removed from memory and need to be reloaded fairly regularly, so I want these operations to be as fast as possible.

The way I'm currently doing this is data gets read from the file into a buffer in the C++ library's memory. In order to populate the BufferedImage, the Java code makes a JNI function call for each pixel to read from this buffer and, if necessary, load another chunk of data into the buffer. This works, but with a higher overhead than I'd like.

What I've been planning to do to improve this is pass the BufferedImage object to the C++ code using a JNI call, and make function calls on it from that side. I've been researching JNI as much as I can, but I haven't been able to find out if there's any cost in modifying Java objects from a C++ library which was loaded by the JVM. Is this a good way to implement this, or is there a faster way to transfer large amounts of byte data with JNI?


Solution

  • The most efficient way to move data across the JNI boundary is probably via direct ByteBuffers. You should move as much as you can at a time, as it is crossing the boundary itself that is inefficient, not the transfer. Certainly more than one pixel, preferably megabytes at a time.