title: Using GetDirectBufferAddress from JNI
body: <p>I am trying to understand how to use <code>GetDirectBufferAddress</code> from the JNI layer. To understand I've build a very simple example:</p>
  <pre class="lang-java prettyprint-override"><code>public class my_image_info {
    static {
        System.loadLibrary(&quot;my_jni&quot;);
    }
    private java.nio.ByteBuffer image_info_bb;
    native static void initc( java.nio.ByteBuffer bb );
    my_image_info()
      {
      image_info_bb = java.nio.ByteBuffer.allocateDirect( 5 * 4 );
      initc( image_info_bb );
      }
    public java.nio.ByteBuffer getBB() {
      return image_info_bb;
    }
    static public void main(String argv[]) {
      my_image_info fii = new my_image_info();
      java.nio.ByteBuffer bb = fii.getBB();
      System.out.println(&quot;1: &quot; + bb.getInt(0));
      System.out.println(&quot;2: &quot; + bb.getInt(4));
      System.out.println(&quot;3: &quot; + bb.getInt(8));
      System.out.println(&quot;4: &quot; + bb.getInt(12));
      System.out.println(&quot;5: &quot; + bb.getInt(16));
  }
  </code></pre>
  <p>And then from the native JNI layer:</p>
  <pre class="lang-c prettyprint-override"><code>JNIEXPORT void JNICALL Java_my_1image_1info_initc
    (JNIEnv *env, jclass cls, jobject jobj)
  {
    int *iBuf = (*env)-&gt;GetDirectBufferAddress(env, jobj);
    iBuf[0] = -2;
    iBuf[1] = -1;
    iBuf[2] = 0;
    iBuf[3] = 1;
    iBuf[4] = 2;
  }
  </code></pre>
  <p>If I run this example over here (debian/linux wheezy amd64) with openjdk :</p>
  <pre class="lang-text prettyprint-override"><code>$ java -version
  java version &quot;1.6.0_34&quot;
  OpenJDK Runtime Environment (IcedTea6 1.13.6) (6b34-1.13.6-1~deb7u1)
  OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)
  </code></pre>
  <p>Here is what I see:</p>
  <pre class="lang-text prettyprint-override"><code>1: -16777217
  2: -1
  3: 0
  4: 16777216
  5: 33554432
  </code></pre>
  <p>I understand the values for index 2 &amp; 3. But all other values do not make any sense to me, I would have expected something like:</p>
  <pre class="lang-text prettyprint-override"><code>1: -2
  2: -1
  3: 0
  4: 1
  5: 2
  </code></pre>
  <p>What did I misunderstood from the <code>ByteBuffer</code> usage in JNI?</p>