Search in sources :

Example 6 with BufferOverflowException

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

the class FileChannelTest method test_map_ReadWrite.

/**
     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
     */
public void test_map_ReadWrite() throws IOException {
    MappedByteBuffer mapped = null;
    writeDataToFile(fileOfReadWriteFileChannel);
    mapped = readWriteFileChannel.map(MapMode.READ_WRITE, 0, CONTENT.length());
    // put something will change its channel
    ByteBuffer returnByPut = mapped.put(TEST_BYTES);
    assertSame(returnByPut, mapped);
    String checkString = "test" + CONTENT.substring(4);
    ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
    mapped.force();
    readWriteFileChannel.position(0);
    readWriteFileChannel.read(checkBuffer);
    assertEquals(checkString, new String(checkBuffer.array(), "iso8859-1"));
    try {
        mapped.put(("test" + CONTENT).getBytes("iso8859-1"));
        fail("should throw BufferOverflowException.");
    } catch (BufferOverflowException ex) {
    // expected;
    }
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) BufferOverflowException(java.nio.BufferOverflowException) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 7 with BufferOverflowException

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

the class OldAndroidNIOTest method byteBufferTest.

private void byteBufferTest(ByteBuffer b) {
    checkBuffer(b);
    // Duplicate buffers revert to big-endian.
    b.order(ByteOrder.LITTLE_ENDIAN);
    ByteBuffer dupe = b.duplicate();
    assertEquals(ByteOrder.BIG_ENDIAN, dupe.order());
    b.order(ByteOrder.BIG_ENDIAN);
    // Bounds checks
    try {
        b.put(-1, (byte) 0);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        b.put(b.limit(), (byte) 0);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // IndexOutOfBoundsException: offset < 0
    try {
        byte[] data = new byte[8];
        b.position(0);
        b.put(data, -1, 2);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // IndexOutOfBoundsException: length > array.length - offset
    try {
        byte[] data = new byte[8];
        b.position(0);
        b.put(data, 1, 8);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // BufferOverflowException: length > remaining()
    try {
        byte[] data = new byte[8];
        b.position(b.limit() - 2);
        b.put(data, 0, 3);
        fail("expected exception not thrown");
    } catch (BufferOverflowException e) {
    // expected
    }
    // Fill buffer with bytes A0 A1 A2 A3 ...
    b.position(0);
    for (int i = 0; i < b.capacity(); i++) {
        b.put((byte) (0xA0 + i));
    }
    try {
        b.put((byte) 0xFF);
        fail("expected exception not thrown");
    } catch (BufferOverflowException e) {
    // expected
    }
    b.position(0);
    assertEquals((byte) 0xA7, b.get(7));
    try {
        b.get(12);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        b.get(-10);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    b.position(0);
    b.order(ByteOrder.LITTLE_ENDIAN);
    assertEquals((byte) 0xA0, b.get());
    assertEquals((byte) 0xA1, b.get());
    assertEquals((byte) 0xA2, b.get());
    assertEquals((byte) 0xA3, b.get());
    assertEquals((byte) 0xA4, b.get());
    assertEquals((byte) 0xA5, b.get());
    assertEquals((byte) 0xA6, b.get());
    assertEquals((byte) 0xA7, b.get());
    assertEquals((byte) 0xA8, b.get());
    assertEquals((byte) 0xA9, b.get());
    assertEquals((byte) 0xAA, b.get());
    assertEquals((byte) 0xAB, b.get());
    try {
        b.get();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    b.position(0);
    b.order(ByteOrder.BIG_ENDIAN);
    assertEquals((byte) 0xA0, b.get());
    assertEquals((byte) 0xA1, b.get());
    assertEquals((byte) 0xA2, b.get());
    assertEquals((byte) 0xA3, b.get());
    assertEquals((byte) 0xA4, b.get());
    assertEquals((byte) 0xA5, b.get());
    assertEquals((byte) 0xA6, b.get());
    assertEquals((byte) 0xA7, b.get());
    assertEquals((byte) 0xA8, b.get());
    assertEquals((byte) 0xA9, b.get());
    assertEquals((byte) 0xAA, b.get());
    assertEquals((byte) 0xAB, b.get());
    try {
        b.get();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    b.position(6);
    b.limit(10);
    assertEquals((byte) 0xA6, b.get());
    // Check sliced buffer
    b.position(6);
    ByteBuffer bb = b.slice();
    checkBuffer(bb);
    assertEquals(0, bb.position());
    assertEquals(4, bb.limit());
    assertEquals(4, bb.capacity());
    assertEquals((byte) 0xA6, bb.get());
    assertEquals((byte) 0xA7, bb.get());
    assertEquals((byte) 0xA8, bb.get());
    assertEquals((byte) 0xA9, bb.get());
    try {
        bb.get();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    // Reset position and limit
    b.position(0);
    b.limit(b.capacity());
    // Check 'getShort'
    b.order(ByteOrder.LITTLE_ENDIAN);
    b.position(0);
    assertEquals((short) 0xA1A0, b.getShort());
    assertEquals((short) 0xA3A2, b.getShort());
    assertEquals((short) 0xA5A4, b.getShort());
    assertEquals((short) 0xA7A6, b.getShort());
    assertEquals((short) 0xA9A8, b.getShort());
    assertEquals((short) 0xABAA, b.getShort());
    try {
        bb.getShort();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    b.order(ByteOrder.BIG_ENDIAN);
    b.position(0);
    assertEquals((short) 0xA0A1, b.getShort());
    assertEquals((short) 0xA2A3, b.getShort());
    assertEquals((short) 0xA4A5, b.getShort());
    assertEquals((short) 0xA6A7, b.getShort());
    assertEquals((short) 0xA8A9, b.getShort());
    assertEquals((short) 0xAAAB, b.getShort());
    try {
        bb.getShort();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    // Check 'getInt'
    b.order(ByteOrder.LITTLE_ENDIAN);
    b.position(0);
    assertEquals(0xA3A2A1A0, b.getInt());
    assertEquals(0xA7A6A5A4, b.getInt());
    assertEquals(0xABAAA9A8, b.getInt());
    try {
        bb.getInt();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    b.order(ByteOrder.BIG_ENDIAN);
    b.position(0);
    assertEquals(0xA0A1A2A3, b.getInt());
    assertEquals(0xA4A5A6A7, b.getInt());
    assertEquals(0xA8A9AAAB, b.getInt());
    try {
        bb.getInt();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    // Check 'getFloat'
    b.order(ByteOrder.LITTLE_ENDIAN);
    b.position(0);
    assertEquals(0xA3A2A1A0, Float.floatToRawIntBits(b.getFloat()));
    assertEquals(0xA7A6A5A4, Float.floatToRawIntBits(b.getFloat()));
    assertEquals(0xABAAA9A8, Float.floatToRawIntBits(b.getFloat()));
    try {
        b.getFloat();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    b.order(ByteOrder.BIG_ENDIAN);
    b.position(0);
    assertEquals(0xA0A1A2A3, Float.floatToRawIntBits(b.getFloat()));
    assertEquals(0xA4A5A6A7, Float.floatToRawIntBits(b.getFloat()));
    assertEquals(0xA8A9AAAB, Float.floatToRawIntBits(b.getFloat()));
    try {
        b.getFloat();
        fail("expected exception not thrown");
    } catch (BufferUnderflowException e) {
    // expected
    }
    // Check 'getDouble(int position)'
    b.order(ByteOrder.LITTLE_ENDIAN);
    assertEquals(0xA7A6A5A4A3A2A1A0L, Double.doubleToRawLongBits(b.getDouble(0)));
    assertEquals(0xA8A7A6A5A4A3A2A1L, Double.doubleToRawLongBits(b.getDouble(1)));
    try {
        b.getDouble(-1);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        b.getDouble(5);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    b.order(ByteOrder.BIG_ENDIAN);
    assertEquals(0xA0A1A2A3A4A5A6A7L, Double.doubleToRawLongBits(b.getDouble(0)));
    assertEquals(0xA1A2A3A4A5A6A7A8L, Double.doubleToRawLongBits(b.getDouble(1)));
    try {
        b.getDouble(-1);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        b.getDouble(5);
        fail("expected exception not thrown");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    // Slice and check 'getInt'
    b.position(1);
    b.limit(5);
    b.order(ByteOrder.LITTLE_ENDIAN);
    bb = b.slice();
    assertEquals(4, bb.capacity());
    assertEquals(ByteOrder.BIG_ENDIAN, bb.order());
    assertEquals(0xA1A2A3A4, bb.getInt(0));
    bb.order(ByteOrder.LITTLE_ENDIAN);
    assertEquals(0xA4A3A2A1, bb.getInt(0));
    bb.order(ByteOrder.LITTLE_ENDIAN);
    ShortBuffer sb = bb.asShortBuffer();
    checkBuffer(sb);
    assertEquals(2, sb.capacity());
    assertEquals((short) 0xA2A1, sb.get());
    assertEquals((short) 0xA4A3, sb.get());
    bb.order(ByteOrder.BIG_ENDIAN);
    sb = bb.asShortBuffer();
    checkBuffer(sb);
    assertEquals(2, sb.capacity());
    assertEquals((short) 0xA1A2, sb.get());
    assertEquals((short) 0xA3A4, sb.get());
    bb.order(ByteOrder.LITTLE_ENDIAN);
    IntBuffer ib = bb.asIntBuffer();
    checkBuffer(ib);
    assertEquals(1, ib.capacity());
    assertEquals(0xA4A3A2A1, ib.get());
    bb.order(ByteOrder.BIG_ENDIAN);
    ib = bb.asIntBuffer();
    checkBuffer(ib);
    assertEquals(1, ib.capacity());
    assertEquals(0xA1A2A3A4, ib.get());
    bb.order(ByteOrder.LITTLE_ENDIAN);
    FloatBuffer fb = bb.asFloatBuffer();
    checkBuffer(fb);
    assertEquals(1, fb.capacity());
    assertEquals(0xA4A3A2A1, Float.floatToRawIntBits(fb.get()));
    bb.order(ByteOrder.BIG_ENDIAN);
    fb = bb.asFloatBuffer();
    checkBuffer(fb);
    assertEquals(1, fb.capacity());
    assertEquals(0xA1A2A3A4, Float.floatToRawIntBits(fb.get()));
}
Also used : IntBuffer(java.nio.IntBuffer) FloatBuffer(java.nio.FloatBuffer) BufferOverflowException(java.nio.BufferOverflowException) ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 8 with BufferOverflowException

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

the class ShortBufferTest method testPutshortArrayintint.

/*
     * Class under test for java.nio.ShortBuffer put(short[], int, int)
     */
public void testPutshortArrayintint() {
    buf.clear();
    short[] array = new short[buf.capacity()];
    try {
        buf.put(new short[buf.capacity() + 1], 0, buf.capacity() + 1);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (BufferOverflowException e) {
    // expected
    }
    assertEquals(buf.position(), 0);
    try {
        buf.put(array, -1, array.length);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        buf.put(array, array.length + 1, 0);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    buf.put(array, array.length, 0);
    assertEquals(buf.position(), 0);
    try {
        buf.put(array, 0, -1);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        buf.put((short[]) null, 0, -1);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        buf.put(array, 2, array.length);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        buf.put(array, Integer.MAX_VALUE, 1);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        buf.put(array, 1, Integer.MAX_VALUE);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (BufferOverflowException expected) {
    } catch (IndexOutOfBoundsException expected) {
    }
    assertEquals(buf.position(), 0);
    loadTestData2(array, 0, array.length);
    ShortBuffer ret = buf.put(array, 0, array.length);
    assertEquals(buf.position(), buf.capacity());
    assertContentEquals(buf, array, 0, array.length);
    assertSame(ret, buf);
}
Also used : BufferOverflowException(java.nio.BufferOverflowException) ShortBuffer(java.nio.ShortBuffer)

Example 9 with BufferOverflowException

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

the class ShortBufferTest method testPutshortArray.

/*
     * Class under test for java.nio.ShortBuffer put(short[])
     */
public void testPutshortArray() {
    short[] array = new short[1];
    buf.clear();
    for (int i = 0; i < buf.capacity(); i++) {
        assertEquals(buf.position(), i);
        array[0] = (short) i;
        ShortBuffer ret = buf.put(array);
        assertEquals(buf.get(i), (short) i);
        assertSame(ret, buf);
    }
    try {
        buf.put(array);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (BufferOverflowException e) {
    // expected
    }
    try {
        buf.position(buf.limit());
        buf.put((short[]) null);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : BufferOverflowException(java.nio.BufferOverflowException) ShortBuffer(java.nio.ShortBuffer)

Example 10 with BufferOverflowException

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

the class BufferOverflowExceptionTest method test_Constructor.

/**
     *@tests {@link java.nio.BufferOverflowException#BufferOverflowException()}
     */
public void test_Constructor() {
    BufferOverflowException exception = new BufferOverflowException();
    assertNull(exception.getMessage());
    assertNull(exception.getLocalizedMessage());
    assertNull(exception.getCause());
}
Also used : BufferOverflowException(java.nio.BufferOverflowException)

Aggregations

BufferOverflowException (java.nio.BufferOverflowException)77 ByteBuffer (java.nio.ByteBuffer)27 ShortBufferException (javax.crypto.ShortBufferException)10 CharBuffer (java.nio.CharBuffer)9 ShortBuffer (java.nio.ShortBuffer)7 Test (org.junit.Test)7 FloatBuffer (java.nio.FloatBuffer)6 IntBuffer (java.nio.IntBuffer)6 BufferUnderflowException (java.nio.BufferUnderflowException)5 ReadOnlyBufferException (java.nio.ReadOnlyBufferException)5 IpPrefix (android.net.IpPrefix)4 IOException (java.io.IOException)4 Inet6Address (java.net.Inet6Address)4 DoubleBuffer (java.nio.DoubleBuffer)4 LongBuffer (java.nio.LongBuffer)4 MappedByteBuffer (java.nio.MappedByteBuffer)4 Pointer (com.cetsoft.imcache.offheap.bytebuffer.Pointer)2 EOFException (java.io.EOFException)2 CoderResult (java.nio.charset.CoderResult)2 JSONObject (org.json_voltpatches.JSONObject)2