12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package com.sdyc.ndmp.protobuf.serializer;
- import com.esotericsoftware.kryo.*;
- import com.esotericsoftware.kryo.io.Input;
- import com.esotericsoftware.kryo.io.Output;
- import com.google.protobuf.GeneratedMessage;
- /**
- * <pre>
- *
- * Created by IntelliJ IDEA.
- * User: zhenqin
- * Date: 14/12/10
- * Time: 09:55
- * To change this template use File | Settings | File Templates.
- *
- * </pre>
- *
- * @author zhenqin
- */
- public class ProtobufKryoSerializer extends com.esotericsoftware.kryo.Serializer<GeneratedMessage> {
- protected final ProtobufSerializer protobufSerializer;
- public ProtobufKryoSerializer() {
- protobufSerializer = new ProtobufSerializer();
- }
- @Override
- public void write(Kryo kryo, Output output, GeneratedMessage object) {
- byte[] bytes = protobufSerializer.serialize(object);
- output.writeInt(bytes.length, false);
- output.write(bytes);
- }
- @Override
- public GeneratedMessage read(Kryo kryo, Input input, Class type) {
- int i = input.readInt(false);
- byte[] bytes = new byte[i];
- input.read(bytes);
- return protobufSerializer.deserialize(bytes);
- }
- }
|