mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 11:05:18 +08:00
同步获取响应数据
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package rpc;
|
||||
|
||||
import com.jsowell.common.util.bean.SerializationUtil;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package rpc;
|
||||
|
||||
import com.jsowell.common.util.bean.SerializationUtil;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
package rpc;
|
||||
|
||||
import io.protostuff.LinkedBuffer;
|
||||
import io.protostuff.ProtostuffIOUtil;
|
||||
import io.protostuff.Schema;
|
||||
import io.protostuff.runtime.RuntimeSchema;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class SerializationUtil {
|
||||
|
||||
private final static Map<Class<?>, Schema<?>> schemaCache = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 序列化
|
||||
*/
|
||||
public static <T> byte[] serialize(T object){
|
||||
LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
|
||||
|
||||
try {
|
||||
Class<T> cls = (Class<T>) object.getClass();
|
||||
Schema<T> schema = getSchema(cls);
|
||||
|
||||
return ProtostuffIOUtil.toByteArray(object, schema, buffer);
|
||||
} catch (Exception e) {
|
||||
throw e;
|
||||
} finally {
|
||||
buffer.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 反序列化
|
||||
*/
|
||||
public static <T> T deserialize(Class<T> cls, byte[] data) {
|
||||
Schema<T> schema = getSchema(cls);
|
||||
T message = schema.newMessage();
|
||||
ProtostuffIOUtil.mergeFrom(data, message, schema);
|
||||
return message;
|
||||
}
|
||||
|
||||
public static <T> Schema<T> getSchema(Class<T> cls) {
|
||||
Schema<T> schema = (Schema<T>) schemaCache.get(cls);
|
||||
|
||||
if(schema == null) {
|
||||
schema = RuntimeSchema.getSchema(cls);
|
||||
schemaCache.put(cls, schema);
|
||||
}
|
||||
return schema;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user