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

3
import com.esotericsoftware.kryo.kryo5.Kryo;
4
import com.esotericsoftware.kryo.kryo5.io.Output;
5
import com.esotericsoftware.kryo.kryo5.serializers.CompatibleFieldSerializer;
6
import com.esotericsoftware.kryo.kryo5.util.MapReferenceResolver;
7

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

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

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

20
    private static final int INIT_BUFFER_SIZE = 2048;
21

22
    private final ObjectPool<KryoCache> pool;
23

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

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

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

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

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

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

60
    }
61

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

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

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

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