5e9133290683062d8ad151a51833ca431a30f3f7
[controller.git] / atomix-storage / src / main / java / io / atomix / utils / serializer / KryoEntryOutput.java
1 /*
2  * Copyright 2023 PANTHEON.tech, s.r.o.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package io.atomix.utils.serializer;
17
18 import static java.util.Objects.requireNonNull;
19
20 import com.esotericsoftware.kryo.Kryo;
21 import com.esotericsoftware.kryo.KryoException;
22 import com.esotericsoftware.kryo.io.Output;
23 import com.esotericsoftware.kryo.serializers.JavaSerializer;
24 import io.atomix.storage.journal.JournalSerdes.EntryOutput;
25 import java.io.IOException;
26
27 @Deprecated(forRemoval = true, since="9.0.3")
28 final class KryoEntryOutput implements EntryOutput {
29     private final Kryo kryo;
30     private final Output output;
31     private final JavaSerializer javaSerializer;
32
33     KryoEntryOutput(final Kryo kryo, final Output output, final JavaSerializer javaSerializer) {
34         this.kryo = requireNonNull(kryo);
35         this.output = requireNonNull(output);
36         this.javaSerializer = requireNonNull(javaSerializer);
37     }
38
39     @Override
40     public void writeBytes(final byte[] bytes) throws IOException {
41         try {
42             output.writeBytes(bytes);
43         } catch (KryoException e) {
44             throw new IOException(e);
45         }
46     }
47
48     @Override
49     public void writeLong(final long value) throws IOException {
50         try {
51             output.writeLong(value, false);
52         } catch (KryoException e) {
53             throw new IOException(e);
54         }
55     }
56
57     @Override
58     public void writeObject(final Object value) throws IOException {
59         try {
60             javaSerializer.write(kryo, output, value);
61         } catch (KryoException e) {
62             throw new IOException(e);
63         }
64     }
65
66     @Override
67     public void writeString(final String value) throws IOException {
68         try {
69             output.writeString(value);
70         } catch (KryoException e) {
71             throw new IOException(e);
72         }
73     }
74
75     @Override
76     public void writeVarInt(final int value) throws IOException {
77         try {
78             output.writeVarInt(value, true);
79         } catch (KryoException e) {
80             throw new IOException(e);
81         }
82     }
83 }