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());
}
}
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());
}
}
Aggregations