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.Serializer;
18  import com.esotericsoftware.kryo.kryo5.io.Input;
19  import com.esotericsoftware.kryo.kryo5.io.Output;
20  
21  /**
22   * V5 serializer for message argument arrays ({@code Object[]}).
23   *
24   * <p>Does not use Kryo's default polymorphic array encoding. Each element is reduced by
25   * {@link SafeObjectTransformer} and written via {@link SafeObjectCodec} (1-byte type tag +
26   * compact payload). String arguments share a stream-scoped string serializer:
27   * {@link LoggingEventKryoVOSerializerV5} passes its {@link MultiEventCompressedStringSerializer},
28   * the {@code int} constructor uses a {@link SingleItemCompressingStringSerializer}.
29   *
30   * <p>Wire format:
31   * <pre>
32   *   varInt(length)
33   *   length × (SafeObjectCodec element)
34   * </pre>
35   *
36   * <p>Null arrays are not handled here; callers should use {@code writeObjectOrNull} /
37   * {@code readObjectOrNull}.
38   *
39   * @see SafeObjectCodec
40   * @see LoggingEventKryoVO#safeObjectArray(Object[])
41   */
42  public class ArgumentArraySerializerV5 extends Serializer<Object[]> {
43  
44      private final Serializer<String> stringSerializer;
45  
46      public ArgumentArraySerializerV5() {
47          this(1024);
48      }
49  
50      /**
51       * @param stringCapacity capacity of the string cache for string arguments
52       */
53      public ArgumentArraySerializerV5(int stringCapacity) {
54          this(new SingleItemCompressingStringSerializer(stringCapacity));
55      }
56  
57      /**
58       * @param stringSerializer shared cache for string arguments (must not be null)
59       */
60      public ArgumentArraySerializerV5(Serializer<String> stringSerializer) {
61          if (stringSerializer == null) {
62              throw new IllegalArgumentException("stringSerializer cannot be null");
63          }
64          this.stringSerializer = stringSerializer;
65          setAcceptsNull(false);
66      }
67  
68      public Serializer<String> getStringSerializer() {
69          return stringSerializer;
70      }
71  
72      @Override
73      public void write(Kryo kryo, Output output, Object[] arguments) {
74          output.writeVarInt(arguments.length, true);
75          for (Object arg : arguments) {
76              SafeObjectCodec.write(kryo, output, stringSerializer, arg);
77          }
78      }
79  
80      @Override
81      public Object[] read(Kryo kryo, Input input, Class<? extends Object[]> type) {
82          int length = input.readVarInt(true);
83          Object[] arguments = new Object[length];
84          for (int i = 0; i < length; i++) {
85              arguments[i] = SafeObjectCodec.read(kryo, input, stringSerializer);
86          }
87          return arguments;
88      }
89  }