From: William S F. <ws...@fu...> - 2008-07-03 21:47:30
|
J. Tang wrote: > I have read the SWIG manual, but am stumped on how to do the following: > > I have the C function: > void recv_data(char **data, int *len); > > that reads stuff and puts data into the location pointed to by '*data' > and the read size to '*len'. I want to write a Java wrapper that > returns an instance of byte[] instead of java.lang.String. > > The best I could come up with is: > > jByteArray fake_recv_data() { > char *data = NULL; > int len = 0; > jobject jresult = NULL; > recv_data(self->sipc, &data, &len); > jresult = (*jenv)->NewByteArray(jenv, len); > (*jenv)->SetByteArrayRegion(jenv, jresult, 0, len, data); > return jresult; > }; > > I believe that I need a native function that will change the output from > a parameter to a function return value. However, I do not know how to > access jenv from within the native function. What is the recommended > SWIG way of doing what I need? > Java SWIG doesn't support turning a parameter into a return type. Instead you have to think of some way to pass the parameter as a Java reference, so that the JNI code can modify it and those modifications can be seen from within Java. > I am sure that there is some magic via %typemaps possible, but I am > thoroughly stumped. '%typemap(argout)' does not seem to change the > return type from void to a jByteArray; '%typemap(out) void' does not > either. > argout can be used to do this in the scripting languages only. > Thanks for any assistance for this seemingly trivial task. > > [*] I am aware that Java does not really have out parameters. > See the Examples/java/typemap example for ideas on how to make an output parameter possible in Java. It uses the various.i library for changing the behaviour of char *. You should be able to use the same ideas for char **. William |