Search in sources :

Example 41 with InvalidMarkException

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

the class LongBufferTest method testSlice.

public void testSlice() {
    assertTrue(buf.capacity() > 5);
    buf.position(1);
    buf.limit(buf.capacity() - 1);
    LongBuffer 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);
    }
}
Also used : LongBuffer(java.nio.LongBuffer) InvalidMarkException(java.nio.InvalidMarkException)

Example 42 with InvalidMarkException

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

the class ShortBufferTest method testCompact.

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

Example 43 with InvalidMarkException

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

the class ShortBufferTest method testSlice.

public void testSlice() {
    assertTrue(buf.capacity() > 5);
    buf.position(1);
    buf.limit(buf.capacity() - 1);
    ShortBuffer 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, (short) 0, slice.capacity());
        buf.put(2, (short) 500);
        assertEquals(slice.get(1), 500);
    }
}
Also used : ShortBuffer(java.nio.ShortBuffer) InvalidMarkException(java.nio.InvalidMarkException)

Example 44 with InvalidMarkException

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

the class AbstractBufferTest method testRewind.

public void testRewind() {
    // save state
    int oldPosition = baseBuf.position();
    int oldLimit = baseBuf.limit();
    Buffer ret = baseBuf.rewind();
    assertEquals(baseBuf.position(), 0);
    assertSame(ret, baseBuf);
    try {
        baseBuf.reset();
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (InvalidMarkException e) {
    // expected
    }
    // restore state
    baseBuf.limit(oldLimit);
    baseBuf.position(oldPosition);
}
Also used : Buffer(java.nio.Buffer) ByteBuffer(java.nio.ByteBuffer) InvalidMarkException(java.nio.InvalidMarkException)

Example 45 with InvalidMarkException

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

the class ByteBufferTest method testSlice.

public void testSlice() {
    assertTrue(buf.capacity() > SMALL_TEST_LENGTH);
    buf.position(1);
    buf.limit(buf.capacity() - 1);
    ByteBuffer 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, (byte) 0, slice.capacity());
        buf.put(2, (byte) 100);
        assertEquals(slice.get(1), 100);
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) InvalidMarkException(java.nio.InvalidMarkException)

Aggregations

InvalidMarkException (java.nio.InvalidMarkException)71 ByteBuffer (java.nio.ByteBuffer)24 Test (org.junit.Test)22 Buffer (java.nio.Buffer)12 CharBuffer (java.nio.CharBuffer)7 DoubleBuffer (java.nio.DoubleBuffer)7 FloatBuffer (java.nio.FloatBuffer)7 IntBuffer (java.nio.IntBuffer)7 LongBuffer (java.nio.LongBuffer)7 ShortBuffer (java.nio.ShortBuffer)7 ReadOnlyBufferException (java.nio.ReadOnlyBufferException)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ObjectInputStream (java.io.ObjectInputStream)1 Test (org.junit.jupiter.api.Test)1 ValueBuffer (org.openhab.core.io.transport.modbus.ValueBuffer)1