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

alibaba / jetcache / #405

16 Apr 2024 05:58AM UTC coverage: 0.0% (-88.9%) from 88.866%
#405

push

areyouok
add encoding to fix coverage report

0 of 5353 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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

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

15
    public static final Kryo5ValueEncoder INSTANCE = new Kryo5ValueEncoder(true);
×
16

17
    private static final int INIT_BUFFER_SIZE = 2048;
18

19
    //Default size = 32K
20
    static ObjectPool<Kryo5Cache> kryoCacheObjectPool = new ObjectPool<>(16, new ObjectPool.ObjectFactory<Kryo5Cache>() {
×
21
        @Override
22
        public Kryo5Cache create() {
23
            return new Kryo5Cache();
×
24
        }
25

26
        @Override
27
        public void reset(Kryo5Cache obj) {
28
            obj.getKryo().reset();
×
29
            obj.getOutput().reset();
×
30
        }
×
31
    });
32

33
    public static class Kryo5Cache {
34
        final Output output;
35
        final Kryo kryo;
36
        public Kryo5Cache(){
×
37
            kryo = new Kryo();
×
38
            kryo.setDefaultSerializer(CompatibleFieldSerializer.class);
×
39
            kryo.setRegistrationRequired(false);
×
40
            output = new Output(INIT_BUFFER_SIZE, -1);
×
41
        }
×
42

43
        public Output getOutput(){
44
            return output;
×
45
        }
46

47
        public Kryo getKryo(){
48
            return kryo;
×
49
        }
50

51
    }
52

53
    public Kryo5ValueEncoder(boolean useIdentityNumber) {
54
        super(useIdentityNumber);
×
55
    }
×
56

57
    @Override
58
    public byte[] apply(Object value) {
59
        Kryo5Cache kryoCache = null;
×
60
        try {
61
            kryoCache = kryoCacheObjectPool.borrowObject();
×
62
            if (useIdentityNumber) {
×
63
                writeInt(kryoCache.getOutput(), SerialPolicy.IDENTITY_NUMBER_KRYO5);
×
64
            }
65
            kryoCache.getKryo().writeClassAndObject(kryoCache.getOutput(), value);
×
66
            return kryoCache.getOutput().toBytes();
×
67
        } catch (Exception e) {
×
68
            StringBuilder sb = new StringBuilder("Kryo Encode error. ");
×
69
            sb.append("msg=").append(e.getMessage());
×
70
            throw new CacheEncodeException(sb.toString(), e);
×
71
        } finally {
72
            if (kryoCache != null) {
×
73
                kryoCacheObjectPool.returnObject(kryoCache);
×
74
            }
75
        }
76
    }
77

78
    private void writeInt(Output output, int value) {
79
        // kryo5 change writeInt to little endian, so we write int manually
80
        output.writeByte(value >>> 24);
×
81
        output.writeByte(value >>> 16);
×
82
        output.writeByte(value >>> 8);
×
83
        output.writeByte(value);
×
84
    }
×
85

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

© 2025 Coveralls, Inc