title: Effeciently transfer large amount of byte data from C++ to Java
body: <p>I have a Java program which is designed to display images in a custom file format, read through a C++ library using <a href="http://en.wikipedia.org/wiki/Java_Native_Interface" rel="nofollow">JNI</a>. 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.</p>
  
  <p>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.</p>
  
  <p>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?</p>