yangys
2024-03-27 e48aa2ac8dea1be5db11c63edf0b912c4ad5ce65
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.qianwen.core.redis.serializer;
 
import io.protostuff.LinkedBuffer;
import io.protostuff.ProtostuffIOUtil;
import io.protostuff.Schema;
import io.protostuff.runtime.RuntimeSchema;
import com.qianwen.core.tool.utils.ObjectUtil;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
 
/* loaded from: blade-starter-redis-9.3.0.0-SNAPSHOT.jar:org/springblade/core/redis/serializer/ProtoStuffSerializer.class */
public class ProtoStuffSerializer implements RedisSerializer<Object> {
    private final Schema<BytesWrapper> schema = RuntimeSchema.getSchema(BytesWrapper.class);
 
    public byte[] serialize(Object object) throws SerializationException {
        if (object == null) {
            return null;
        }
        LinkedBuffer buffer = LinkedBuffer.allocate(512);
        try {
            byte[] byteArray = ProtostuffIOUtil.toByteArray(new BytesWrapper(object), this.schema, buffer);
            buffer.clear();
            return byteArray;
        } catch (Throwable th) {
            buffer.clear();
            throw th;
        }
    }
 
    public Object deserialize(byte[] bytes) throws SerializationException {
        if (ObjectUtil.isEmpty(bytes)) {
            return null;
        }
        BytesWrapper<Object> wrapper = new BytesWrapper<>();
        ProtostuffIOUtil.mergeFrom(bytes, wrapper, this.schema);
        return wrapper.getValue();
    }
}