@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); }
/** Merges the {@code message} with the byte array using the given {@code schema}. */ public static <T> void mergeFrom(byte[] data, T message, Schema<T> schema, boolean numeric) throws IOException { mergeFrom(data, 0, data.length, message, schema, numeric); }
@Override protected <T> void mergeFrom(InputStream in, T message, Schema<T> schema) throws IOException { SmileIOUtil.mergeFrom(in, message, schema, false); }
@Override protected <T> void mergeFrom(byte[] data, int offset, int length, T message, Schema<T> schema) throws IOException { SmileIOUtil.mergeFrom(data, offset, length, message, schema, false); }