Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting between C++ istream/ostream and Java InputStream/OutputStream in JNI

Are there any existing C++/JNI libraries that do the work of marshalling access between a Java InputStream and a C++ istream, as well as a Java OutputStream and a C++ ostream? In particular, I want a Java application to be able to pass an InputStream or an OutputStream into a C++ library that uses iostreams as its streaming interface.

If no existing library exists, what is the minimum functionality I need to implement in an istream/ostream implementation in order to support this? To make things worse, I will also need to support seek and tell, although in principle I should be able to use mark, reset, and skip to do what I need.

As these streams can be quite large I do not want to simply load them into a ByteArray which then gets turned into a stringstream or the like.

like image 456
fluffy Avatar asked Nov 11 '22 10:11

fluffy


1 Answers

I am assuming that your sources/sinks are the Java objects and that your C++ library needs to read from the Java InputStream and write to Java OutputStream instances and that you need a facade of C++ IO streams that you can pass down to your C++ parts that will call back for data into your Java objects.

I suggest implementing C++ IO streams facade for the Java streams using the Boost.Iostreams library. Browse the docs and examples. What you want is the stream_buffer and stream classes together with your own implementation of Sink and Source concepts (see also Device concept and device class).

UPDATE

To implement seeking you need to implement the SeekableDevice concept and provide appropriate tag in your device's category.

like image 157
wilx Avatar answered Nov 14 '22 21:11

wilx