From 012235d05d8dc7c2decdc7229d93033b0399ecbb Mon Sep 17 00:00:00 2001
From: xiejun <xiejun@vci-tech.com>
Date: 星期日, 10 十一月 2024 15:49:53 +0800
Subject: [PATCH] 集成获取mdm分发通用数据格式接口集成
---
Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ProtostuffUtil.java | 99 +++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 99 insertions(+), 0 deletions(-)
diff --git a/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ProtostuffUtil.java b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ProtostuffUtil.java
new file mode 100644
index 0000000..79fd5e5
--- /dev/null
+++ b/Source/BladeX-Tool/blade-core-tool/src/main/java/org/springblade/core/tool/utils/ProtostuffUtil.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2018-2028, DreamLu All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * Neither the name of the dreamlu.net developer nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ * Author: DreamLu 鍗㈡槬姊� (596392912@qq.com)
+ */
+package org.springblade.core.tool.utils;
+
+import io.protostuff.LinkedBuffer;
+import io.protostuff.ProtostuffIOUtil;
+import io.protostuff.Schema;
+import io.protostuff.runtime.RuntimeSchema;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Protostuff 宸ュ叿绫�
+ *
+ * @author L.cm
+ */
+public class ProtostuffUtil {
+
+ /**
+ * 閬垮厤姣忔搴忓垪鍖栭兘閲嶆柊鐢宠Buffer绌洪棿
+ */
+ private static final LinkedBuffer BUFFER = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
+ /**
+ * 缂撳瓨Schema
+ */
+ private static final Map<Class<?>, Schema<?>> SCHEMA_CACHE = new ConcurrentHashMap<>();
+
+ /**
+ * 搴忓垪鍖栨柟娉曪紝鎶婃寚瀹氬璞″簭鍒楀寲鎴愬瓧鑺傛暟缁�
+ *
+ * @param obj obj
+ * @param <T> T
+ * @return byte[]
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> byte[] serialize(T obj) {
+ Class<T> clazz = (Class<T>) obj.getClass();
+ Schema<T> schema = getSchema(clazz);
+ byte[] data;
+ try {
+ data = ProtostuffIOUtil.toByteArray(obj, schema, BUFFER);
+ } finally {
+ BUFFER.clear();
+ }
+ return data;
+ }
+
+ /**
+ * 鍙嶅簭鍒楀寲鏂规硶锛屽皢瀛楄妭鏁扮粍鍙嶅簭鍒楀寲鎴愭寚瀹欳lass绫诲瀷
+ *
+ * @param data data
+ * @param clazz clazz
+ * @param <T> T
+ * @return T
+ */
+ public static <T> T deserialize(byte[] data, Class<T> clazz) {
+ Schema<T> schema = getSchema(clazz);
+ T obj = schema.newMessage();
+ ProtostuffIOUtil.mergeFrom(data, obj, schema);
+ return obj;
+ }
+
+ /**
+ * 鑾峰彇Schema
+ * @param clazz clazz
+ * @param <T> T
+ * @return T
+ */
+ @SuppressWarnings("unchecked")
+ private static <T> Schema<T> getSchema(Class<T> clazz) {
+ Schema<T> schema = (Schema<T>) SCHEMA_CACHE.get(clazz);
+ if (Objects.isNull(schema)) {
+ //杩欎釜schema閫氳繃RuntimeSchema杩涜鎳掑垱寤哄苟缂撳瓨
+ //鎵�浠ュ彲浠ヤ竴鐩磋皟鐢≧untimeSchema.getSchema(),杩欎釜鏂规硶鏄嚎绋嬪畨鍏ㄧ殑
+ schema = RuntimeSchema.getSchema(clazz);
+ if (Objects.nonNull(schema)) {
+ SCHEMA_CACHE.put(clazz, schema);
+ }
+ }
+ return schema;
+ }
+
+}
--
Gitblit v1.9.3