Search in sources :

Example 86 with DataInput

use of java.io.DataInput 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)

Example 87 with DataInput

use of java.io.DataInput in project geode by apache.

the class DataSerializableJUnitTest method testLongObject.

/**
   * Tests data serializing a {@link Long} using {@link DataSerializer#writeObject}.
   */
@Test
public void testLongObject() throws Exception {
    Long value = new Long(getRandom().nextLong());
    DataOutputStream out = getDataOutput();
    DataSerializer.writeObject(value, out);
    out.flush();
    DataInput in = getDataInput();
    Long value2 = (Long) DataSerializer.readObject(in);
    assertEquals(value, value2);
}
Also used : DataInput(java.io.DataInput) DataOutputStream(java.io.DataOutputStream) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 88 with DataInput

use of java.io.DataInput in project geode by apache.

the class DataSerializableJUnitTest method testBigUtfString.

/**
   * Tests data serializing a non-<code>null</code> {@link String} longer than 64k.
   */
@Test
public void testBigUtfString() throws Exception {
    StringBuffer sb = new StringBuffer(100000);
    for (int i = 0; i < 100000; i++) {
        if ((i % 1) == 0) {
            sb.append(Character.MAX_VALUE);
        } else {
            sb.append(Character.MIN_VALUE);
        }
    }
    String value = sb.toString();
    DataOutputStream out = getDataOutput();
    DataSerializer.writeString(value, out);
    DataSerializer.writeObject(value, out);
    out.flush();
    DataInput in = getDataInput();
    String value2 = DataSerializer.readString(in);
    assertEquals(value, value2);
    value2 = (String) DataSerializer.readObject(in);
    assertEquals(value, value2);
}
Also used : DataInput(java.io.DataInput) DataOutputStream(java.io.DataOutputStream) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 89 with DataInput

use of java.io.DataInput in project asterixdb by apache.

the class BinaryEntry method print.

// Inefficient. Just for debugging.
@SuppressWarnings("rawtypes")
public String print(ISerializerDeserializer serde) throws HyracksDataException {
    ByteArrayInputStream inStream = new ByteArrayInputStream(buf, off, len);
    DataInput dataIn = new DataInputStream(inStream);
    return serde.deserialize(dataIn).toString();
}
Also used : DataInput(java.io.DataInput) ByteArrayInputStream(java.io.ByteArrayInputStream) DataInputStream(java.io.DataInputStream)

Example 90 with DataInput

use of java.io.DataInput in project asterixdb by apache.

the class InMemoryInvertedListCursor method printInvList.

@SuppressWarnings("rawtypes")
@Override
public String printInvList(ISerializerDeserializer[] serdes) throws HyracksDataException {
    StringBuilder strBuilder = new StringBuilder();
    try {
        while (btreeCursor.hasNext()) {
            btreeCursor.next();
            ITupleReference tuple = btreeCursor.getTuple();
            ByteArrayInputStream inStream = new ByteArrayInputStream(tuple.getFieldData(1), tuple.getFieldStart(1), tuple.getFieldLength(1));
            DataInput dataIn = new DataInputStream(inStream);
            Object o = serdes[0].deserialize(dataIn);
            strBuilder.append(o.toString() + " ");
        }
    } finally {
        btreeCursor.close();
        btreeCursor.reset();
    }
    btreeAccessor.search(btreeCursor, btreePred);
    return strBuilder.toString();
}
Also used : DataInput(java.io.DataInput) ByteArrayInputStream(java.io.ByteArrayInputStream) ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference) DataInputStream(java.io.DataInputStream)

Aggregations

DataInput (java.io.DataInput)247 Test (org.junit.Test)132 DataOutputStream (java.io.DataOutputStream)117 DataInputStream (java.io.DataInputStream)112 ByteArrayInputStream (java.io.ByteArrayInputStream)106 UnitTest (org.apache.geode.test.junit.categories.UnitTest)96 ByteArrayOutputStream (java.io.ByteArrayOutputStream)29 DataOutput (java.io.DataOutput)23 IOException (java.io.IOException)21 ArrayList (java.util.ArrayList)16 ARecord (org.apache.asterix.om.base.ARecord)14 EOFException (java.io.EOFException)9 File (java.io.File)9 BufferedInputStream (java.io.BufferedInputStream)8 HashMap (java.util.HashMap)7 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)7 MembershipTest (org.apache.geode.test.junit.categories.MembershipTest)7 GrowableArray (org.apache.hyracks.data.std.util.GrowableArray)7 FileInputStream (java.io.FileInputStream)6 ByteBuffer (java.nio.ByteBuffer)6