Package org.infinispan.client.hotrod.marshall

Source Code of org.infinispan.client.hotrod.marshall.ProtoStreamMarshaller

package org.infinispan.client.hotrod.marshall;

import org.infinispan.commons.io.ByteBuffer;
import org.infinispan.commons.marshall.AbstractMarshaller;
import org.infinispan.protostream.ProtobufUtil;
import org.infinispan.protostream.SerializationContext;

import java.io.IOException;

/**
* @author anistor@redhat.com
* @since 6.0
*/
public class ProtoStreamMarshaller extends AbstractMarshaller {

   //todo [anistor] this static field is temporary. we need a way to register the protobuf message marshallers when we configure hotrod client
   private static final SerializationContext serializationContext = ProtobufUtil.newSerializationContext();

   public static SerializationContext getSerializationContext() {
      return serializationContext;
   }

   public ProtoStreamMarshaller() {
   }

   @Override
   public Object objectFromByteBuffer(byte[] buf, int offset, int length) throws IOException, ClassNotFoundException {
      return ProtobufUtil.fromWrappedByteArray(serializationContext, buf, offset, length);
   }

   @Override
   public boolean isMarshallable(Object o) {
      return serializationContext.canMarshall(o.getClass());
   }

   @Override
   protected ByteBuffer objectToBuffer(Object o, int estimatedSize) throws IOException, InterruptedException {
      byte[] bytes = ProtobufUtil.toWrappedByteArray(serializationContext, o);
      return new ByteBuffer(bytes, 0, bytes.length);
   }
}
TOP

Related Classes of org.infinispan.client.hotrod.marshall.ProtoStreamMarshaller

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.