title: Direct ByteBuffer
body: <p>I am testing out direct ByteBuffer(java.nio.ByteBuffer) with JNI. So the code below tries to:</p>
  
  <ol>
  <li>Put values into direct ByteBuffer in Java</li>
  <li>Change the value in C++</li>
  <li>Get the value in Java</li>
  </ol>
  
  <p>I was wondering where exactly did I do wrong? The C++ code manage to get the data from Java but changes in C++ did not reflect back in Java.</p>
  
  <p>This is what I did on java:</p>
  
  <pre><code>public static void main(String[] args){
      ByteBuffer bb = ByteBuffer.allocateDirect(3);
      byte[] b = {122,121,120};
      System.out.println("1: " + new String(b));
      bb.put(b);
  
      new JNI.process(bb);
  
      byte[] c = new byte[3];
      c[0] = bb.get();
      System.out.println("4: " + new String(c));
  }
  </code></pre>
  
  <p>This is what I did on JNI function:</p>
  
  <pre><code>JNIEXPORT void JNICALL Java_MarsJNI_mapreduce
    (JNIEnv *env, jobject thisObj, jobject output){
      char *out = (char*)env-&gt;GetDirectBufferAddress(output);
      printf("2: %s\n", out);
      out = "ABC";
      printf("3: %s\n", out);
  }
  </code></pre>
  
  <p>And the result I get is:</p>
  
  <pre><code>1: zyx
  2: zyx
  3: ABC
  Exception in thread "main" java.nio.BufferUnderflowException
      at java.nio.Buffer.nextGetIndex(Buffer.java:474)
      at java.nio.DirectByteBuffer.get(DirectByteBuffer.java:208)
      at MarsJNI.main(MarsJNI.java:21)
  </code></pre>