1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.kryo;
15
16 import com.esotericsoftware.kryo.kryo5.Kryo;
17 import com.esotericsoftware.kryo.kryo5.KryoException;
18 import com.esotericsoftware.kryo.kryo5.Serializer;
19 import com.esotericsoftware.kryo.kryo5.io.Input;
20 import com.esotericsoftware.kryo.kryo5.io.Output;
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 public final class SafeObjectCodec {
42
43 public static final byte TAG_NULL = 0;
44 public static final byte TAG_STRING = 1;
45 public static final byte TAG_INTEGER = 2;
46 public static final byte TAG_LONG = 3;
47 public static final byte TAG_SHORT = 4;
48 public static final byte TAG_FLOAT = 5;
49 public static final byte TAG_DOUBLE = 6;
50
51 private SafeObjectCodec() {
52 }
53
54
55
56
57
58
59 public static void write(Kryo kryo, Output output, Serializer<String> stringSerializer, Object rawValue) {
60 Object value = SafeObjectTransformer.asAllowedNonNullObject(rawValue);
61
62 if (value == SafeObjectTransformer.NULL_PROXY) {
63 output.writeByte(TAG_NULL);
64 return;
65 }
66 if (value instanceof String) {
67 output.writeByte(TAG_STRING);
68 kryo.writeObject(output, (String) value, stringSerializer);
69 return;
70 }
71 if (value instanceof Integer) {
72 output.writeByte(TAG_INTEGER);
73 output.writeVarInt((Integer) value, false);
74 return;
75 }
76 if (value instanceof Long) {
77 output.writeByte(TAG_LONG);
78 output.writeVarLong((Long) value, false);
79 return;
80 }
81 if (value instanceof Short) {
82 output.writeByte(TAG_SHORT);
83 output.writeShort((Short) value);
84 return;
85 }
86 if (value instanceof Float) {
87 output.writeByte(TAG_FLOAT);
88 output.writeFloat((Float) value);
89 return;
90 }
91 if (value instanceof Double) {
92 output.writeByte(TAG_DOUBLE);
93 output.writeDouble((Double) value);
94 return;
95 }
96 throw new KryoException("Unexpected value type after SafeObjectTransformer: "
97 + value.getClass().getName());
98 }
99
100
101
102
103 public static Object read(Kryo kryo, Input input, Serializer<String> stringSerializer) {
104 byte tag = input.readByte();
105 switch (tag) {
106 case TAG_NULL:
107 return null;
108 case TAG_STRING:
109 return kryo.readObject(input, String.class, stringSerializer);
110 case TAG_INTEGER:
111 return input.readVarInt(false);
112 case TAG_LONG:
113 return input.readVarLong(false);
114 case TAG_SHORT:
115 return input.readShort();
116 case TAG_FLOAT:
117 return input.readFloat();
118 case TAG_DOUBLE:
119 return input.readDouble();
120 default:
121 throw new KryoException("Unknown SafeObjectCodec value tag: " + tag);
122 }
123 }
124 }