Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Based on the information you've provided it appears you are using a ByteBuffer implementation that doesn't allow the Native code to get access to the underlying memory structure. It is attempting to access the direct memory in your ByteBuffer, which it probably shouldn't be doing, and is failing because the class deriving from ByteBuffer doesn't store data directly. </p> <p>If this is critical code you can't change, your best bet would be to create a ByteBuffer using the Java implementation, then copy the original data into your temporary buffer; Pass the new buffer to your native method. I would then profile the code to see if it is a performance impact. </p> <p>Here is an example of how to do this. I am a little hesitant to use <code>rewind()</code> and <code>limit()</code> as I don't know what the implementation of your ByteBuffer will return so check to make sure it implements the interface of ByteBuffer correctly. </p> <p>This code illegally access index 3 on purpose to show that extra data isn't added.</p> <p><code>public static void main(String[] args) {</code></p> <pre><code> // This will be your implementation of ByteBuffer that // doesn't allow direct access. ByteBuffer originalBuffer = ByteBuffer.wrap(new byte[]{12, 50, 70}); originalBuffer.rewind(); byte[] newArray = new byte[originalBuffer.limit()]; originalBuffer.get(newArray, 0, newArray.length); ByteBuffer newBuffer = ByteBuffer.wrap(newArray); System.out.println("Limit: " + newBuffer.limit()); System.out.println("Index 0: " + newBuffer.get(0)); System.out.println("Index 1: " + newBuffer.get(1)); System.out.println("Index 2: " + newBuffer.get(2)); System.out.println("Index 3: " + newBuffer.get(3)); } </code></pre> <p><em>Output:</em></p> <p>Limit: 3</p> <p>Index 0: 12</p> <p>Index 1: 50</p> <p>Index 2: 70</p> <p>Exception in thread "main" java.lang.IndexOutOfBoundsException</p> <pre><code> at java.nio.Buffer.checkIndex(Buffer.java:514) at java.nio.HeapByteBuffer.get(HeapByteBuffer.java:121) at stackoverflow_4534583.Main.main(Main.java:35) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload