title: Append to byte array using swig
body: <p>I would like to append to a Java byte[] from C++ using swig. The trick is to do this without calling GetByteArrayElements as this would make a copy of the byte[] if the byte[] cannot be pinned.</p>
  
  <p>Without swig, we can easily do this with SetByteArrayRegion - write number of bytes to the byte[] directly.
  The problem with swig is that:</p>
  
  <ul>
  <li><p>the code that does the actual work is in .cpp - for example read the contents of a file and put it in a string fileContents. The c++ code doesn’t have access to the jni fields. </p></li>
  <li><p>The code that does SetByteArrayRegion is in the .i file - it does not have access to fileContents data produced by the c++ function.</p></li>
  </ul>
  
  <p>I could not find any example to append data generated by c++ function into a byte[] using swig, without pinning the whole buffer and just using SetByteArrayRegion to copy the necessary data over.</p>
  
  <p>Sample c++ functionality to achieve:</p>
  
  <pre><code>
      void append(char *s, int pos) {
        char* fileContents = readFromFile();
        strcat(s+pos, fileContents);
      }
  </code></pre>
  
  <p>Possible work-around solution:</p>
  
  <p>In the .i file we do the actual implementation (call readFromFile) and with those results call SetByteArrayRegion directly in the %template . This is rather hacky.</p>