Search in sources :

Example 21 with BinaryDecoder

use of co.cask.cdap.common.io.BinaryDecoder in project cdap by caskdata.

the class QueueEntry method deserializeHashKeys.

public static Map<String, Integer> deserializeHashKeys(byte[] bytes, int off, int len) throws IOException {
    if (bytes == null || (len == 1 && bytes[off] == 0)) {
        // No hash keys.
        return ImmutableMap.of();
    }
    ByteArrayInputStream bis = new ByteArrayInputStream(bytes, off, len);
    BinaryDecoder decoder = new BinaryDecoder(bis);
    int size = decoder.readInt();
    Map<String, Integer> hashKeys = Maps.newHashMapWithExpectedSize(size);
    while (size > 0) {
        // per avro spec, ther ecan be multiple blocks
        while (size-- > 0) {
            String key = decoder.readString();
            int value = decoder.readInt();
            hashKeys.put(key, value);
        }
        // next block length, will be always zero in this case
        size = decoder.readInt();
    }
    return hashKeys;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder)

Example 22 with BinaryDecoder

use of co.cask.cdap.common.io.BinaryDecoder in project cdap by caskdata.

the class CodecTest method testCodec.

@Test
public void testCodec() throws IOException {
    PipedOutputStream output = new PipedOutputStream();
    PipedInputStream input = new PipedInputStream(output);
    Encoder encoder = new BinaryEncoder(output);
    Decoder decoder = new BinaryDecoder(input);
    encoder.writeNull();
    Assert.assertNull(decoder.readNull());
    encoder.writeBool(true);
    Assert.assertTrue(decoder.readBool());
    encoder.writeBool(false);
    Assert.assertFalse(decoder.readBool());
    encoder.writeInt(0);
    Assert.assertEquals(0, decoder.readInt());
    encoder.writeInt(-1);
    Assert.assertEquals(-1, decoder.readInt());
    encoder.writeInt(1234);
    Assert.assertEquals(1234, decoder.readInt());
    encoder.writeInt(-1234);
    Assert.assertEquals(-1234, decoder.readInt());
    encoder.writeInt(Short.MAX_VALUE);
    Assert.assertEquals(Short.MAX_VALUE, decoder.readInt());
    encoder.writeInt(Short.MIN_VALUE);
    Assert.assertEquals(Short.MIN_VALUE, decoder.readInt());
    encoder.writeInt(Integer.MAX_VALUE);
    Assert.assertEquals(Integer.MAX_VALUE, decoder.readInt());
    encoder.writeInt(Integer.MIN_VALUE);
    Assert.assertEquals(Integer.MIN_VALUE, decoder.readInt());
    encoder.writeLong(0);
    Assert.assertEquals(0, decoder.readLong());
    encoder.writeLong(-20);
    Assert.assertEquals(-20, decoder.readLong());
    encoder.writeLong(30000);
    Assert.assertEquals(30000, decoder.readLong());
    encoder.writeLong(-600000);
    Assert.assertEquals(-600000, decoder.readLong());
    encoder.writeLong(Integer.MAX_VALUE);
    Assert.assertEquals(Integer.MAX_VALUE, decoder.readLong());
    encoder.writeLong(Integer.MIN_VALUE);
    Assert.assertEquals(Integer.MIN_VALUE, decoder.readLong());
    encoder.writeLong(Long.MAX_VALUE);
    Assert.assertEquals(Long.MAX_VALUE, decoder.readLong());
    encoder.writeLong(Long.MIN_VALUE);
    Assert.assertEquals(Long.MIN_VALUE, decoder.readLong());
    encoder.writeFloat(3.14f);
    Assert.assertEquals(3.14f, decoder.readFloat(), 0.0000001f);
    encoder.writeFloat(Short.MAX_VALUE);
    Assert.assertEquals(Short.MAX_VALUE, decoder.readFloat(), 0.0000001f);
    encoder.writeFloat(Integer.MIN_VALUE);
    Assert.assertEquals(Integer.MIN_VALUE, decoder.readFloat(), 0.0000001f);
    encoder.writeFloat((long) Integer.MAX_VALUE * Short.MAX_VALUE);
    Assert.assertEquals((long) Integer.MAX_VALUE * Short.MAX_VALUE, decoder.readFloat(), 0.0000001f);
    encoder.writeFloat(Float.MAX_VALUE);
    Assert.assertEquals(Float.MAX_VALUE, decoder.readFloat(), 0.0000001f);
    encoder.writeFloat(Float.MIN_VALUE);
    Assert.assertEquals(Float.MIN_VALUE, decoder.readFloat(), 0.0000001f);
    encoder.writeDouble(Math.E);
    Assert.assertEquals(Math.E, decoder.readDouble(), 0.0000001f);
    encoder.writeDouble(Integer.MAX_VALUE);
    Assert.assertEquals(Integer.MAX_VALUE, decoder.readDouble(), 0.0000001f);
    encoder.writeDouble(Long.MIN_VALUE);
    Assert.assertEquals(Long.MIN_VALUE, decoder.readDouble(), 0.0000001f);
    encoder.writeDouble((long) Integer.MAX_VALUE * Short.MAX_VALUE);
    Assert.assertEquals((long) Integer.MAX_VALUE * Short.MAX_VALUE, decoder.readDouble(), 0.0000001f);
    encoder.writeDouble(Double.MAX_VALUE);
    Assert.assertEquals(Double.MAX_VALUE, decoder.readDouble(), 0.0000001f);
    encoder.writeDouble(Double.MIN_VALUE);
    Assert.assertEquals(Double.MIN_VALUE, decoder.readDouble(), 0.0000001f);
    encoder.writeString("This is a testing message");
    Assert.assertEquals("This is a testing message", decoder.readString());
    String str = Character.toString((char) 200) + Character.toString((char) 20000) + Character.toString((char) 40000);
    encoder.writeString(str);
    Assert.assertEquals(str, decoder.readString());
    ByteBuffer buf = ByteBuffer.allocate(12);
    buf.asIntBuffer().put(10).put(1024).put(9999999);
    encoder.writeBytes(buf);
    IntBuffer inBuf = decoder.readBytes().asIntBuffer();
    Assert.assertEquals(10, inBuf.get());
    Assert.assertEquals(1024, inBuf.get());
    Assert.assertEquals(9999999, inBuf.get());
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) Encoder(co.cask.cdap.common.io.Encoder) IntBuffer(java.nio.IntBuffer) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Decoder(co.cask.cdap.common.io.Decoder) ByteBuffer(java.nio.ByteBuffer) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Example 23 with BinaryDecoder

use of co.cask.cdap.common.io.BinaryDecoder in project cdap by caskdata.

the class RecordWithString method testCollection.

@Test
public void testCollection() throws UnsupportedTypeException, IOException {
    List<String> list = Lists.newArrayList("1", "2", "3");
    Schema sourceSchema = new ReflectionSchemaGenerator().generate(new TypeToken<List<String>>() {
    }.getType());
    Schema targetSchema = new ReflectionSchemaGenerator().generate(new TypeToken<Set<String>>() {
    }.getType());
    PipedOutputStream output = new PipedOutputStream();
    PipedInputStream input = new PipedInputStream(output);
    new ReflectionDatumWriter<List<String>>(sourceSchema).encode(list, new BinaryEncoder(output));
    Set<String> set = new ReflectionDatumReader<>(targetSchema, new TypeToken<Set<String>>() {
    }).read(new BinaryDecoder(input), sourceSchema);
    Assert.assertEquals(Sets.newHashSet("1", "2", "3"), set);
    targetSchema = new ReflectionSchemaGenerator().generate(String[].class);
    new ReflectionDatumWriter<List<String>>(sourceSchema).encode(list, new BinaryEncoder(output));
    String[] array = new ReflectionDatumReader<>(targetSchema, new TypeToken<String[]>() {
    }).read(new BinaryDecoder(input), sourceSchema);
    Assert.assertArrayEquals(new String[] { "1", "2", "3" }, array);
}
Also used : Schema(co.cask.cdap.api.data.schema.Schema) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) TypeToken(com.google.common.reflect.TypeToken) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Test(org.junit.Test)

Example 24 with BinaryDecoder

use of co.cask.cdap.common.io.BinaryDecoder in project cdap by caskdata.

the class RecordWithString method testTypeProject.

@Test
public void testTypeProject() throws IOException, UnsupportedTypeException {
    final Record1 r1 = new Record1(10, Maps.<Integer, Value>newHashMap(), new URL("http://www.yahoo.com"));
    r1.properties.put(1, new Value(1, "Name1"));
    r1.properties.put(2, new Value(2, "Name2"));
    r1.properties.put(3, null);
    PipedOutputStream output = new PipedOutputStream();
    PipedInputStream input = new PipedInputStream(output);
    Schema sourceSchema = new ReflectionSchemaGenerator().generate(Record1.class);
    Schema targetSchema = new ReflectionSchemaGenerator().generate(Record2.class);
    new ReflectionDatumWriter<Record1>(sourceSchema).encode(r1, new BinaryEncoder(output));
    Record2 r2 = new ReflectionDatumReader<>(targetSchema, TypeToken.of(Record2.class)).read(new BinaryDecoder(input), sourceSchema);
    Assert.assertEquals(10L, r2.i.longValue());
    Assert.assertTrue(Iterables.all(r2.properties.entrySet(), new Predicate<Map.Entry<String, Value>>() {

        @Override
        public boolean apply(Map.Entry<String, Value> input) {
            Value value = r1.properties.get(Integer.valueOf(input.getKey()));
            return (value == null && input.getValue() == null) || (value.equals(input.getValue()));
        }
    }));
    Assert.assertNull(r2.name);
    Assert.assertArrayEquals(new long[] { 1L, 2L }, r2.numbers);
    Assert.assertEquals(URI.create("http://www.yahoo.com"), r2.url);
    Assert.assertEquals(r1.uuid, r2.uuid);
}
Also used : Schema(co.cask.cdap.api.data.schema.Schema) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) URL(java.net.URL) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Predicate(com.google.common.base.Predicate) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 25 with BinaryDecoder

use of co.cask.cdap.common.io.BinaryDecoder in project cdap by caskdata.

the class RecordWithString method testEnum.

@Test
public void testEnum() throws IOException, UnsupportedTypeException {
    PipedOutputStream output = new PipedOutputStream();
    PipedInputStream input = new PipedInputStream(output);
    Schema schema = new ReflectionSchemaGenerator().generate(TestEnum.class);
    ReflectionDatumWriter<TestEnum> writer = new ReflectionDatumWriter<>(schema);
    BinaryEncoder encoder = new BinaryEncoder(output);
    writer.encode(TestEnum.VALUE1, encoder);
    writer.encode(TestEnum.VALUE3, encoder);
    writer.encode(TestEnum.VALUE2, encoder);
    BinaryDecoder decoder = new BinaryDecoder(input);
    Schema readSchema = Schema.parseJson(schema.toString());
    ReflectionDatumReader<TestEnum> reader = new ReflectionDatumReader<>(readSchema, TypeToken.of(TestEnum.class));
    Assert.assertEquals(TestEnum.VALUE1, reader.read(decoder, readSchema));
    Assert.assertEquals(TestEnum.VALUE3, reader.read(decoder, readSchema));
    Assert.assertEquals(TestEnum.VALUE2, reader.read(decoder, readSchema));
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) Schema(co.cask.cdap.api.data.schema.Schema) ReflectionDatumWriter(co.cask.cdap.internal.io.ReflectionDatumWriter) PipedOutputStream(java.io.PipedOutputStream) ReflectionDatumReader(co.cask.cdap.internal.io.ReflectionDatumReader) PipedInputStream(java.io.PipedInputStream) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) BinaryDecoder(co.cask.cdap.common.io.BinaryDecoder) Test(org.junit.Test)

Aggregations

BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)36 Test (org.junit.Test)23 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)22 PipedInputStream (java.io.PipedInputStream)21 PipedOutputStream (java.io.PipedOutputStream)21 ReflectionDatumReader (co.cask.cdap.internal.io.ReflectionDatumReader)19 TypeToken (com.google.common.reflect.TypeToken)17 Schema (co.cask.cdap.api.data.schema.Schema)10 IOException (java.io.IOException)10 Decoder (co.cask.cdap.common.io.Decoder)6 ReflectionSchemaGenerator (co.cask.cdap.internal.io.ReflectionSchemaGenerator)6 ByteBufferInputStream (co.cask.common.io.ByteBufferInputStream)6 ImmutableList (com.google.common.collect.ImmutableList)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 StreamEvent (co.cask.cdap.api.flow.flowlet.StreamEvent)5 ByteBuffer (java.nio.ByteBuffer)5 List (java.util.List)5 SchemaHash (co.cask.cdap.api.data.schema.SchemaHash)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Map (java.util.Map)3