Swig: pass a byte array in Java to C

I am trying to create a Java implementation to transfer bytes [] to C using Swig.

Swig:

%include "typemaps.i"
%apply(char *STRING, int LENGTH) { (char *buff, int len) }; 
%inline {
   typedef struct {
        char*         buff;        
        int           len;  
  } workit_t;
}

In my generated java class (workit_t.java), the buff parameter is a string, not a byte [].

Java:

public void setBuff(String value){
 ... 
}

What am I doing wrong in defining swig?

When I write a simple swig definition without structure, I get the type of parameter I want.

Swig:

%include "typemaps.i"
%apply(char *STRING, int LENGTH) { (char *buff1, int *len1) };

Java:

public static void Mathit(byte[] buff1, byte[] buff2) {
...
}
+2
source share
1 answer

Well, I was able to get it right.

Before:

%include "typemaps.i"
%apply(char *STRING, int LENGTH) { (char *buff, int len) }; 
%inline {
   typedef struct {
        char*         buff;        
        int           len;  
  } workit_t;
}

Now:

%include various.i                    
%apply char *BYTE { char *buff };  //map a Java byte[] array to a C char array
%inline {
   typedef struct {
        char*         buff;        
        int           len;  
   } workit_t;
}

Or:

%include various.i                    
%apply char *NIOBUFFER { char *buff }; //map Java nio buffers to C char array
%inline {
   typedef struct {
        char*         buff;        
        int           len;  
   } workit_t;
}
0
source

All Articles