1
2
3
4
5
6
7
8
9
10
11
12
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 import com.esotericsoftware.kryo.kryo5.minlog.Log;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.util.zip.GZIPInputStream;
26 import java.util.zip.GZIPOutputStream;
27
28 public class SingleItemCompressingStringSerializer extends SerializerBase<String> {
29
30 static int SIZE_THRESHOLD_FOR_COMPRESSION = 256;
31 static byte UNMODIFIED = 'U';
32 static byte COMPRESSED = 'C';
33
34 int totalGain = 0;
35 private final ReusableUtf8 utf8 = new ReusableUtf8();
36
37 public SingleItemCompressingStringSerializer(int capacity) {
38 super(capacity);
39 }
40
41 public SingleItemCompressingStringSerializer(SerializerBase.BiMapType biMapType, int capacity) {
42 super(biMapType, capacity);
43 }
44
45 @Override
46 protected void writeNewEntry(Kryo kryo, Output output, String value) {
47 Log.trace("writeNewEntry for String: " + value);
48
49
50 if (value == null) {
51 throw new IllegalArgumentException("Null values are not permitted");
52 }
53
54 int length = value.length();
55
56 if (length < SIZE_THRESHOLD_FOR_COMPRESSION) {
57 outputAsString(output, value);
58 } else {
59 int utf8Len = utf8.encode(value);
60 byte[] compressedBytes = gzipCompress(utf8.buffer(), 0, utf8Len);
61 if (compressedBytes.length >= utf8Len) {
62
63 outputAsString(output, value);
64 Log.trace("String compression not effective, writing unmodified string: " + value);
65 } else {
66 totalGain += utf8Len - compressedBytes.length;
67 Log.debug("Writing compressed string: "+compressedBytes+" instead of "+utf8Len+" totalGain="+totalGain+" bytes");
68 output.writeByte(COMPRESSED);
69 output.writeInt(compressedBytes.length);
70 output.writeBytes(compressedBytes);
71 }
72 }
73
74 }
75
76 private static void outputAsString(Output output, String value) {
77 output.writeByte(UNMODIFIED);
78 output.writeString(value);
79 }
80
81 @Override
82 protected String readNewEntry(Kryo kryo, Input input) {
83
84 byte entryType = input.readByte();
85
86 if (entryType == UNMODIFIED) {
87 String value = input.readString();
88 Log.trace("readNewEntry for String: " + value);
89 return value;
90 } else {
91 int compressedLength = input.readInt();
92 byte[] compressedBytes = input.readBytes(compressedLength);
93 byte[] decompressedBytes = decompressGzip(compressedBytes);
94 String value = utf8.decode(decompressedBytes, 0, decompressedBytes.length);
95 Log.trace("readNewEntry for Compressed String: " + value);
96 return value;
97
98 }
99
100
101 }
102
103 private byte[] decompressGzip(byte[] compressedBytes) {
104
105 ByteArrayInputStream bais = new ByteArrayInputStream(compressedBytes);
106 try (GZIPInputStream gzip = new GZIPInputStream(bais)) {
107 byte[] result = gzip.readAllBytes();
108 return result;
109 } catch (IOException e) {
110 throw new RuntimeException(e);
111 }
112 }
113
114 public static byte[] gzipCompress(byte[] data) {
115 return gzipCompress(data, 0, data.length);
116 }
117
118 public static byte[] gzipCompress(byte[] data, int off, int len) {
119 ByteArrayOutputStream baos = new ByteArrayOutputStream();
120 try (GZIPOutputStream gzip = new GZIPOutputStream(baos)) {
121 gzip.write(data, off, len);
122 } catch (IOException e) {
123 throw new RuntimeException(e);
124 }
125 return baos.toByteArray();
126 }
127 }