Login  Register

Re: Buffers.newDirectIntBuffer(int[]) does not set limit

Posted by jmaasing on Jul 25, 2015; 7:31am
URL: https://forum.jogamp.org/Buffers-newDirectIntBuffer-int-does-not-set-limit-tp4034959p4034967.html

Sven Gothel wrote
Question remains, how did it (0 limit) happen?

Please answer in related bug report and close it if proven
to be your issue.

I guess we are a bit 'crazy' about this issue,
since iff this would be an internal bug within GlueGen and/or JOGL
it is quite substantial (and would break everything).
Hence .. we are a bit concerned and tedious now :)

~Sven
I had some time to investigate this, the problem is in my expectations on what the Buffers class does. You are absolutely right in that limit is correctly set by the Buffers class. The difference is actually in the position.

This test illustrates what my code did:

public class BuffersTest {
	@Test
	public void testFlipReadOnExplicitlyPutBuffer() {
		IntBuffer explicit = Buffers.newDirectIntBuffer(3);
		explicit.put(1) ;
		explicit.put(2) ;
		explicit.put(3) ;
		explicit.flip() ;
		
		assertTrue(explicit.get() == 1) ;
		assertTrue(explicit.get() == 2) ;
		assertTrue(explicit.get() == 3) ;
	}
	
	@Test
	public void testFlipReadOnArrayCreatedBuffer() {
		IntBuffer implicit = Buffers.newDirectIntBuffer(new int[] {1,2,3});
		implicit.flip() ;
		
		assertTrue(implicit.get() == 1) ;
		assertTrue(implicit.get() == 2) ;
		assertTrue(implicit.get() == 3) ;
	}
}

Notice that I 'flip()' the buffer before I read from them and this is the key problem.
In the 'implicit' case the position on the returned buffer is set to '0'. I assumed it would be set to 3 so it would be the same as in the explicit but I assumed wrong.

I do not consider this a bug, more a mistaken assumption on my part. I will close the bugzilla issue. Sorry for all the noise.