Improve segmented journal actor metrics
[controller.git] / atomix-storage / src / main / java / io / atomix / utils / serializer / KryoEntryInput.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.Input;
23 import com.esotericsoftware.kryo.serializers.JavaSerializer;
24 import io.atomix.storage.journal.JournalSerdes.EntryInput;
25 import java.io.IOException;
26
27 final class KryoEntryInput implements EntryInput {
28     private final Kryo kryo;
29     private final Input input;
30     private final JavaSerializer javaSerializer;
31
32     KryoEntryInput(final Kryo kryo, final Input input, final JavaSerializer javaSerializer) {
33         this.kryo = requireNonNull(kryo);
34         this.input = requireNonNull(input);
35         this.javaSerializer = requireNonNull(javaSerializer);
36     }
37
38     @Override
39     public byte[] readBytes(final int length) throws IOException {
40         try {
41             return input.readBytes(length);
42         } catch (KryoException e) {
43             throw new IOException(e);
44         }
45     }
46
47     @Override
48     public long readLong() throws IOException {
49         try {
50             return input.readLong(false);
51         } catch (KryoException e) {
52             throw new IOException(e);
53         }
54     }
55
56     @Override
57     public Object readObject() throws IOException {
58         try {
59             return javaSerializer.read(kryo, input, null);
60         } catch (KryoException e) {
61             throw new IOException(e);
62         }
63     }
64
65     @Override
66     public String readString() throws IOException {
67         try {
68             return input.readString();
69         } catch (KryoException e) {
70             throw new IOException(e);
71         }
72     }
73
74     @Override
75     public int readVarInt() throws IOException {
76         try {
77             return input.readVarInt(true);
78         } catch (KryoException e) {
79             throw new IOException(e);
80         }
81     }
82 }