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 ch.qos.logback.core.kryo.SerializerBase;
017import com.esotericsoftware.kryo.kryo5.Kryo;
018import com.esotericsoftware.kryo.kryo5.io.Input;
019import com.esotericsoftware.kryo.kryo5.io.Output;
020
021import java.io.ByteArrayOutputStream;
022import java.io.IOException;
023import java.util.zip.DataFormatException;
024import java.util.zip.Deflater;
025import java.util.zip.Inflater;
026
027/**
028 * {@link String} serializer that DEFLATE-compresses <em>long new</em> string payloads as one
029 * raw (nowrap) deflate stream for the life of this serializer instance.
030 *
031 * <p>New values shorter than {@link #SIZE_THRESHOLD_FOR_COMPRESSION} UTF-8 bytes are written
032 * unmodified ({@code 'U'} + Kryo string). Longer new values are appended to the ongoing
033 * deflate stream ({@code 'C'} + length-prefixed {@link Deflater#SYNC_FLUSH} chunk). Cached
034 * hits do not touch the deflater. Short and long new entries may be interleaved; only
035 * {@code 'C'} records advance compressor/decompressor state.
036 *
037 * <p>Uncompressed framing inside the deflate stream is {@code int utf8Length} (big-endian)
038 * + UTF-8 bytes. There is no gzip/zlib wrapper; record boundaries are the outer
039 * {@code chunkLength} plus {@code SYNC_FLUSH}.
040 *
041 * <p>Wire format per new entry:
042 * <pre>
043 *   'U'  Kryo string
044 *   'C'  int chunkLength; byte[chunkLength]  // next SYNC_FLUSH deflate segment
045 * </pre>
046 */
047public class MultiEventCompressedStringSerializer extends SerializerBase<String> {
048
049    static int SIZE_THRESHOLD_FOR_COMPRESSION = 256;
050    static byte UNMODIFIED = 'U';
051    static byte COMPRESSED = 'C';
052
053    private static final int DEFLATE_BUF = 512;
054
055    /** Accumulated (uncompressed UTF-8 size − compressed chunk size) for logging/metrics. */
056    private int totalGain = 0;
057
058    private DrainableByteArrayOutputStream writeCapture;
059    private Deflater deflater;
060    private byte[] deflateBuf;
061    private byte[] writeUncompressed;
062
063    private Inflater inflater;
064    private byte[] inflateBuf;
065    private final ByteArrayOutputStream inflated = new ByteArrayOutputStream(DEFLATE_BUF);
066    private final ReusableUtf8 utf8 = new ReusableUtf8();
067
068    public MultiEventCompressedStringSerializer(int capacity) {
069        super(capacity);
070    }
071
072    public MultiEventCompressedStringSerializer(BiMapType biMapType, int capacity) {
073        super(biMapType, capacity);
074    }
075
076    @Override
077    protected void writeNewEntry(Kryo kryo, Output output, String value) {
078        if (value == null) {
079            throw new IllegalArgumentException("Null values are not permitted");
080        }
081        // UTF-8 is at most 3 bytes/char: skip encoding when the upper bound is under the threshold.
082        if ((long) value.length() * ReusableUtf8.MAX_BYTES_PER_CHAR < SIZE_THRESHOLD_FOR_COMPRESSION) {
083            output.writeByte(UNMODIFIED);
084            output.writeString(value);
085            return;
086        }
087        ensureWriteUncompressed(4 + utf8.maxEncodedLength(value.length()));
088        int utf8Len = utf8.encode(value, writeUncompressed, 4);
089        if (utf8Len < SIZE_THRESHOLD_FOR_COMPRESSION) {
090            output.writeByte(UNMODIFIED);
091            output.writeString(value);
092            return;
093        }
094        output.writeByte(COMPRESSED);
095        writeCompressed(output, utf8Len);
096    }
097
098    @Override
099    protected String readNewEntry(Kryo kryo, Input input) {
100        byte flag = input.readByte();
101        if (flag == UNMODIFIED) {
102            return input.readString();
103        }
104        if (flag != COMPRESSED) {
105            throw new RuntimeException("MultiEventStringSerializer: unknown entry flag " + flag);
106        }
107        try {
108            return readCompressed(input);
109        } catch (DataFormatException | IOException e) {
110            throw new RuntimeException("MultiEventStringSerializer read failed", e);
111        }
112    }
113
114    private void writeCompressed(Output output, int len) {
115        ensureWritePipeline();
116        writeUncompressed[0] = (byte) (len >>> 24);
117        writeUncompressed[1] = (byte) (len >>> 16);
118        writeUncompressed[2] = (byte) (len >>> 8);
119        writeUncompressed[3] = (byte) len;
120        deflater.setInput(writeUncompressed, 0, 4 + len);
121
122        int n;
123        do {
124            n = deflater.deflate(deflateBuf, 0, deflateBuf.length, Deflater.SYNC_FLUSH);
125            if (n > 0) {
126                writeCapture.write(deflateBuf, 0, n);
127            }
128        } while (n == deflateBuf.length);
129
130        totalGain += len - writeCapture.size();
131        writeCapture.drainTo(output);
132    }
133
134    private String readCompressed(Input input) throws DataFormatException, IOException {
135        int chunkLength = input.readInt();
136        byte[] chunk = input.readBytes(chunkLength);
137        ensureReadPipeline();
138        inflater.setInput(chunk);
139        while (!inflater.needsInput()) {
140            int n = inflater.inflate(inflateBuf);
141            if (n <= 0) {
142                break;
143            }
144            inflated.write(inflateBuf, 0, n);
145        }
146        byte[] all = inflated.toByteArray();
147        inflated.reset();
148        if (all.length < 4) {
149            throw new IOException("Truncated deflate length prefix: got " + all.length + " bytes");
150        }
151        int utf8Length = ((all[0] & 0xff) << 24)
152                | ((all[1] & 0xff) << 16)
153                | ((all[2] & 0xff) << 8)
154                | (all[3] & 0xff);
155        if (utf8Length < 0 || all.length < 4 + utf8Length) {
156            throw new IOException("Truncated UTF-8 payload: expected " + utf8Length
157                    + " bytes, got " + Math.max(0, all.length - 4));
158        }
159        if (all.length > 4 + utf8Length) {
160            inflated.write(all, 4 + utf8Length, all.length - 4 - utf8Length);
161        }
162        return utf8.decode(all, 4, utf8Length);
163    }
164
165    private void ensureWriteUncompressed(int need) {
166        if (writeUncompressed == null || writeUncompressed.length < need) {
167            writeUncompressed = new byte[Math.max(need, DEFLATE_BUF)];
168        }
169    }
170
171    private void ensureWritePipeline() {
172        if (deflater != null) {
173            return;
174        }
175        deflater = new Deflater(Deflater.BEST_SPEED, true);
176        deflateBuf = new byte[DEFLATE_BUF];
177        ensureWriteUncompressed(DEFLATE_BUF);
178        writeCapture = new DrainableByteArrayOutputStream();
179    }
180
181    private void ensureReadPipeline() {
182        if (inflater != null) {
183            return;
184        }
185        inflater = new Inflater(true);
186        inflateBuf = new byte[DEFLATE_BUF];
187    }
188
189    /**
190     * Releases the deflater. Not required for normal record streaming with sync-flush.
191     */
192    public void finishWrite() {
193        if (deflater != null) {
194            deflater.end();
195            deflater = null;
196        }
197    }
198
199    public int getTotalGain() {
200        return totalGain;
201    }
202
203    static final class DrainableByteArrayOutputStream extends ByteArrayOutputStream {
204        DrainableByteArrayOutputStream() {
205            super(DEFLATE_BUF);
206        }
207
208        synchronized void drainTo(Output output) {
209            output.writeInt(count);
210            output.writeBytes(buf, 0, count);
211            reset();
212        }
213    }
214}