Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Passing an array of bytes to Java without copying?

843829Jul 13 2001 — edited Jun 13 2003
I have implemented a video capture device for the Java Media Framework that connects Java to Microsoft's DirectShow. I use the JNI to pass the video stream buffer to the JMF every time a new frame is available.

The C++ JNI function that passes the buffer on to Java looks something like this (note: I put "..." in places of tabs/spaces since this site seems to remove leading spaces from lines):

IMediaSample *gVideoFrame;

JNIEXPORT void JNICALL Java_com_teraglobal_GetDSFrame
(JNIEnv *inEnv, jobject inObj, jbyteArray theData)
{
....// Get the pointer to the image buffer
....jbyte *pSource = NULL;
....HRESULT hr = gVideoFrame->GetPointer((BYTE **)(&pSource));
....if (SUCCEEDED(hr))
....{
........// Copy the bytes from our array into Java's array (EEECH!!!)
........inEnv->SetByteArrayRegion(theData, 0, inEnv->GetArrayLength(theData), pSource);
....}
}

The problem: For each video frame, I need to do a memory copy to move the image from the native C buffer into a Java byte array. For a 320x240 frame that amounts to 150k+ memory move each time--this slows things down! I would like to pass this buffer pointer directly on to Java without the memory copy, but the JMF requires that the frame be in a "byte[]" Java Object.

My question: is there any way I can tell Java to use my pointer from the native C++ code when it allocates the "byte[]" array object?

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 11 2003
Added on Jul 13 2001
10 comments
2,432 views