All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mvnsearch.spring.boot.nats.serialization.AvroSerialization Maven / Gradle / Ivy

Go to download

Spring Boot starter NATS with publish/subscribe, KeyValue, Object Store, Durable Component, Services framework etc. support.

There is a newer version: 0.2.1
Show newest version
package org.mvnsearch.spring.boot.nats.serialization;

import java.lang.reflect.Method;
import java.nio.ByteBuffer;

public class AvroSerialization implements ObjectSerialization {

  public Object convert(byte[] bytes, Class targetClass) throws Exception {
    Method parseFromMethod = targetClass.getMethod("fromByteBuffer", ByteBuffer.class);
    return parseFromMethod.invoke(null, ByteBuffer.wrap(bytes));
  }

  public byte[] toBytes(Object object) throws Exception {
    Method toByteBufferMethod = object.getClass().getMethod("toByteBuffer");
    ByteBuffer byteBuffer = (ByteBuffer) toByteBufferMethod.invoke(object);
    if (byteBuffer.hasArray()) {
      return byteBuffer.array();
    } else {
      byte[] arr = new byte[byteBuffer.remaining()];
      byteBuffer.get(arr);
      return arr;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy