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.KryoException;
018import com.esotericsoftware.kryo.kryo5.Serializer;
019import com.esotericsoftware.kryo.kryo5.io.Input;
020import com.esotericsoftware.kryo.kryo5.io.Output;
021
022/**
023 * Compact wire encoding for objects reduced by {@link SafeObjectTransformer}.
024 *
025 * <p>Applies {@link SafeObjectTransformer#asAllowedNonNullObject(Object)}, then writes a
026 * 1-byte type tag and a type-specific payload (never {@code writeClassAndObject}).
027 *
028 * <pre>
029 *   tag (1 byte) + payload
030 *     0 NULL    — no payload (null or {@link SafeObjectTransformer#NULL_PROXY})
031 *     1 STRING  — {@link SingleItemCompressingStringSerializer}
032 *     2 INTEGER — writeVarInt (zig-zag)
033 *     3 LONG    — writeVarLong (zig-zag)
034 *     4 SHORT   — writeShort
035 *     5 FLOAT   — writeFloat
036 *     6 DOUBLE  — writeDouble
037 * </pre>
038 *
039 * <p>Used for KVP values and message argument arrays.
040 */
041public final class SafeObjectCodec {
042
043    public static final byte TAG_NULL = 0;
044    public static final byte TAG_STRING = 1;
045    public static final byte TAG_INTEGER = 2;
046    public static final byte TAG_LONG = 3;
047    public static final byte TAG_SHORT = 4;
048    public static final byte TAG_FLOAT = 5;
049    public static final byte TAG_DOUBLE = 6;
050
051    private SafeObjectCodec() {
052    }
053
054    /**
055     * Encodes {@code rawValue} after {@link SafeObjectTransformer#asAllowedNonNullObject(Object)}.
056     * {@link SafeObjectTransformer#NULL_PROXY} is written as {@link #TAG_NULL} and restored as
057     * {@code null} on read.
058     */
059    public static void write(Kryo kryo, Output output, Serializer<String> stringSerializer, Object rawValue) {
060        Object value = SafeObjectTransformer.asAllowedNonNullObject(rawValue);
061
062        if (value == SafeObjectTransformer.NULL_PROXY) {
063            output.writeByte(TAG_NULL);
064            return;
065        }
066        if (value instanceof String) {
067            output.writeByte(TAG_STRING);
068            kryo.writeObject(output, (String) value, stringSerializer);
069            return;
070        }
071        if (value instanceof Integer) {
072            output.writeByte(TAG_INTEGER);
073            output.writeVarInt((Integer) value, false);
074            return;
075        }
076        if (value instanceof Long) {
077            output.writeByte(TAG_LONG);
078            output.writeVarLong((Long) value, false);
079            return;
080        }
081        if (value instanceof Short) {
082            output.writeByte(TAG_SHORT);
083            output.writeShort((Short) value);
084            return;
085        }
086        if (value instanceof Float) {
087            output.writeByte(TAG_FLOAT);
088            output.writeFloat((Float) value);
089            return;
090        }
091        if (value instanceof Double) {
092            output.writeByte(TAG_DOUBLE);
093            output.writeDouble((Double) value);
094            return;
095        }
096        throw new KryoException("Unexpected value type after SafeObjectTransformer: "
097                + value.getClass().getName());
098    }
099
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}