View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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   * Compact wire encoding for objects reduced by {@link SafeObjectTransformer}.
24   *
25   * <p>Applies {@link SafeObjectTransformer#asAllowedNonNullObject(Object)}, then writes a
26   * 1-byte type tag and a type-specific payload (never {@code writeClassAndObject}).
27   *
28   * <pre>
29   *   tag (1 byte) + payload
30   *     0 NULL    — no payload (null or {@link SafeObjectTransformer#NULL_PROXY})
31   *     1 STRING  — {@link SingleItemCompressingStringSerializer}
32   *     2 INTEGER — writeVarInt (zig-zag)
33   *     3 LONG    — writeVarLong (zig-zag)
34   *     4 SHORT   — writeShort
35   *     5 FLOAT   — writeFloat
36   *     6 DOUBLE  — writeDouble
37   * </pre>
38   *
39   * <p>Used for KVP values and message argument arrays.
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       * Encodes {@code rawValue} after {@link SafeObjectTransformer#asAllowedNonNullObject(Object)}.
56       * {@link SafeObjectTransformer#NULL_PROXY} is written as {@link #TAG_NULL} and restored as
57       * {@code null} on read.
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      * Inverse of {@link #write(Kryo, Output, Serializer, Object)}.
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 }