• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

alibaba / jetcache / #447

23 May 2026 01:52AM UTC coverage: 88.921% (-0.4%) from 89.31%
#447

push

areyouok
feat: allow customize DecodeFilter for kryo decoder

54 of 59 new or added lines in 8 files covered. (91.53%)

24 existing lines in 8 files now uncovered.

4976 of 5596 relevant lines covered (88.92%)

0.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.47
/jetcache-core/src/main/java/com/alicp/jetcache/support/Kryo5ValueEncoder.java
1
package com.alicp.jetcache.support;
2

3
import com.alicp.jetcache.anno.SerialPolicy;
4
import com.esotericsoftware.kryo.kryo5.Kryo;
5
import com.esotericsoftware.kryo.kryo5.io.Output;
6
import com.esotericsoftware.kryo.kryo5.serializers.CompatibleFieldSerializer;
7
import com.esotericsoftware.kryo.kryo5.util.MapReferenceResolver;
8

9
/**
10
 * Created on 2016/10/4.
11
 *
12
 * @author huangli
13
 */
14
public class Kryo5ValueEncoder extends AbstractValueEncoder {
15

16
    public static final ObjectPool<KryoCache> DEFAULT_POOL = new ObjectPool<>(16,
1✔
17
            new KryoCacheFactory(DecodeFilter.getDefault()));
1✔
18

19
    public static final Kryo5ValueEncoder INSTANCE = new Kryo5ValueEncoder(true, DEFAULT_POOL);
1✔
20

21
    private static final int INIT_BUFFER_SIZE = 2048;
22

23
    private final ObjectPool<KryoCache> pool;
24

25
    public static class KryoCacheFactory implements ObjectPool.ObjectFactory<KryoCache> {
26
        private final DecodeFilter decodeFilter;
27
        public KryoCacheFactory(DecodeFilter decodeFilter) {
1✔
28
            this.decodeFilter = decodeFilter;
1✔
29
        }
1✔
30

31
        @Override
32
        public KryoCache create() {
33
            return new KryoCache(decodeFilter);
1✔
34
        }
35

36
        @Override
37
        public void reset(KryoCache obj) {
38
            obj.getKryo().reset();
1✔
39
            obj.getOutput().reset();
1✔
40
        }
1✔
41
    }
42

43
    public static class KryoCache {
44
        final Output output;
45
        final Kryo kryo;
46
        public KryoCache(DecodeFilter decodeFilter) {
1✔
47
            kryo = new Kryo(new Kryo5ClassResolver(decodeFilter), new MapReferenceResolver());
1✔
48
            kryo.setDefaultSerializer(CompatibleFieldSerializer.class);
1✔
49
            kryo.setRegistrationRequired(false);
1✔
50
            output = new Output(INIT_BUFFER_SIZE, -1);
1✔
51
        }
1✔
52

53
        public Output getOutput(){
54
            return output;
1✔
55
        }
56

57
        public Kryo getKryo(){
58
            return kryo;
1✔
59
        }
60

61
    }
62

63
    public Kryo5ValueEncoder(boolean useIdentityNumber, ObjectPool<KryoCache> pool) {
64
        super(useIdentityNumber);
1✔
65
        this.pool = pool;
1✔
66
    }
1✔
67

68
    @Override
69
    public byte[] apply(Object value) {
70
        KryoCache kryoCache = null;
1✔
71
        try {
72
            kryoCache = pool.borrowObject();
1✔
73
            if (useIdentityNumber) {
1✔
74
                writeInt(kryoCache.getOutput(), SerialPolicy.IDENTITY_NUMBER_KRYO5);
1✔
75
            }
76
            kryoCache.getKryo().writeClassAndObject(kryoCache.getOutput(), value);
1✔
77
            return kryoCache.getOutput().toBytes();
1✔
78
        } catch (Exception e) {
×
79
            StringBuilder sb = new StringBuilder("Kryo Encode error. ");
×
80
            sb.append("msg=").append(e.getMessage());
×
UNCOV
81
            throw new CacheEncodeException(sb.toString(), e);
×
82
        } finally {
83
            if (kryoCache != null) {
1✔
84
                pool.returnObject(kryoCache);
1✔
85
            }
86
        }
87
    }
88

89
    private void writeInt(Output output, int value) {
90
        // kryo5 change writeInt to little endian, so we write int manually
91
        output.writeByte(value >>> 24);
1✔
92
        output.writeByte(value >>> 16);
1✔
93
        output.writeByte(value >>> 8);
1✔
94
        output.writeByte(value);
1✔
95
    }
1✔
96

97
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc