Search in sources :

Example 6 with Encoder

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

the class KeyIdentifierCodec method encode.

@Override
public byte[] encode(KeyIdentifier keyIdentifier) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(bos);
    encoder.writeInt(KeyIdentifier.Schemas.getVersion());
    DatumWriter<KeyIdentifier> writer = writerFactory.create(KEY_IDENTIFIER_TYPE, KeyIdentifier.Schemas.getCurrentSchema());
    writer.encode(keyIdentifier, encoder);
    return bos.toByteArray();
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) Encoder(co.cask.cdap.common.io.Encoder) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 7 with Encoder

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

the class QueueEntry method serializeEmptyHashKeys.

// many entries will have no hash keys. Serialize that once and for good
private static byte[] serializeEmptyHashKeys() {
    try {
        // we don't synchronize here: the worst thing that go wrong here is repeated assignment to the same value
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Encoder encoder = new BinaryEncoder(bos);
        encoder.writeInt(0);
        return bos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException("encoding empty hash keys went wrong - bailing out: " + e.getMessage(), e);
    }
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) Encoder(co.cask.cdap.common.io.Encoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 8 with Encoder

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

the class StreamEventCodec method encodePayload.

/**
   * Encodes the given {@link StreamEvent} into {@code byte[]} that could become
   * payload of a QueueEntry.
   *
   * @param event The {@link StreamEvent} to encode.
   * @return Encoded {@code byte[]}.
   */
public byte[] encodePayload(StreamEvent event) {
    // TODO: This is a bit hacky to do it directly for now, for performance reason.
    ByteBuffer body = event.getBody();
    Map<String, String> headers = event.getHeaders();
    long timestamp = event.getTimestamp();
    // Some assumption on the header size to minimize array copying
    // 16 bytes Schema hash + body size + (header size) * (50 bytes key/value pair) + 9 bytes timestamp (vlong encoding)
    ByteArrayOutputStream os = new ByteArrayOutputStream(16 + body.remaining() + headers.size() * 50 + 9);
    Encoder encoder = new BinaryEncoder(os);
    try {
        // Write the schema hash
        os.write(STREAM_EVENT_SCHEMA.getSchemaHash().toByteArray());
        StreamEventDataCodec.encode(event, encoder);
        encoder.writeLong(timestamp);
        return os.toByteArray();
    } catch (IOException e) {
        // It should never happens, otherwise something very wrong.
        throw Throwables.propagate(e);
    }
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) Encoder(co.cask.cdap.common.io.Encoder) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer)

Example 9 with Encoder

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

the class AccessTokenCodec method encode.

@Override
public byte[] encode(AccessToken token) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Encoder encoder = new BinaryEncoder(bos);
    encoder.writeInt(AccessToken.Schemas.getVersion());
    DatumWriter<AccessToken> writer = writerFactory.create(ACCESS_TOKEN_TYPE, AccessToken.Schemas.getCurrentSchema());
    writer.encode(token, encoder);
    return bos.toByteArray();
}
Also used : BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) Encoder(co.cask.cdap.common.io.Encoder) BinaryEncoder(co.cask.cdap.common.io.BinaryEncoder) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 10 with Encoder

use of co.cask.cdap.common.io.Encoder 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)

Aggregations

Encoder (co.cask.cdap.common.io.Encoder)12 BinaryEncoder (co.cask.cdap.common.io.BinaryEncoder)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 Schema (co.cask.cdap.api.data.schema.Schema)4 IOException (java.io.IOException)3 ByteBuffer (java.nio.ByteBuffer)3 Label (org.objectweb.asm.Label)3 Iterator (java.util.Iterator)2 Map (java.util.Map)2 Set (java.util.Set)2 BinaryDecoder (co.cask.cdap.common.io.BinaryDecoder)1 Decoder (co.cask.cdap.common.io.Decoder)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Buffer (java.nio.Buffer)1 IntBuffer (java.nio.IntBuffer)1 Collection (java.util.Collection)1 UUID (java.util.UUID)1