Java Code Examples for io.protostuff.ProtobufIOUtil#mergeFrom()

The following examples show how to use io.protostuff.ProtobufIOUtil#mergeFrom() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InheritanceTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testInheritanceProtobuf() throws IOException
{
    Schema<InputSystem> schema = getSchema(InputSystem.class);
    InputSystem sys = new InputSystem();
    KeyBoard kb = new KeyBoard();
    Mouse ms = new Mouse();
    kb.setName("Test");
    kb.setNumberOfKeys(10);
    ms.setName("Test1");
    ms.setNumberOfButtons(2);
    List<InputDevice> devices = new ArrayList<InputDevice>();
    devices.add(ms);
    devices.add(kb);
    sys.setInputDevices(devices);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ProtobufIOUtil.writeTo(out, sys, schema, buf());
    byte[] listData = out.toByteArray();
    InputSystem deserSystem = new InputSystem();
    ByteArrayInputStream in = new ByteArrayInputStream(listData);
    ProtobufIOUtil.mergeFrom(in, deserSystem, schema, buf());

    assertEquals(sys, deserSystem);
}
 
Example 2
Source File: PolymorphicSerializationTest.java    From protostuff with Apache License 2.0 6 votes vote down vote up
public void testProtobuf() throws Exception
{
    Schema<Zoo> schema = RuntimeSchema.getSchema(Zoo.class);
    Zoo p = filledZoo();

    byte[] data = ProtobufIOUtil.toByteArray(p, schema, buf());
    // System.err.println("protobuf: " + data.length);

    Zoo p2 = new Zoo();
    ProtobufIOUtil.mergeFrom(data, p2, schema);

    assertEquals(p, p2);

    List<Zoo> list = new ArrayList<Zoo>();
    list.add(p);
    list.add(p2);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ProtobufIOUtil.writeListTo(out, list, schema, buf());
    byte[] listData = out.toByteArray();

    ByteArrayInputStream in = new ByteArrayInputStream(listData);
    List<Zoo> parsedList = ProtobufIOUtil.parseListFrom(in, schema);

    assertEquals(list, parsedList);
}
 
Example 3
Source File: SerializationUtil.java    From springboot-learn with MIT License 5 votes vote down vote up
public static <T> T deserialize(byte[] data, Class<?> genericClass) {
    try {
        T tMsg = (T) objenesis.newInstance(genericClass);
        Schema<T> schema = (Schema<T>) getSchema(genericClass);
        ProtobufIOUtil.mergeFrom(data, tMsg, schema);

        return tMsg;
    } catch (Exception e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example 4
Source File: BusProtoStuffMessageConverter.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
private static Object readContent(final EventType eventType, final byte[] content) {
    final Class<?> targetClass = eventType.getTargetClass();
    if (targetClass == null) {
        LOG.error("Cannot read clazz header for given EventType value {}, missing mapping", eventType.getValue());
        throw new MessageConversionException("Missing mapping of EventType for value " + eventType.getValue());
    }
    @SuppressWarnings("unchecked")
    final Schema<Object> schema = (Schema<Object>) RuntimeSchema.getSchema(targetClass);
    final Object deserializeEvent = schema.newMessage();
    ProtobufIOUtil.mergeFrom(content, deserializeEvent, schema);
    return deserializeEvent;
}
 
Example 5
Source File: MathObjectsTest.java    From protostuff with Apache License 2.0 5 votes vote down vote up
public void testProtobuf() throws Exception
{
    Schema<Payment> schema = RuntimeSchema.getSchema(Payment.class);
    Payment p = filledPayment();

    byte[] data = ProtobufIOUtil.toByteArray(p, schema, buf());

    Payment p2 = new Payment();
    ProtobufIOUtil.mergeFrom(data, p2, schema);
    /*
     * System.err.println(p2.getId()); System.err.println(p2.getBd()); System.err.println(p2.getBi());
     * System.err.println(p2.getBdList()); System.err.println(p2.getBiList());
     */

    assertEquals(p, p2);

    List<Payment> list = new ArrayList<Payment>();
    list.add(p);
    list.add(p2);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ProtobufIOUtil.writeListTo(out, list, schema, buf());
    byte[] listData = out.toByteArray();

    ByteArrayInputStream in = new ByteArrayInputStream(listData);
    List<Payment> parsedList = ProtobufIOUtil.parseListFrom(in, schema);

    assertEquals(list, parsedList);
}
 
Example 6
Source File: RuntimeSchemaBenchmark.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Benchmark
public SparseInt10 runtime_sparse_deserialize_10_int_field() throws Exception
{
    SparseInt10 int10 = new SparseInt10();
    ProtobufIOUtil.mergeFrom(data_10_int, int10, sparseInt10RuntimeSchema);
    return int10;
}
 
Example 7
Source File: RuntimeSchemaBenchmark.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Benchmark
public GeneratedInt10 generated_deserialize_10_int_field() throws Exception
{
    GeneratedInt10 int10 = new GeneratedInt10();
    ProtobufIOUtil.mergeFrom(data_10_int, int10, GeneratedInt10.getSchema());
    return int10;
}
 
Example 8
Source File: ConnectionReaderImpl.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T extends AbstractConnectionConf> T getConnectionConf(String typeName, ByteString bytesS) {
  Schema<T> schema = (Schema<T>) schemaByName.get(typeName);
  if(schema == null) {
    throw new MissingSourceTypeException(typeName, String.format("Unable to find handler for source of type [%s].", typeName));
  }

  T conf = schema.newMessage();
  byte[] bytes = bytesS.toByteArray();
  ProtobufIOUtil.mergeFrom(bytes, conf, schema);
  return conf;
}
 
Example 9
Source File: CatalogProtocol.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
  try {
    SourceConfig config = new SourceConfig();
    ProtobufIOUtil.mergeFrom(wrapper.getBytes().toByteArray(), config, SourceConfig.getSchema());
    if(update) {
      listener.sourceUpdate(config);
    } else {
      listener.sourceDelete(config);
    }
  } finally {
    sender.send(OK);
  }
}
 
Example 10
Source File: RuntimeSchemaBenchmark.java    From protostuff with Apache License 2.0 5 votes vote down vote up
@Benchmark
public Int1 runtime_deserialize_1_int_field() throws Exception
{
    Int1 int1 = new Int1();
    ProtobufIOUtil.mergeFrom(data_1_int, int1, int1RuntimeSchema);
    return int1;
}
 
Example 11
Source File: ProtoMarshaller.java    From faster-framework-project with Apache License 2.0 5 votes vote down vote up
@Override
public Object parse(InputStream stream) {
    if (stream == null) {
        return new Object();
    }
    Object obj = responseSchema.newMessage();
    try {
        ProtobufIOUtil.mergeFrom(StreamUtils.copyToByteArray(stream), obj, responseSchema);
        return obj;
    } catch (IOException ignored) {
    }
    return new Object();
}
 
Example 12
Source File: QueryUtil.java    From datawave with Apache License 2.0 5 votes vote down vote up
public static <T extends Query> T deserialize(String queryImplClassName, Text columnVisibility, Value value) throws InvalidProtocolBufferException,
                ClassNotFoundException {
    @SuppressWarnings("unchecked")
    Class<T> queryClass = (Class<T>) Class.forName(queryImplClassName);
    byte[] b = value.get();
    Schema<T> schema = RuntimeSchema.getSchema(queryClass);
    T queryImpl = schema.newMessage();
    ProtobufIOUtil.mergeFrom(b, queryImpl, schema);
    queryImpl.setColumnVisibility(columnVisibility.toString());
    return queryImpl;
}
 
Example 13
Source File: ProtobufRuntimeCollectionSchemaTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> void mergeFrom(InputStream in, T message, Schema<T> schema)
        throws IOException
{
    ProtobufIOUtil.mergeFrom(in, message, schema);
}
 
Example 14
Source File: ProtobufNullArrayElementInObjectArrayTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> void mergeFrom(InputStream in, T message, Schema<T> schema)
        throws IOException
{
    ProtobufIOUtil.mergeFrom(in, message, schema);
}
 
Example 15
Source File: ProtobufRuntimeMapTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> void mergeFrom(byte[] data, int offset, int length,
        T message, Schema<T> schema) throws IOException
{
    ProtobufIOUtil.mergeFrom(data, offset, length, message, schema);
}
 
Example 16
Source File: ProtobufRuntimeCollectionSchemaTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> void mergeFrom(byte[] data, int offset, int length,
        T message, Schema<T> schema) throws IOException
{
    ProtobufIOUtil.mergeFrom(data, offset, length, message, schema);
}
 
Example 17
Source File: BusProtoStuffMessageConverter.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
private static EventType readClassHeader(final byte[] typeInformation) {
    final Schema<EventType> schema = RuntimeSchema.getSchema(EventType.class);
    final EventType deserializedType = schema.newMessage();
    ProtobufIOUtil.mergeFrom(typeInformation, deserializedType, schema);
    return deserializedType;
}
 
Example 18
Source File: ProtobufRuntimeMapTest.java    From protostuff with Apache License 2.0 4 votes vote down vote up
@Override
protected <T> void mergeFrom(InputStream in, T message, Schema<T> schema)
        throws IOException
{
    ProtobufIOUtil.mergeFrom(in, message, schema);
}
 
Example 19
Source File: JobsProtoUtil.java    From dremio-oss with Apache License 2.0 3 votes vote down vote up
/**
 * Generic method to convert Protobuf to Protostuff. Safe to use Protostuff's serializer as it can properly
 * deserialize messages serialized by Protobuf.
 * @param protostuffSchema Protostuff object schema
 * @param protobuf Protobuf object to convert
 * @param <M> Type of Protobuf
 * @param <T> Type of Protostuff
 * @return Converted object as Protostuff
 */
private static <M extends GeneratedMessageV3, T extends Message<T> & Schema<T>>
T toStuff(Schema<T> protostuffSchema, M protobuf) {
  T message = protostuffSchema.newMessage();
  ProtobufIOUtil.mergeFrom(protobuf.toByteArray(), message, protostuffSchema);
  return message;
}
 
Example 20
Source File: NameSpaceContainer.java    From dremio-oss with Apache License 2.0 2 votes vote down vote up
/**
 * Create a NameSpaceContainer from a protostuff ByteString
 *
 * @param bytes a ByteString representation a NameSpaceContainer
 * @return
 */
public static NameSpaceContainer from(ByteString bytes) {
  com.dremio.service.namespace.protostuff.NameSpaceContainer container = com.dremio.service.namespace.protostuff.NameSpaceContainer.getSchema().newMessage();
  ProtobufIOUtil.mergeFrom(bytes.toByteArray(), container, container.getSchema());
  return new NameSpaceContainer(container);
}