2 * Copyright 2023 PANTHEON.tech, s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package io.atomix.utils.serializer;
18 import static java.util.Objects.requireNonNull;
20 import com.esotericsoftware.kryo.Kryo;
21 import com.esotericsoftware.kryo.KryoException;
22 import com.esotericsoftware.kryo.Serializer;
23 import com.esotericsoftware.kryo.io.Input;
24 import com.esotericsoftware.kryo.io.Output;
25 import com.esotericsoftware.kryo.serializers.JavaSerializer;
26 import com.google.common.base.MoreObjects;
27 import io.atomix.storage.journal.JournalSerdes.EntrySerdes;
28 import java.io.IOException;
30 final class EntrySerializer<T> extends Serializer<T> {
31 // Note: uses identity to create things in Kryo, hence we want an instance for every serdes we wrap
32 private final JavaSerializer javaSerializer = new JavaSerializer();
33 private final EntrySerdes<T> serdes;
35 EntrySerializer(final EntrySerdes<T> serdes) {
36 this.serdes = requireNonNull(serdes);
40 public T read(final Kryo kryo, final Input input, final Class<T> type) {
42 return serdes.read(new KryoEntryInput(kryo, input, javaSerializer));
43 } catch (IOException e) {
44 throw new KryoException(e);
49 public void write(final Kryo kryo, final Output output, final T object) {
51 serdes.write(new KryoEntryOutput(kryo, output, javaSerializer), object);
52 } catch (IOException e) {
53 throw new KryoException(e);
58 public String toString() {
59 return MoreObjects.toStringHelper(this).addValue(serdes).toString();