@Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    ByteBufferInput input = new ByteBufferInput(in.nioBuffer(), false);
    T newMsg = schema.newMessage();

    schema.mergeFrom(input, newMsg);
    out.add(newMsg);
  }
  @Override
  protected <T> void roundTrip(T message, Schema<T> schema, Pipe.Schema<T> pipeSchema)
      throws Exception {
    byte[] protobuf = ProtobufIOUtil.toByteArray(message, schema, buf());

    ByteArrayInputStream protobufStream = new ByteArrayInputStream(protobuf);

    byte[] protostuff =
        ProtostuffIOUtil.toByteArray(
            ProtobufIOUtil.newPipe(protobuf, 0, protobuf.length), pipeSchema, buf());

    byte[] protostuffFromStream =
        ProtostuffIOUtil.toByteArray(ProtobufIOUtil.newPipe(protobufStream), pipeSchema, buf());

    assertTrue(Arrays.equals(protostuff, protostuffFromStream));

    T parsedMessage = schema.newMessage();
    ProtostuffIOUtil.mergeFrom(protostuff, parsedMessage, schema);
    SerializableObjects.assertEquals(message, parsedMessage);

    ByteArrayInputStream protostuffStream = new ByteArrayInputStream(protostuff);

    byte[] protobufRoundTrip =
        ProtobufIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length), pipeSchema, buf());

    byte[] protobufRoundTripFromStream =
        ProtobufIOUtil.toByteArray(ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, buf());

    assertTrue(Arrays.equals(protobufRoundTrip, protobufRoundTripFromStream));

    assertTrue(Arrays.equals(protobufRoundTrip, protobuf));
  }
Exemplo n.º 3
0
 public <T> T decode(byte[] bytes) {
   T object = (T) schema.newMessage();
   decode(bytes, object);
   return object;
 }