• 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/AbstractJsonEncoder.java
1
/**
2
 * Created on 2022/07/27.
3
 */
4
package com.alicp.jetcache.support;
5

6
import com.alicp.jetcache.CacheValueHolder;
7
import com.alicp.jetcache.anno.SerialPolicy;
8

9
import java.nio.charset.StandardCharsets;
10

11
/**
12
 * @author huangli
13
 */
14
public abstract class AbstractJsonEncoder extends AbstractValueEncoder {
15
    public AbstractJsonEncoder(boolean useIdentityNumber) {
16
        super(useIdentityNumber);
×
17
    }
×
18

19
    protected abstract byte[] encodeSingleValue(Object value);
20

21
    @Override
22
    public byte[] apply(Object value) {
23
        try {
24
            JsonData[] data = encode(value);
×
25
            int len = len(data);
×
26
            byte[] buffer = useIdentityNumber ? new byte[len + 4] : new byte[len];
×
27
            int index = 0;
×
28
            if (useIdentityNumber) {
×
29
                index = writeInt(buffer, index, SerialPolicy.IDENTITY_NUMBER_FASTJSON2);
×
30
            }
31
            if (data == null) {
×
32
                writeShort(buffer, index, -1);
×
33
            } else {
34
                index = writeShort(buffer, index, data.length);
×
35
                for (JsonData d : data) {
×
36
                    if (d == null) {
×
37
                        index = writeShort(buffer, index, -1);
×
38
                    } else {
39
                        index = writeShort(buffer, index, d.getClassName().length);
×
40
                        index = writeBytes(buffer, index, d.getClassName());
×
41
                        index = writeInt(buffer, index, d.getData().length);
×
42
                        index = writeBytes(buffer, index, d.getData());
×
43
                    }
44
                }
45
            }
46
            return buffer;
×
47
        } catch (Throwable e) {
×
48
            StringBuilder sb = new StringBuilder("Fastjson Encode error. ");
×
49
            sb.append("msg=").append(e.getMessage());
×
50
            throw new CacheEncodeException(sb.toString(), e);
×
51
        }
52
    }
53

54
    private int len(JsonData[] data) {
55
        if (data == null) {
×
56
            return 2;
×
57
        }
58
        int x = 2;
×
59
        for (JsonData d : data) {
×
60
            if (d == null) {
×
61
                x += 2;
×
62
            } else {
63
                x += 2 + d.getClassName().length + 4 + d.getData().length;
×
64
            }
65
        }
66
        return x;
×
67
    }
68

69
    private int writeInt(byte[] buf, int index, int value) {
70
        buf[index] = (byte) (value >> 24 & 0xFF);
×
71
        buf[index + 1] = (byte) (value >> 16 & 0xFF);
×
72
        buf[index + 2] = (byte) (value >> 8 & 0xFF);
×
73
        buf[index + 3] = (byte) (value & 0xFF);
×
74
        return index + 4;
×
75
    }
76

77
    private int writeShort(byte[] buf, int index, int value) {
78
        buf[index] = (byte) (value >> 8 & 0xFF);
×
79
        buf[index + 1] = (byte) (value & 0xFF);
×
80
        return index + 2;
×
81
    }
82

83
    private int writeBytes(byte[] buf, int index, byte[] data) {
84
        System.arraycopy(data, 0, buf, index, data.length);
×
85
        return index + data.length;
×
86
    }
87

88
    private JsonData[] encode(Object value) {
89
        if (value == null) {
×
90
            return null;
×
91
        }
92
        if (value instanceof CacheValueHolder) {
×
93
            CacheValueHolder h = (CacheValueHolder) value;
×
94
            Object bizObject = h.getValue();
×
95
            h.setValue(null);
×
96
            JsonData[] result = new JsonData[2];
×
97
            result[0] = encodeJsonData(h);
×
98
            result[1] = encodeJsonData(bizObject);
×
99
            h.setValue(bizObject);
×
100
            return result;
×
101
        } else if (value instanceof CacheMessage) {
×
102
            CacheMessage cm = (CacheMessage) value;
×
103
            Object[] keys = cm.getKeys();
×
104
            cm.setKeys(null);
×
105
            JsonData[] result = keys == null ? new JsonData[1] : new JsonData[keys.length + 1];
×
106
            result[0] = encodeJsonData(cm);
×
107
            if (keys != null) {
×
108
                for (int i = 0; i < keys.length; i++) {
×
109
                    result[i + 1] = encodeJsonData(keys[i]);
×
110
                }
111
            }
112
            cm.setKeys(keys);
×
113
            return result;
×
114
        } else {
115
            return new JsonData[]{encodeJsonData(value)};
×
116
        }
117
    }
118

119
    private JsonData encodeJsonData(Object value) {
120
        if (value == null) {
×
121
            return null;
×
122
        }
123
        JsonData jsonData = new JsonData();
×
124
        jsonData.setClassName(value.getClass().getName().getBytes(StandardCharsets.UTF_8));
×
125
        jsonData.setData(encodeSingleValue(value));
×
126
        return jsonData;
×
127
    }
128

129
    private static class JsonData {
130
        private byte[] className;
131
        private byte[] data;
132

133
        public byte[] getClassName() {
134
            return className;
×
135
        }
136

137
        public void setClassName(byte[] className) {
138
            this.className = className;
×
139
        }
×
140

141
        public byte[] getData() {
142
            return data;
×
143
        }
144

145
        public void setData(byte[] data) {
146
            this.data = data;
×
147
        }
×
148
    }
149
}
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