Search in sources :

Example 1 with DataInputDecoder

use of org.apache.flink.formats.avro.utils.DataInputDecoder in project flink by apache.

the class EncoderDecoderTest method testObjectSerialization.

private static <X> void testObjectSerialization(X obj) {
    try {
        // serialize
        ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
        {
            DataOutputStream dataOut = new DataOutputStream(baos);
            DataOutputEncoder encoder = new DataOutputEncoder();
            encoder.setOut(dataOut);
            @SuppressWarnings("unchecked") Class<X> clazz = (Class<X>) obj.getClass();
            ReflectDatumWriter<X> writer = new ReflectDatumWriter<>(clazz);
            writer.write(obj, encoder);
            dataOut.flush();
            dataOut.close();
        }
        byte[] data = baos.toByteArray();
        X result;
        // deserialize
        {
            ByteArrayInputStream bais = new ByteArrayInputStream(data);
            DataInputStream dataIn = new DataInputStream(bais);
            DataInputDecoder decoder = new DataInputDecoder();
            decoder.setIn(dataIn);
            @SuppressWarnings("unchecked") Class<X> clazz = (Class<X>) obj.getClass();
            ReflectDatumReader<X> reader = new ReflectDatumReader<>(clazz);
            // create a reuse object if possible, otherwise we have no reuse object
            X reuse = null;
            try {
                @SuppressWarnings("unchecked") X test = (X) obj.getClass().newInstance();
                reuse = test;
            } catch (Throwable t) {
            // do nothing
            }
            result = reader.read(reuse, decoder);
        }
        // check
        final String message = "Deserialized object is not the same as the original";
        if (obj.getClass().isArray()) {
            Class<?> clazz = obj.getClass();
            if (clazz == byte[].class) {
                assertArrayEquals(message, (byte[]) obj, (byte[]) result);
            } else if (clazz == short[].class) {
                assertArrayEquals(message, (short[]) obj, (short[]) result);
            } else if (clazz == int[].class) {
                assertArrayEquals(message, (int[]) obj, (int[]) result);
            } else if (clazz == long[].class) {
                assertArrayEquals(message, (long[]) obj, (long[]) result);
            } else if (clazz == char[].class) {
                assertArrayEquals(message, (char[]) obj, (char[]) result);
            } else if (clazz == float[].class) {
                assertArrayEquals(message, (float[]) obj, (float[]) result, 0.0f);
            } else if (clazz == double[].class) {
                assertArrayEquals(message, (double[]) obj, (double[]) result, 0.0);
            } else {
                assertArrayEquals(message, (Object[]) obj, (Object[]) result);
            }
        } else {
            assertEquals(message, obj, result);
        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
        fail("Test failed due to an exception: " + e.getMessage());
    }
}
Also used : DataOutputEncoder(org.apache.flink.formats.avro.utils.DataOutputEncoder) DataOutputStream(java.io.DataOutputStream) ReflectDatumWriter(org.apache.avro.reflect.ReflectDatumWriter) DataInputDecoder(org.apache.flink.formats.avro.utils.DataInputDecoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ReflectDatumReader(org.apache.avro.reflect.ReflectDatumReader)

Example 2 with DataInputDecoder

use of org.apache.flink.formats.avro.utils.DataInputDecoder in project flink by apache.

the class EncoderDecoderTest method testComplexStringsDirecty.

@Test
public void testComplexStringsDirecty() {
    try {
        Random rnd = new Random(349712539451944123L);
        for (int i = 0; i < 10; i++) {
            String testString = StringUtils.getRandomString(rnd, 10, 100);
            ByteArrayOutputStream baos = new ByteArrayOutputStream(512);
            {
                DataOutputStream dataOut = new DataOutputStream(baos);
                DataOutputEncoder encoder = new DataOutputEncoder();
                encoder.setOut(dataOut);
                encoder.writeString(testString);
                dataOut.flush();
                dataOut.close();
            }
            byte[] data = baos.toByteArray();
            // deserialize
            {
                ByteArrayInputStream bais = new ByteArrayInputStream(data);
                DataInputStream dataIn = new DataInputStream(bais);
                DataInputDecoder decoder = new DataInputDecoder();
                decoder.setIn(dataIn);
                String deserialized = decoder.readString();
                assertEquals(testString, deserialized);
            }
        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
        fail("Test failed due to an exception: " + e.getMessage());
    }
}
Also used : Random(java.util.Random) DataOutputEncoder(org.apache.flink.formats.avro.utils.DataOutputEncoder) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) DataInputDecoder(org.apache.flink.formats.avro.utils.DataInputDecoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 DataInputDecoder (org.apache.flink.formats.avro.utils.DataInputDecoder)2 DataOutputEncoder (org.apache.flink.formats.avro.utils.DataOutputEncoder)2 Random (java.util.Random)1 ReflectDatumReader (org.apache.avro.reflect.ReflectDatumReader)1 ReflectDatumWriter (org.apache.avro.reflect.ReflectDatumWriter)1 Test (org.junit.Test)1