001/* 002 * Logback: the reliable, generic, fast and flexible logging framework. 003 * Copyright (C) 1999-2026, QOS.ch. All rights reserved. 004 * 005 * This program and the accompanying materials are dual-licensed under 006 * either the terms of the Eclipse Public License v2.0 as published by 007 * the Eclipse Foundation 008 * 009 * or (per the licensee's choosing) 010 * 011 * under the terms of the GNU Lesser General Public License version 2.1 012 * as published by the Free Software Foundation. 013 */ 014package ch.qos.logback.classic.kryo; 015 016import com.esotericsoftware.kryo.kryo5.Kryo; 017import com.esotericsoftware.kryo.kryo5.Serializer; 018import com.esotericsoftware.kryo.kryo5.io.Input; 019import com.esotericsoftware.kryo.kryo5.io.Output; 020 021/** 022 * V5 serializer for message argument arrays ({@code Object[]}). 023 * 024 * <p>Does not use Kryo's default polymorphic array encoding. Each element is reduced by 025 * {@link SafeObjectTransformer} and written via {@link SafeObjectCodec} (1-byte type tag + 026 * compact payload). String arguments share a stream-scoped string serializer: 027 * {@link LoggingEventKryoVOSerializerV5} passes its {@link MultiEventCompressedStringSerializer}, 028 * the {@code int} constructor uses a {@link SingleItemCompressingStringSerializer}. 029 * 030 * <p>Wire format: 031 * <pre> 032 * varInt(length) 033 * length × (SafeObjectCodec element) 034 * </pre> 035 * 036 * <p>Null arrays are not handled here; callers should use {@code writeObjectOrNull} / 037 * {@code readObjectOrNull}. 038 * 039 * @see SafeObjectCodec 040 * @see LoggingEventKryoVO#safeObjectArray(Object[]) 041 */ 042public class ArgumentArraySerializerV5 extends Serializer<Object[]> { 043 044 private final Serializer<String> stringSerializer; 045 046 public ArgumentArraySerializerV5() { 047 this(1024); 048 } 049 050 /** 051 * @param stringCapacity capacity of the string cache for string arguments 052 */ 053 public ArgumentArraySerializerV5(int stringCapacity) { 054 this(new SingleItemCompressingStringSerializer(stringCapacity)); 055 } 056 057 /** 058 * @param stringSerializer shared cache for string arguments (must not be null) 059 */ 060 public ArgumentArraySerializerV5(Serializer<String> stringSerializer) { 061 if (stringSerializer == null) { 062 throw new IllegalArgumentException("stringSerializer cannot be null"); 063 } 064 this.stringSerializer = stringSerializer; 065 setAcceptsNull(false); 066 } 067 068 public Serializer<String> getStringSerializer() { 069 return stringSerializer; 070 } 071 072 @Override 073 public void write(Kryo kryo, Output output, Object[] arguments) { 074 output.writeVarInt(arguments.length, true); 075 for (Object arg : arguments) { 076 SafeObjectCodec.write(kryo, output, stringSerializer, arg); 077 } 078 } 079 080 @Override 081 public Object[] read(Kryo kryo, Input input, Class<? extends Object[]> type) { 082 int length = input.readVarInt(true); 083 Object[] arguments = new Object[length]; 084 for (int i = 0; i < length; i++) { 085 arguments[i] = SafeObjectCodec.read(kryo, input, stringSerializer); 086 } 087 return arguments; 088 } 089}