Code example for Buffer

Methods: Buffer.position, capacity, limit, mark, position, reset

0
    /* 
     * Class under test for Buffer position(int) 
     */ 
    public static void testPositionint(Buffer buf) {
        // save state 
        int oldPosition = buf.position();
        int oldLimit = buf.limit();
         
        try { 
            buf.position(-1);
            fail("Should throw Exception"); //$NON-NLS-1$
        } catch (IllegalArgumentException e) {
            // expected 
        } 
        try { 
            buf.position(buf.limit() + 1);
            fail("Should throw Exception"); //$NON-NLS-1$
        } catch (IllegalArgumentException e) {
            // expected 
        } 
         
        buf.mark();
        buf.position(buf.position());
        buf.reset();
        assertEquals(buf.position(), oldPosition);
         
        buf.position(0);
        assertEquals(buf.position(), 0);
        buf.position(buf.limit());
        assertEquals(buf.position(), buf.limit());
         
        if (buf.capacity() > 0) {
            buf.limit(buf.capacity());
            buf.position(buf.limit());
            buf.mark();
            buf.position(buf.limit() - 1);
            assertEquals(buf.position(), buf.limit() - 1);
            // mark should be invalid 
            try { 
                buf.reset();
                fail("Should throw Exception"); //$NON-NLS-1$
            } catch (InvalidMarkException e) {
                // expected 
            } 
        } 
         
        Buffer ret = buf.position(0);
        assertSame(ret, buf);
 
        // restore state 
        buf.limit(oldLimit);
        buf.position(oldPosition);
    } 
 
    public static void testRemaining(Buffer buf) {
        assertEquals(buf.remaining(), buf.limit() - buf.position());
    }