• Post Reply Bookmark Topic Watch Topic
  • New Topic

What's the best way to pass/return Input/Output streams from Java to JNI?  RSS feed

 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to pass in an inputstream to JNI and then manipulate that data, or use it in C++. As well, I want to grab a stream of data in C++ and be able to pass back an InputStream (from a JNI method) which a C++ thread will continue streaming data to.

What's the best way to do this?
 
Henry Wong
author
Sheriff
Posts: 23282
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Bizman:
I want to pass in an inputstream to JNI and then manipulate that data, or use it in C++. As well, I want to grab a stream of data in C++ and be able to pass back an InputStream (from a JNI method) which a C++ thread will continue streaming data to.

What's the best way to do this?


IMHO, there is no "best way" to do this. Unless you have the option of doing all the stream processing in Java and only pass the data to C++.

C++ can call methods of Java objects. It can even contruct and return Java objects to Java. It is just incredibly clumsy.

For example... here is how you would create an array list and call the add method to add a Java object.



Henry
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I know how to use Java objects from C++, but here's what I'm confused about.

I'm writing to a modem's outline (via C++'s waveOutWrite, etc). The problem is I don't know how to stream to that method from Java. Every call to wavOutWrite requires a header along with a pointer to the data (and the data length) to be written. The problem is I can't see anywhere that I can know it's read a byte and then write another byte before it reads the next one. I.e. be streaming into the LPSTR buffer that it uses. Can I do this? From java?
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason, I can't get it to work where I stream the bytes in (512 ata a time). The only way I can get it to work is write it into a ByteArrayOutputStream and then pass in stream.toByteArray() and read it in one shot. Any idea why?
 
Henry Wong
author
Sheriff
Posts: 23282
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dan Bizman:
For some reason, I can't get it to work where I stream the bytes in (512 ata a time). The only way I can get it to work is write it into a ByteArrayOutputStream and then pass in stream.toByteArray() and read it in one shot. Any idea why?


Oh, I see what you are trying to do... you want to write to a stream on the Java side. And on the C++ side, you want to read from the stream to write the data out to the modem. Is this correct?

If this is true, I guess you can create a pair of piped streams (PipedInputStream and PipedOutputStreams) pass one end to the C++ side to read, and have Java write to the other end. You'll need to have separate thread as the C++ side should not return until the operation has completed.

Henry
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:


Oh, I see what you are trying to do... you want to write to a stream on the Java side. And on the C++ side, you want to read from the stream to write the data out to the modem. Is this correct?

If this is true, I guess you can create a pair of piped streams (PipedInputStream and PipedOutputStreams) pass one end to the C++ side to read, and have Java write to the other end. You'll need to have separate thread as the C++ side should not return until the operation has completed.

Henry


That's not a bad idea but I wonder, performance-wise, how will it be? When you're talking about a Piped stream on the C++ side, I assume you mean doing the "env->GetMethodId(...) etc, stuff. Isn't that basically reflection? So I'd be taking a reflection hit on top of a JNI hit. Not saying that I have any alternative, but if there is one...

Thanks again. I'll be taking a look at that option and see if it works.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!