Search in sources :

Example 21 with InvalidMarkException

use of java.nio.InvalidMarkException in project robovm by robovm.

the class FloatBufferTest method testCompact.

public void testCompact() {
    // case: buffer is full
    buf.clear();
    buf.mark();
    loadTestData1(buf);
    FloatBuffer ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), buf.capacity());
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 0.0f, buf.capacity());
    try {
        buf.reset();
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
    // case: buffer is empty
    buf.position(0);
    buf.limit(0);
    buf.mark();
    ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), 0);
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 0.0f, buf.capacity());
    try {
        buf.reset();
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
    // case: normal
    assertTrue(buf.capacity() > 5);
    buf.position(1);
    buf.limit(5);
    buf.mark();
    ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), 4);
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 1.0f, 4);
    try {
        buf.reset();
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) InvalidMarkException(java.nio.InvalidMarkException)

Example 22 with InvalidMarkException

use of java.nio.InvalidMarkException in project robovm by robovm.

the class FloatBufferTest method testSlice.

public void testSlice() {
    assertTrue(buf.capacity() > 5);
    buf.position(1);
    buf.limit(buf.capacity() - 1);
    FloatBuffer slice = buf.slice();
    assertEquals(buf.isReadOnly(), slice.isReadOnly());
    assertEquals(buf.isDirect(), slice.isDirect());
    assertEquals(buf.order(), slice.order());
    assertEquals(slice.position(), 0);
    assertEquals(slice.limit(), buf.remaining());
    assertEquals(slice.capacity(), buf.remaining());
    try {
        slice.reset();
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
    // slice share the same content with buf
    if (!slice.isReadOnly()) {
        loadTestData1(slice);
        assertContentLikeTestData1(buf, 1, 0, slice.capacity());
        buf.put(2, 500);
        assertEquals(slice.get(1), 500, 0.0);
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) InvalidMarkException(java.nio.InvalidMarkException)

Example 23 with InvalidMarkException

use of java.nio.InvalidMarkException in project robovm by robovm.

the class IntBufferTest method testCompact.

public void testCompact() {
    // case: buffer is full
    buf.clear();
    buf.mark();
    loadTestData1(buf);
    IntBuffer ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), buf.capacity());
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 0, buf.capacity());
    try {
        buf.reset();
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
    // case: buffer is empty
    buf.position(0);
    buf.limit(0);
    buf.mark();
    ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), 0);
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 0, buf.capacity());
    try {
        buf.reset();
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
    // case: normal
    assertTrue(buf.capacity() > 5);
    buf.position(1);
    buf.limit(5);
    buf.mark();
    ret = buf.compact();
    assertSame(ret, buf);
    assertEquals(buf.position(), 4);
    assertEquals(buf.limit(), buf.capacity());
    assertContentLikeTestData1(buf, 0, 1, 4);
    try {
        buf.reset();
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
}
Also used : IntBuffer(java.nio.IntBuffer) InvalidMarkException(java.nio.InvalidMarkException)

Aggregations

InvalidMarkException (java.nio.InvalidMarkException)23 ByteBuffer (java.nio.ByteBuffer)10 Buffer (java.nio.Buffer)6 CharBuffer (java.nio.CharBuffer)2 DoubleBuffer (java.nio.DoubleBuffer)2 FloatBuffer (java.nio.FloatBuffer)2 IntBuffer (java.nio.IntBuffer)2 LongBuffer (java.nio.LongBuffer)2 ShortBuffer (java.nio.ShortBuffer)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 ReadOnlyBufferException (java.nio.ReadOnlyBufferException)1