Search in sources :

Example 1 with ByteBufferInputStream

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

the class DataSerializableJUnitTest method getDataInputStream.

private DataInputStream getDataInputStream() {
    ByteBuffer bb = ByteBuffer.wrap(this.baos.toByteArray());
    ByteBufferInputStream bbis = new ByteBufferInputStream(bb);
    return new DataInputStream(bbis);
}
Also used : ByteBufferInputStream(org.apache.geode.internal.tcp.ByteBufferInputStream) DataInputStream(java.io.DataInputStream) ByteBuffer(java.nio.ByteBuffer)

Example 2 with ByteBufferInputStream

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

the class DataSerializableJUnitTest method getDataInput.

/**
   * Returns a <code>DataInput</code> to read from
   */
private DataInput getDataInput() {
    // changed this to use ByteBufferInputStream to give us better
    // test coverage of this class.
    ByteBuffer bb = ByteBuffer.wrap(this.baos.toByteArray());
    ByteBufferInputStream bbis = new ByteBufferInputStream(bb);
    return bbis;
}
Also used : ByteBufferInputStream(org.apache.geode.internal.tcp.ByteBufferInputStream) ByteBuffer(java.nio.ByteBuffer)

Example 3 with ByteBufferInputStream

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

the class DataSerializableJUnitTest method testVersionedDataSerializable.

/**
   * Tests serializing an object that implements {@link DataSerializable}
   */
@Test
public void testVersionedDataSerializable() throws Exception {
    VersionedDataSerializableImpl ds = new VersionedDataSerializableImpl(getRandom());
    VersionedDataOutputStream v = new VersionedDataOutputStream(this.baos, Version.GFE_70);
    DataSerializer.writeObject(ds, v);
    v.flush();
    ByteBuffer bb = ByteBuffer.wrap(this.baos.toByteArray());
    ByteBufferInputStream bbis = new ByteBufferInputStream(bb);
    VersionedDataInputStream vin = new VersionedDataInputStream(bbis, Version.GFE_70);
    VersionedDataSerializableImpl ds2 = (VersionedDataSerializableImpl) DataSerializer.readObject(vin);
    assertEquals(ds, ds2);
    assertTrue(ds.preMethodInvoked());
    assertTrue(ds2.preMethodInvoked());
}
Also used : ByteBufferInputStream(org.apache.geode.internal.tcp.ByteBufferInputStream) ByteBuffer(java.nio.ByteBuffer) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 4 with ByteBufferInputStream

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

the class ByteArrayData method getDataInputStream.

public DataInputStream getDataInputStream() {
    ByteBuffer bb = ByteBuffer.wrap(this.baos.toByteArray());
    ByteBufferInputStream bbis = new ByteBufferInputStream(bb);
    return new DataInputStream(bbis);
}
Also used : ByteBufferInputStream(org.apache.geode.internal.tcp.ByteBufferInputStream) DataInputStream(java.io.DataInputStream) ByteBuffer(java.nio.ByteBuffer)

Example 5 with ByteBufferInputStream

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

the class DataSerializableJUnitTest method testStatArchiveCompactValueSerialization.

/**
   * Test for {@link StatArchiveWriter#writeCompactValue} and
   * {@link StatArchiveWriter#readCompactValue}. Also added test for
   * ByteBufferInputStream#readUnsigned* methods (bug #41197).
   */
@Test
public void testStatArchiveCompactValueSerialization() throws Exception {
    // test all combos of valueToTest and + and -offsets
    long[] valuesToTest = new long[] { 0, Byte.MAX_VALUE, Byte.MIN_VALUE, Short.MAX_VALUE, Short.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Long.MAX_VALUE, Long.MIN_VALUE };
    int[] offsets = new int[] { 0, 1, 4, 9, 14, 15, 16, -1, -4, -9, -14, -15, -16 };
    // write all combos of longs to the outputstream
    HeapDataOutputStream hdos = new HeapDataOutputStream(Version.CURRENT);
    DataOutput out = hdos;
    for (long valueToTest : valuesToTest) {
        for (int offset : offsets) {
            long val = valueToTest + offset;
            StatArchiveWriter.writeCompactValue(val, out);
        }
    }
    // now read all the combos
    byte[] bytes = hdos.toByteArray();
    DataInput in = new DataInputStream(new ByteArrayInputStream(bytes));
    for (long valueToTest : valuesToTest) {
        for (int offset : offsets) {
            long expectedVal = valueToTest + offset;
            long val = StatArchiveWriter.readCompactValue(in);
            assertEquals(expectedVal, val);
        }
    }
    // also test using ByteBufferInputStream (bug #41197)
    in = new ByteBufferInputStream(ByteBuffer.wrap(bytes));
    for (long valueToTest : valuesToTest) {
        for (int offset : offsets) {
            long expectedVal = valueToTest + offset;
            long val = StatArchiveWriter.readCompactValue(in);
            assertEquals(expectedVal, val);
        }
    }
    // now check ByteBufferInputStream#readUnsignedShort explicitly
    // readUnsignedByte is already tested in StatArchiveWriter.readCompactValue
    // above likely in a more thorough manner than a simple explicit test would
    short[] shortValuesToTest = new short[] { 0, Byte.MAX_VALUE, Byte.MIN_VALUE, Short.MAX_VALUE, Short.MIN_VALUE };
    ByteBufferOutputStream bos = new ByteBufferOutputStream();
    out = new DataOutputStream(bos);
    for (short valueToTest : shortValuesToTest) {
        for (int offset : offsets) {
            short val = (short) (valueToTest + offset);
            out.writeShort(val);
        }
    }
    // now read all the combos
    in = new ByteBufferInputStream(bos.getContentBuffer());
    for (short valueToTest : shortValuesToTest) {
        for (int offset : offsets) {
            int expectedVal = (valueToTest + offset) & 0xffff;
            int val = in.readUnsignedShort();
            assertEquals(expectedVal, val);
        }
    }
}
Also used : DataOutput(java.io.DataOutput) DataOutputStream(java.io.DataOutputStream) ByteBufferInputStream(org.apache.geode.internal.tcp.ByteBufferInputStream) DataInputStream(java.io.DataInputStream) DataInput(java.io.DataInput) ByteArrayInputStream(java.io.ByteArrayInputStream) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Aggregations

ByteBufferInputStream (org.apache.geode.internal.tcp.ByteBufferInputStream)8 ByteBuffer (java.nio.ByteBuffer)6 DataInputStream (java.io.DataInputStream)3 IOException (java.io.IOException)2 UnitTest (org.apache.geode.test.junit.categories.UnitTest)2 Test (org.junit.Test)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInput (java.io.DataInput)1 DataOutput (java.io.DataOutput)1 DataOutputStream (java.io.DataOutputStream)1 PdxSerializationException (org.apache.geode.pdx.PdxSerializationException)1