Пример #1
0
  @Override
  protected <T> void roundTrip(T message, Schema<T> schema, Pipe.Schema<T> pipeSchema)
      throws Exception {
    byte[] smile = SmileIOUtil.toByteArray(message, schema, false);

    ByteArrayInputStream smileStream = new ByteArrayInputStream(smile);

    byte[] protostuff =
        ProtostuffIOUtil.toByteArray(
            SmileIOUtil.newPipe(smile, 0, smile.length, false), pipeSchema, buf());

    byte[] protostuffFromStream =
        ProtostuffIOUtil.toByteArray(SmileIOUtil.newPipe(smileStream, false), 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[] smileRoundTrip =
        SmileIOUtil.toByteArray(
            ProtostuffIOUtil.newPipe(protostuff, 0, protostuff.length), pipeSchema, false);

    byte[] smileRoundTripFromStream =
        SmileIOUtil.toByteArray(ProtostuffIOUtil.newPipe(protostuffStream), pipeSchema, false);

    assertTrue(Arrays.equals(smileRoundTrip, smileRoundTripFromStream));

    assertTrue(smileRoundTrip.length == smile.length);

    // comment out since smile encodes strings differently when using direct
    // SmileGenerator.writeUTF8String() ... (w/c is only used when the pipe source
    // is binary and the utf8 is intact (e.g protostuff,protobuf)

    // THE reason is that even if the string provided is ascii, smile encodes it
    // as unicode. See line 1043 of SmileGenerator.
    // assertEquals(strSmileRoundTrip, STRING.deser(smile));

    // This will fail on some messages.
    T messageSmile = schema.newMessage();
    SmileIOUtil.mergeFrom(smile, messageSmile, schema, false);
    T messageSmileRoundTrip = schema.newMessage();
    SmileIOUtil.mergeFrom(smileRoundTrip, messageSmileRoundTrip, schema, false);

    assertEquals(messageSmile, messageSmileRoundTrip);
  }
Пример #2
0
  public static void roundTrip(Message message, Schema schema, Pipe.Schema 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(protostuff.length == protostuffFromStream.length);
    assertEquals(
        StringSerializer.STRING.deser(protostuff),
        StringSerializer.STRING.deser(protostuffFromStream));

    Message parsedMessage = (Message) 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(protobufRoundTrip.length == protobufRoundTripFromStream.length);

    String strProtobufRoundTrip = StringSerializer.STRING.deser(protobufRoundTrip);

    assertEquals(strProtobufRoundTrip, StringSerializer.STRING.deser(protobufRoundTripFromStream));

    assertTrue(protobufRoundTrip.length == protobuf.length);

    assertEquals(strProtobufRoundTrip, StringSerializer.STRING.deser(protobuf));
  }