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

alibaba / jetcache / #448

23 May 2026 01:38PM UTC coverage: 89.341% (+0.4%) from 88.921%
#448

push

areyouok
fix: init fail if Kryo not in classpath

0 of 4 new or added lines in 1 file covered. (0.0%)

27 existing lines in 9 files now uncovered.

5029 of 5629 relevant lines covered (89.34%)

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.esotericsoftware.kryo.Kryo;
4
import com.esotericsoftware.kryo.io.Output;
5
import com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer;
6
import com.esotericsoftware.kryo.util.MapReferenceResolver;
7

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

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

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

22
    private static final int INIT_BUFFER_SIZE = 2048;
23

24
    private final ObjectPool<KryoCache> pool;
25

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

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

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

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

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

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

62
    }
63

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

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

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

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