1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.kryo;
15
16 import ch.qos.logback.classic.spi.IThrowableProxy;
17 import ch.qos.logback.classic.spi.StackTraceElementProxy;
18 import ch.qos.logback.classic.spi.ThrowableProxyVO;
19 import ch.qos.logback.core.kryo.SerializerBase;
20 import com.esotericsoftware.kryo.kryo5.Kryo;
21 import com.esotericsoftware.kryo.kryo5.Serializer;
22 import com.esotericsoftware.kryo.kryo5.io.Input;
23 import com.esotericsoftware.kryo.kryo5.io.Output;
24 import com.esotericsoftware.kryo.kryo5.minlog.Log;
25
26 public class ThrowableProxyVOSerializerV5 extends Serializer<ThrowableProxyVO> {
27 private final BasicStringSerializer classNameSerializer;
28 private final BasicStringSerializer messageSerializer;
29 private final Serializer<StackTraceElementProxy> stackTraceElementProxySerializerV5;
30
31 public ThrowableProxyVOSerializerV5(int capacity) {
32 this(capacity, new StackTraceElementProxySerializerV5(capacity));
33 }
34
35
36
37
38
39 public ThrowableProxyVOSerializerV5(int capacity, Serializer<StackTraceElementProxy> stackTraceElementProxySerializer) {
40 setAcceptsNull(false);
41 this.classNameSerializer = new BasicStringSerializer(SerializerBase.BiMapType.DIRECT_MAP, capacity);
42
43
44 this.messageSerializer = new BasicStringSerializer(SerializerBase.BiMapType.DIRECT_MAP, capacity);
45
46 this.stackTraceElementProxySerializerV5 = stackTraceElementProxySerializer;
47
48 }
49
50 @Override
51 public void write(Kryo kryo, Output output, ThrowableProxyVO throwableProxyVO) {
52 kryo.writeObject(output, throwableProxyVO.getClassName(), classNameSerializer);
53 kryo.writeObjectOrNull(output, throwableProxyVO.getOverridingMessage(), messageSerializer);
54 kryo.writeObjectOrNull(output, throwableProxyVO.getMessage(), messageSerializer);
55 output.writeVarInt(throwableProxyVO.getCommonFrames(), true);
56
57 writeStackTrace(kryo, output, throwableProxyVO.getStackTraceElementProxyArray());
58
59 IThrowableProxy cause = throwableProxyVO.getCause();
60 if (cause == null) {
61 output.writeBoolean(false);
62 } else {
63 output.writeBoolean(true);
64 kryo.writeObject(output, asThrowableProxyVO(cause), this);
65 }
66
67 IThrowableProxy[] suppressed = throwableProxyVO.getSuppressed();
68 if (suppressed == null) {
69 output.writeVarInt(-1, true);
70 } else {
71 output.writeVarInt(suppressed.length, true);
72 for (IThrowableProxy s : suppressed) {
73 kryo.writeObject(output, asThrowableProxyVO(s), this);
74 }
75 }
76
77 output.writeBoolean(throwableProxyVO.isCyclic());
78
79 }
80
81 @Override
82 public ThrowableProxyVO read(Kryo kryo, Input input, Class<? extends ThrowableProxyVO> type) {
83 Log.trace("readNewEntry for ThrowableProxyVO");
84
85 String className = kryo.readObject(input, String.class, classNameSerializer);
86 String overridingMessage = kryo.readObjectOrNull(input, String.class, messageSerializer);
87 String message = kryo.readObjectOrNull(input, String.class, messageSerializer);
88 int commonFrames = input.readVarInt(true);
89
90 StackTraceElementProxy[] stackTraceElementProxyArray = readStackTrace(kryo, input);
91
92 ThrowableProxyVO cause = null;
93 if (input.readBoolean()) {
94 cause = kryo.readObject(input, ThrowableProxyVO.class, this);
95 }
96
97 int suppressedLength = input.readVarInt(true);
98 IThrowableProxy[] suppressed = null;
99 if (suppressedLength >= 0) {
100 suppressed = new IThrowableProxy[suppressedLength];
101 for (int i = 0; i < suppressedLength; i++) {
102 suppressed[i] = kryo.readObject(input, ThrowableProxyVO.class, this);
103 }
104 }
105
106 boolean cyclic = input.readBoolean();
107
108
109 return ThrowableProxyVO.builder()
110 .setClassName(className)
111 .setOverridingMessage(overridingMessage)
112 .setMessage(message)
113 .setCommonFramesCount(commonFrames)
114 .setStackTraceElementProxyArray(stackTraceElementProxyArray)
115 .setCause(cause)
116 .setSuppressed(suppressed)
117 .setCyclic(cyclic)
118 .build();
119
120 }
121
122 private void writeStackTrace(Kryo kryo, Output output, StackTraceElementProxy[] steps) {
123 if (steps == null) {
124 output.writeVarInt(-1, true);
125 return;
126 }
127 output.writeVarInt(steps.length, true);
128 for (StackTraceElementProxy step : steps) {
129 kryo.writeObject(output, step, stackTraceElementProxySerializerV5);
130 }
131 }
132
133 private StackTraceElementProxy[] readStackTrace(Kryo kryo, Input input) {
134 int length = input.readVarInt(true);
135 if (length < 0) {
136 return null;
137 }
138 StackTraceElementProxy[] steps = new StackTraceElementProxy[length];
139 for (int i = 0; i < length; i++) {
140 steps[i] = kryo.readObject(input, StackTraceElementProxy.class, stackTraceElementProxySerializerV5);
141 }
142 return steps;
143 }
144
145 private static ThrowableProxyVO asThrowableProxyVO(IThrowableProxy proxy) {
146 if (proxy instanceof ThrowableProxyVO) {
147 return (ThrowableProxyVO) proxy;
148 }
149 return ThrowableProxyVO.build(proxy);
150 }
151 }