forked from kafbat/kafka-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase64Serde.java
60 lines (51 loc) · 1.5 KB
/
Base64Serde.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package io.kafbat.ui.serdes.builtin;
import io.kafbat.ui.serde.api.DeserializeResult;
import io.kafbat.ui.serde.api.SchemaDescription;
import io.kafbat.ui.serde.api.Serde;
import io.kafbat.ui.serdes.BuiltInSerde;
import java.util.Base64;
import java.util.Map;
import java.util.Optional;
public class Base64Serde implements BuiltInSerde {
public static String name() {
return "Base64";
}
@Override
public Optional<String> getDescription() {
return Optional.empty();
}
@Override
public Optional<SchemaDescription> getSchema(String topic, Serde.Target type) {
return Optional.empty();
}
@Override
public boolean canDeserialize(String topic, Serde.Target type) {
return true;
}
@Override
public boolean canSerialize(String topic, Serde.Target type) {
return true;
}
@Override
public Serde.Serializer serializer(String topic, Serde.Target type) {
var decoder = Base64.getDecoder();
return inputString -> {
inputString = inputString.trim();
// it is actually a hack to provide ability to sent empty array as a key/value
if (inputString.isEmpty()) {
return new byte[] {};
}
return decoder.decode(inputString);
};
}
@Override
public Serde.Deserializer deserializer(String topic, Serde.Target type) {
var encoder = Base64.getEncoder();
return (headers, data) ->
new DeserializeResult(
encoder.encodeToString(data),
DeserializeResult.Type.STRING,
Map.of()
);
}
}