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 ch.qos.logback.core.kryo.SerializerBase;
17  import com.esotericsoftware.kryo.kryo5.Kryo;
18  import com.esotericsoftware.kryo.kryo5.io.Input;
19  import com.esotericsoftware.kryo.kryo5.io.Output;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.util.zip.DataFormatException;
24  import java.util.zip.Deflater;
25  import java.util.zip.Inflater;
26  
27  /**
28   * {@link String} serializer that DEFLATE-compresses <em>long new</em> string payloads as one
29   * raw (nowrap) deflate stream for the life of this serializer instance.
30   *
31   * <p>New values shorter than {@link #SIZE_THRESHOLD_FOR_COMPRESSION} UTF-8 bytes are written
32   * unmodified ({@code 'U'} + Kryo string). Longer new values are appended to the ongoing
33   * deflate stream ({@code 'C'} + length-prefixed {@link Deflater#SYNC_FLUSH} chunk). Cached
34   * hits do not touch the deflater. Short and long new entries may be interleaved; only
35   * {@code 'C'} records advance compressor/decompressor state.
36   *
37   * <p>Uncompressed framing inside the deflate stream is {@code int utf8Length} (big-endian)
38   * + UTF-8 bytes. There is no gzip/zlib wrapper; record boundaries are the outer
39   * {@code chunkLength} plus {@code SYNC_FLUSH}.
40   *
41   * <p>Wire format per new entry:
42   * <pre>
43   *   'U'  Kryo string
44   *   'C'  int chunkLength; byte[chunkLength]  // next SYNC_FLUSH deflate segment
45   * </pre>
46   */
47  public class MultiEventCompressedStringSerializer extends SerializerBase<String> {
48  
49      static int SIZE_THRESHOLD_FOR_COMPRESSION = 256;
50      static byte UNMODIFIED = 'U';
51      static byte COMPRESSED = 'C';
52  
53      private static final int DEFLATE_BUF = 512;
54  
55      /** Accumulated (uncompressed UTF-8 size − compressed chunk size) for logging/metrics. */
56      private int totalGain = 0;
57  
58      private DrainableByteArrayOutputStream writeCapture;
59      private Deflater deflater;
60      private byte[] deflateBuf;
61      private byte[] writeUncompressed;
62  
63      private Inflater inflater;
64      private byte[] inflateBuf;
65      private final ByteArrayOutputStream inflated = new ByteArrayOutputStream(DEFLATE_BUF);
66      private final ReusableUtf8 utf8 = new ReusableUtf8();
67  
68      public MultiEventCompressedStringSerializer(int capacity) {
69          super(capacity);
70      }
71  
72      public MultiEventCompressedStringSerializer(BiMapType biMapType, int capacity) {
73          super(biMapType, capacity);
74      }
75  
76      @Override
77      protected void writeNewEntry(Kryo kryo, Output output, String value) {
78          if (value == null) {
79              throw new IllegalArgumentException("Null values are not permitted");
80          }
81          // UTF-8 is at most 3 bytes/char: skip encoding when the upper bound is under the threshold.
82          if ((long) value.length() * ReusableUtf8.MAX_BYTES_PER_CHAR < SIZE_THRESHOLD_FOR_COMPRESSION) {
83              output.writeByte(UNMODIFIED);
84              output.writeString(value);
85              return;
86          }
87          ensureWriteUncompressed(4 + utf8.maxEncodedLength(value.length()));
88          int utf8Len = utf8.encode(value, writeUncompressed, 4);
89          if (utf8Len < SIZE_THRESHOLD_FOR_COMPRESSION) {
90              output.writeByte(UNMODIFIED);
91              output.writeString(value);
92              return;
93          }
94          output.writeByte(COMPRESSED);
95          writeCompressed(output, utf8Len);
96      }
97  
98      @Override
99      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 }