title: SWIG - creating a typemap for function with four arguments that passes a bytearray
body: <p>This is a follow-up of the question <a href="https://stackoverflow.com/questions/11618267/how-to-use-a-bytebuffer-return-from-c-to-java">How to use a ByteBuffer return from C++ to Java</a> - I will acknowledge Flexo for his relentless support so far - I guess I just am not getting hang of typemaps yet... :-(  </p>
  
  <p>I have a C function with four arguments as follows, where  the third and fourth arguments are passed by reference</p>
  
  <pre><code>extern "C" int foo(const char* passByValText, const int passbyValLen, 
               unsigned char* retTextByRef, int *retTextLen) 
  </code></pre>
  
  <p>The Calling Java Wrapper function needs to look something like this</p>
  
  <pre><code>int foo(String passByValText, int passbyValLen, byte[] retBuf, SWIGTYPE_p_int retTextLen);
  </code></pre>
  
  <p>Alternatively it could be </p>
  
  <pre><code>int foo(String passByValText, int passbyValLen, ByteBuffer retBuf, SWIGTYPE_p_int retTextLen);
  </code></pre>
  
  <p>I can share an apology for a typemap that I wrote - but basically that might confuse matters even more ... so I am keeping this a clean slate ..</p>
  
  <p>below is example code on c side</p>
  
  <pre><code>extern "C" int foo (const char* passByValText, const int passbyValLen, char *retTextByRef, int *retTextLen){
      // binary value stuffed in the retTextByRef. In real program I compute this
      unsigned char someText[10]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x00};    
      memcpy(cipherText,someText,9);
      // I set the length of the value I stuffed into the buffer in c program
      *cipherLen=9;
  return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>And the Java program that calls this c function </p>
  
  <pre><code>class Caller{
      public static void main(String[] args) {
  
      //Define the parameter to pass by reference!!
      SWIGTYPE_p_int retTextLen=MYMODULE.new_intp();
  
      //Create a Java Wrapper Instance 
      JavaWrapClass myWrap=new JavaWrapClass();
  
      StringBuffer sBuf = new StringBuffer(50);
      int retTextLenVal=0;
      myWrap.foo("PASSED-STRING-BY-VALUE", 24, sBuf, retTextLen);
      retTextLenVal= MYMODULE.intp_value(retTextLen);
      System.out.println("JAVA :: got length "+ retTextLenVal);
      System.out.println("JAVA :: got BUFFER "+ sBuf);
      }
  }
  </code></pre>
  
  <p>Thanks a ton!!!</p>