Search in sources :

Example 11 with ByteSource

use of org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource in project geode by apache.

the class ByteSourceJUnitTest method testGetFloatInt.

@Test
public void testGetFloatInt() {
    ByteBuffer bb = ByteBuffer.allocate(20);
    FloatBuffer fb = bb.asFloatBuffer();
    fb.put(1.1f);
    fb.put(2.2f);
    fb.put(3.3f);
    fb.put(4.4f);
    fb.put(5.5f);
    byte[] bytes = bb.array();
    ByteSource bs = createByteSource(bytes);
    bs.position(3);
    float f = bs.getFloat(0);
    assertEquals(1.1f, f, 0.0001);
    assertEquals(3, bs.position());
    f = bs.getFloat(4);
    assertEquals(2.2f, f, 0.0001);
    assertEquals(3, bs.position());
    f = bs.getFloat(4 * 4);
    assertEquals(5.5f, f, 0.0001);
    assertEquals(3, bs.position());
    try {
        bs.getFloat((4 * 4) + 1);
        fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException expected) {
    }
}
Also used : ByteSource(org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) UnitTest(org.apache.geode.test.junit.categories.UnitTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 12 with ByteSource

use of org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource in project geode by apache.

the class ByteSourceJUnitTest method testGetInt1.

@Test
public void testGetInt1() {
    ByteBuffer bb = ByteBuffer.allocate(20);
    IntBuffer ib = bb.asIntBuffer();
    ib.put(0x11102233);
    ib.put(0x22203344);
    ib.put(0x33304455);
    ib.put(0x44405566);
    ib.put(0x55506677);
    byte[] bytes = bb.array();
    ByteSource bs = createByteSource(bytes);
    int i = bs.getInt();
    assertEquals(0x11102233, i);
    assertEquals(4, bs.position());
    i = bs.getInt();
    assertEquals(0x22203344, i);
    assertEquals(8, bs.position());
    bs.position(4 * 4);
    i = bs.getInt();
    assertEquals(0x55506677, i);
    assertEquals(20, bs.position());
    try {
        bs.getInt();
        fail("expected BufferUnderflowException");
    } catch (BufferUnderflowException expected) {
    }
}
Also used : IntBuffer(java.nio.IntBuffer) ByteSource(org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) UnitTest(org.apache.geode.test.junit.categories.UnitTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 13 with ByteSource

use of org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource in project geode by apache.

the class ByteSourceJUnitTest method testGetCharInt.

@Test
public void testGetCharInt() {
    ByteBuffer bb = ByteBuffer.allocate(10);
    CharBuffer cb = bb.asCharBuffer();
    cb.put("abcde");
    byte[] bytes = bb.array();
    ByteSource bs = createByteSource(bytes);
    bs.position(3);
    char c = bs.getChar(0);
    assertEquals('a', c);
    assertEquals(3, bs.position());
    c = bs.getChar(2);
    assertEquals('b', c);
    assertEquals(3, bs.position());
    c = bs.getChar(8);
    assertEquals('e', c);
    assertEquals(3, bs.position());
    try {
        bs.getChar(10);
        fail("expected IndexOutOfBoundsException");
    } catch (IndexOutOfBoundsException expected) {
    }
}
Also used : CharBuffer(java.nio.CharBuffer) ByteSource(org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) UnitTest(org.apache.geode.test.junit.categories.UnitTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 14 with ByteSource

use of org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource in project geode by apache.

the class ByteSourceJUnitTest method testArray.

@Test
public void testArray() {
    byte[] bytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    ByteSource bs = createByteSource(bytes);
    if (bs.hasArray()) {
        assertEquals(bytes, bs.array());
        ByteBuffer bb = ByteBuffer.wrap(bytes);
        bb.position(1);
        bs = ByteSourceFactory.create(bb.slice());
        assertEquals(bytes, bs.array());
    } else {
        try {
            bs.array();
            fail("expected UnsupportedOperationException");
        } catch (UnsupportedOperationException expected) {
        }
    }
}
Also used : ByteSource(org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) UnitTest(org.apache.geode.test.junit.categories.UnitTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 15 with ByteSource

use of org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource in project geode by apache.

the class ByteSourceJUnitTest method testDuplicate.

@Test
public void testDuplicate() {
    ByteSource bs = createByteSource(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 });
    ByteSource dup = bs.duplicate();
    assertEquals(bs, dup);
    dup.position(1);
    assertNotEquals(bs, dup);
    dup.position(0);
    assertEquals(bs, dup);
    dup.limit(2);
    assertNotEquals(bs, dup);
    dup.limit(bs.limit());
    assertEquals(bs, dup);
    if (bs.hasArray()) {
        byte[] bsBytes = bs.array();
        byte[] dupBytes = dup.array();
        assertEquals(bsBytes, dupBytes);
        assertNotEquals(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }, dupBytes);
    }
    bs.position(1);
    bs.limit(2);
    ByteSource dup2 = bs.duplicate();
    assertEquals(bs, dup2);
    assertEquals(1, dup2.position());
    assertEquals(2, dup2.limit());
}
Also used : ByteSource(org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource) Test(org.junit.Test) SerializationTest(org.apache.geode.test.junit.categories.SerializationTest) UnitTest(org.apache.geode.test.junit.categories.UnitTest) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

ByteSource (org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource)33 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)29 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)29 UnitTest (org.apache.geode.test.junit.categories.UnitTest)29 Test (org.junit.Test)29 ByteBuffer (java.nio.ByteBuffer)16 BufferUnderflowException (java.nio.BufferUnderflowException)9 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 InternalGemFireException (org.apache.geode.InternalGemFireException)1 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)1 Version (org.apache.geode.internal.Version)1