CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/serialization/SerializerFixedBinaryLength.java

134 lines
4.3 KiB
Java
Raw Normal View History

2021-02-02 19:40:37 +01:00
package it.cavallium.dbengine.database.serialization;
2023-03-06 12:19:08 +01:00
import it.cavallium.buffer.Buf;
import it.cavallium.buffer.BufDataInput;
import it.cavallium.buffer.BufDataOutput;
import java.nio.charset.StandardCharsets;
2021-09-22 18:33:28 +02:00
import java.util.Objects;
2021-02-02 19:40:37 +01:00
import org.jetbrains.annotations.NotNull;
2021-02-03 14:37:02 +01:00
@SuppressWarnings("unused")
2021-09-01 00:01:56 +02:00
public interface SerializerFixedBinaryLength<A> extends Serializer<A> {
2021-02-02 19:40:37 +01:00
int getSerializedBinaryLength();
2021-10-19 00:22:05 +02:00
@Override
default int getSerializedSizeHint() {
return getSerializedBinaryLength();
}
static SerializerFixedBinaryLength<Buf> noop(int length) {
2021-02-02 19:40:37 +01:00
return new SerializerFixedBinaryLength<>() {
@Override
public @NotNull Buf deserialize(@NotNull BufDataInput in) throws SerializationException {
Objects.requireNonNull(in);
if (in.available() < getSerializedBinaryLength()) {
2021-10-19 00:22:05 +02:00
throw new IllegalArgumentException(
"Fixed serializer with " + getSerializedBinaryLength() + " bytes has tried to deserialize an element with "
+ in.available() + " bytes instead");
}
return Buf.wrap(in.readNBytes(getSerializedBinaryLength()));
2021-02-02 19:40:37 +01:00
}
@Override
public void serialize(@NotNull Buf deserialized, BufDataOutput out) throws SerializationException {
if (deserialized.size() != getSerializedBinaryLength()) {
throw new IllegalArgumentException(
"Fixed serializer with " + getSerializedBinaryLength() + " bytes has tried to serialize an element with "
+ deserialized.size() + " bytes instead");
}
out.writeBytes(deserialized);
2021-02-02 19:40:37 +01:00
}
@Override
public int getSerializedBinaryLength() {
return length;
}
};
}
2021-10-19 00:22:05 +02:00
static SerializerFixedBinaryLength<String> utf8(int length) {
2021-02-02 19:40:37 +01:00
return new SerializerFixedBinaryLength<>() {
2021-10-19 00:22:05 +02:00
2021-02-02 19:40:37 +01:00
@Override
public @NotNull String deserialize(@NotNull BufDataInput in) throws SerializationException {
if (in.available() < getSerializedBinaryLength()) {
2021-10-19 00:22:05 +02:00
throw new SerializationException(
"Fixed serializer with " + getSerializedBinaryLength() + " bytes has tried to deserialize an element with "
+ in.available() + " bytes instead");
}
return new String(in.readNBytes(length), StandardCharsets.UTF_8);
2021-02-02 19:40:37 +01:00
}
@Override
public void serialize(@NotNull String deserialized, BufDataOutput out) throws SerializationException {
2021-10-19 00:22:05 +02:00
var bytes = deserialized.getBytes(StandardCharsets.UTF_8);
out.ensureWritable(bytes.length);
out.write(bytes);
2023-05-22 23:34:08 +02:00
if (bytes.length != getSerializedBinaryLength()) {
2021-10-19 00:22:05 +02:00
throw new SerializationException("Fixed serializer with " + getSerializedBinaryLength()
+ " bytes has tried to serialize an element with "
+ bytes.length + " bytes instead");
}
}
@Override
public int getSerializedBinaryLength() {
return length;
}
};
}
static SerializerFixedBinaryLength<Integer> intSerializer() {
return new SerializerFixedBinaryLength<>() {
2021-10-19 00:22:05 +02:00
@Override
public @NotNull Integer deserialize(@NotNull BufDataInput in) throws SerializationException {
Objects.requireNonNull(in);
if (in.available() < getSerializedBinaryLength()) {
2021-10-19 00:22:05 +02:00
throw new IllegalArgumentException(
"Fixed serializer with " + getSerializedBinaryLength() + " bytes has tried to deserialize an element with "
+ in.available() + " bytes instead");
}
return in.readInt();
}
@Override
public void serialize(@NotNull Integer deserialized, BufDataOutput out) throws SerializationException {
out.writeInt(deserialized);
2021-02-02 19:40:37 +01:00
}
@Override
public int getSerializedBinaryLength() {
return Integer.BYTES;
}
};
}
static SerializerFixedBinaryLength<Long> longSerializer() {
2021-02-02 19:40:37 +01:00
return new SerializerFixedBinaryLength<>() {
2021-10-19 00:22:05 +02:00
2021-02-02 19:40:37 +01:00
@Override
public @NotNull Long deserialize(@NotNull BufDataInput in) throws SerializationException {
Objects.requireNonNull(in);
if (in.available() < getSerializedBinaryLength()) {
2021-10-19 00:22:05 +02:00
throw new IllegalArgumentException(
"Fixed serializer with " + getSerializedBinaryLength() + " bytes has tried to deserialize an element with "
+ in.available() + " bytes instead");
}
return in.readLong();
2021-02-02 19:40:37 +01:00
}
@Override
public void serialize(@NotNull Long deserialized, BufDataOutput out) throws SerializationException {
out.writeLong(deserialized);
2021-02-02 19:40:37 +01:00
}
@Override
public int getSerializedBinaryLength() {
return Long.BYTES;
}
};
}
}