Search in sources :

Example 41 with BinaryDecoder

use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by cdapio.

the class ASMDatumCodecTest method testInt.

@Test
public void testInt() throws UnsupportedTypeException, IOException {
    TypeToken<Integer> type = new TypeToken<Integer>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<Integer> writer = getWriter(type);
    writer.encode(12234234, new BinaryEncoder(os));
    ReflectionDatumReader<Integer> reader = new ReflectionDatumReader<>(getSchema(type), type);
    int value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(12234234, value);
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 42 with BinaryDecoder

use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by cdapio.

the class ASMDatumCodecTest method testEnum.

@Test
public void testEnum() throws UnsupportedTypeException, IOException {
    TypeToken<TestEnum> type = new TypeToken<TestEnum>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<TestEnum> writer = getWriter(type);
    BinaryEncoder encoder = new BinaryEncoder(os);
    writer.encode(TestEnum.VALUE1, encoder);
    writer.encode(TestEnum.VALUE4, encoder);
    writer.encode(TestEnum.VALUE3, encoder);
    ReflectionDatumReader<TestEnum> reader = new ReflectionDatumReader<>(getSchema(type), type);
    TestEnum value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(TestEnum.VALUE1, value);
    value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(TestEnum.VALUE4, value);
    value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(TestEnum.VALUE3, value);
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 43 with BinaryDecoder

use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by cdapio.

the class ASMDatumCodecTest method testShort.

@Test
public void testShort() throws UnsupportedTypeException, IOException {
    TypeToken<Short> type = new TypeToken<Short>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<Short> writer = getWriter(type);
    writer.encode((short) 3000, new BinaryEncoder(os));
    ReflectionDatumReader<Short> reader = new ReflectionDatumReader<>(getSchema(type), type);
    short value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals((short) 3000, value);
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 44 with BinaryDecoder

use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by cdapio.

the class ASMDatumCodecTest method testURI.

@Test
public void testURI() throws IOException, UnsupportedTypeException {
    TypeToken<List<URI>> type = new TypeToken<List<URI>>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<List<URI>> writer = getWriter(type);
    List<URI> writeValue = ImmutableList.of(URI.create("http://www.abc.com"));
    writer.encode(writeValue, new BinaryEncoder(os));
    ReflectionDatumReader<List<URI>> reader = new ReflectionDatumReader<>(getSchema(type), type);
    Assert.assertEquals(writeValue, reader.read(new BinaryDecoder(is), getSchema(type)));
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) URI(java.net.URI) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 45 with BinaryDecoder

use of io.cdap.cdap.common.io.BinaryDecoder in project cdap by cdapio.

the class ASMDatumCodecTest method testDouble.

@Test
public void testDouble() throws UnsupportedTypeException, IOException {
    TypeToken<Double> type = new TypeToken<Double>() {
    };
    PipedOutputStream os = new PipedOutputStream();
    PipedInputStream is = new PipedInputStream(os);
    DatumWriter<Double> writer = getWriter(type);
    writer.encode(3.14d, new BinaryEncoder(os));
    ReflectionDatumReader<Double> reader = new ReflectionDatumReader<>(getSchema(type), type);
    double value = reader.read(new BinaryDecoder(is), getSchema(type));
    Assert.assertEquals(3.14d, value, 0.000001d);
}
Also used : BinaryEncoder(io.cdap.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(io.cdap.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(io.cdap.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Aggregations

BinaryDecoder (io.cdap.cdap.common.io.BinaryDecoder)52 BinaryEncoder (io.cdap.cdap.common.io.BinaryEncoder)42 Test (org.junit.Test)42 PipedInputStream (java.io.PipedInputStream)40 PipedOutputStream (java.io.PipedOutputStream)40 ReflectionDatumReader (io.cdap.cdap.internal.io.ReflectionDatumReader)34 TypeToken (com.google.common.reflect.TypeToken)32 Schema (io.cdap.cdap.api.data.schema.Schema)16 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ImmutableList (com.google.common.collect.ImmutableList)10 ReflectionSchemaGenerator (io.cdap.cdap.internal.io.ReflectionSchemaGenerator)10 IOException (java.io.IOException)10 List (java.util.List)10 Decoder (io.cdap.cdap.common.io.Decoder)8 ImmutableMap (com.google.common.collect.ImmutableMap)6 Map (java.util.Map)6 ReflectionDatumWriter (io.cdap.cdap.internal.io.ReflectionDatumWriter)4 Predicate (com.google.common.base.Predicate)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 DataSetException (io.cdap.cdap.api.dataset.DataSetException)2