X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=third-party%2Fatomix%2Fstorage%2Fsrc%2Fmain%2Fjava%2Fio%2Fatomix%2Futils%2Fserializer%2FKryoEntryOutput.java;fp=third-party%2Fatomix%2Fstorage%2Fsrc%2Fmain%2Fjava%2Fio%2Fatomix%2Futils%2Fserializer%2FKryoEntryOutput.java;h=90886dde036fcc4403bde3838060182d1beb617a;hp=0000000000000000000000000000000000000000;hb=014a5d10aa01fcb9b1a7f8bae97e13ca3b7aa698;hpb=0bacb5d7a91f1bc0fcd13763b1cdfcd74ed2e0e6 diff --git a/third-party/atomix/storage/src/main/java/io/atomix/utils/serializer/KryoEntryOutput.java b/third-party/atomix/storage/src/main/java/io/atomix/utils/serializer/KryoEntryOutput.java new file mode 100644 index 0000000000..90886dde03 --- /dev/null +++ b/third-party/atomix/storage/src/main/java/io/atomix/utils/serializer/KryoEntryOutput.java @@ -0,0 +1,82 @@ +/* + * Copyright 2023 PANTHEON.tech, s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.atomix.utils.serializer; + +import static java.util.Objects.requireNonNull; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.KryoException; +import com.esotericsoftware.kryo.io.Output; +import com.esotericsoftware.kryo.serializers.JavaSerializer; +import io.atomix.storage.journal.JournalSerdes.EntryOutput; +import java.io.IOException; + +final class KryoEntryOutput implements EntryOutput { + private final Kryo kryo; + private final Output output; + private final JavaSerializer javaSerializer; + + KryoEntryOutput(final Kryo kryo, final Output output, final JavaSerializer javaSerializer) { + this.kryo = requireNonNull(kryo); + this.output = requireNonNull(output); + this.javaSerializer = requireNonNull(javaSerializer); + } + + @Override + public void writeBytes(final byte[] bytes) throws IOException { + try { + output.writeBytes(bytes); + } catch (KryoException e) { + throw new IOException(e); + } + } + + @Override + public void writeLong(final long value) throws IOException { + try { + output.writeLong(value, false); + } catch (KryoException e) { + throw new IOException(e); + } + } + + @Override + public void writeObject(final Object value) throws IOException { + try { + javaSerializer.write(kryo, output, value); + } catch (KryoException e) { + throw new IOException(e); + } + } + + @Override + public void writeString(final String value) throws IOException { + try { + output.writeString(value); + } catch (KryoException e) { + throw new IOException(e); + } + } + + @Override + public void writeVarInt(final int value) throws IOException { + try { + output.writeVarInt(value, true); + } catch (KryoException e) { + throw new IOException(e); + } + } +}