• 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/KryoValueEncoder.java
1
package com.alicp.jetcache.support;
2

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

9
/**
10
 * Created on 2016/10/4.
11
 *
12
 * Since 2.8.0 the com.esotericsoftware:kryo should be 5+, kryo4 is not supported.
13
 *
14
 * @author huangli
15
 */
16
public class KryoValueEncoder extends AbstractValueEncoder {
17

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

21
    public static final KryoValueEncoder INSTANCE = new KryoValueEncoder(true, DEFAULT_POOL);
1✔
22

23
    private static final int INIT_BUFFER_SIZE = 2048;
24

25
    private final ObjectPool<KryoCache> pool;
26

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

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

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

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

55
        public Output getOutput() {
56
            return output;
1✔
57
        }
58

59
        public Kryo getKryo(){
60
            return kryo;
1✔
61
        }
62

63
    }
64

65
    public KryoValueEncoder(boolean useIdentityNumber, ObjectPool<KryoCache> pool) {
66
        super(useIdentityNumber);
1✔
67
        this.pool = pool;
1✔
68
    }
1✔
69

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

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

99
}
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