2021-08-31 09:14:46 +02:00
|
|
|
package it.cavallium.dbengine.database.serialization;
|
|
|
|
|
2021-09-17 16:56:28 +02:00
|
|
|
import io.net5.buffer.api.Buffer;
|
2021-08-31 09:14:46 +02:00
|
|
|
import java.io.DataOutput;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
|
|
|
public class BufferDataOutput implements DataOutput {
|
|
|
|
|
|
|
|
private final Buffer buf;
|
|
|
|
|
|
|
|
public BufferDataOutput(Buffer bufferSend) {
|
|
|
|
this.buf = bufferSend;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(int b) {
|
2021-09-03 16:11:14 +02:00
|
|
|
buf.ensureWritable(Integer.BYTES);
|
2021-08-31 09:14:46 +02:00
|
|
|
buf.writeUnsignedByte(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(byte @NotNull [] b) {
|
2021-09-03 16:11:14 +02:00
|
|
|
buf.ensureWritable(Byte.BYTES * b.length);
|
2021-08-31 09:14:46 +02:00
|
|
|
buf.writeBytes(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(byte @NotNull [] b, int off, int len) {
|
2021-09-03 16:11:14 +02:00
|
|
|
buf.ensureWritable(len);
|
2021-08-31 09:14:46 +02:00
|
|
|
buf.writeBytes(b, off, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeBoolean(boolean v) {
|
2021-09-03 16:11:14 +02:00
|
|
|
buf.ensureWritable(Byte.BYTES);
|
2021-08-31 09:14:46 +02:00
|
|
|
buf.writeUnsignedByte(v ? 1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeByte(int v) {
|
2021-09-03 16:11:14 +02:00
|
|
|
buf.ensureWritable(Byte.BYTES);
|
2021-08-31 09:14:46 +02:00
|
|
|
buf.writeByte((byte) v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeShort(int v) {
|
2021-09-03 16:11:14 +02:00
|
|
|
buf.ensureWritable(Short.BYTES);
|
2021-08-31 09:14:46 +02:00
|
|
|
buf.writeShort((short) v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeChar(int v) {
|
2021-09-03 16:11:14 +02:00
|
|
|
buf.ensureWritable(Character.BYTES);
|
2021-08-31 09:14:46 +02:00
|
|
|
buf.writeChar((char) v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeInt(int v) {
|
2021-09-03 16:11:14 +02:00
|
|
|
buf.ensureWritable(Integer.BYTES);
|
2021-08-31 09:14:46 +02:00
|
|
|
buf.writeInt(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeLong(long v) {
|
2021-09-03 16:11:14 +02:00
|
|
|
buf.ensureWritable(Long.BYTES);
|
2021-08-31 09:14:46 +02:00
|
|
|
buf.writeLong(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeFloat(float v) {
|
2021-09-03 16:11:14 +02:00
|
|
|
buf.ensureWritable(Float.BYTES);
|
2021-08-31 09:14:46 +02:00
|
|
|
buf.writeFloat(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeDouble(double v) {
|
2021-09-03 16:11:14 +02:00
|
|
|
buf.ensureWritable(Double.BYTES);
|
2021-08-31 09:14:46 +02:00
|
|
|
buf.writeDouble(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeBytes(@NotNull String s) {
|
2021-09-03 16:11:14 +02:00
|
|
|
var b= s.getBytes(StandardCharsets.UTF_8);
|
|
|
|
buf.ensureWritable(Byte.BYTES * b.length);
|
|
|
|
buf.writeBytes(b);
|
2021-08-31 09:14:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeChars(@NotNull String s) {
|
2021-09-03 16:11:14 +02:00
|
|
|
var chars = s.toCharArray();
|
|
|
|
buf.ensureWritable(Character.BYTES * chars.length);
|
|
|
|
for (char c : chars) {
|
|
|
|
buf.writeChar(c);
|
|
|
|
}
|
2021-08-31 09:14:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void writeUTF(@NotNull String s) {
|
2022-02-09 20:02:23 +01:00
|
|
|
int sizeShortOffset = buf.writerOffset();
|
2022-02-10 00:35:07 +01:00
|
|
|
buf.ensureWritable(Short.BYTES + 1);
|
2022-02-09 20:02:23 +01:00
|
|
|
int stringOffset = sizeShortOffset + Short.BYTES;
|
|
|
|
buf.writerOffset(stringOffset);
|
2022-02-10 00:35:07 +01:00
|
|
|
// todo: replace with writeCharSequence when it will be optimized in netty 5
|
|
|
|
{
|
|
|
|
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
|
|
|
|
buf.ensureWritable(bytes.length);
|
|
|
|
buf.writeBytes(bytes);
|
|
|
|
}
|
2022-02-09 20:02:23 +01:00
|
|
|
int endOffset = buf.writerOffset();
|
|
|
|
int stringSize = endOffset - stringOffset;
|
|
|
|
buf.writerOffset(sizeShortOffset);
|
|
|
|
buf.writeUnsignedShort(stringSize);
|
|
|
|
if (stringSize > (1 << 16) - 1) {
|
|
|
|
buf.writerOffset(sizeShortOffset);
|
|
|
|
throw new IndexOutOfBoundsException("String too large: " + stringSize);
|
|
|
|
} else {
|
|
|
|
buf.writerOffset(endOffset);
|
|
|
|
}
|
2021-08-31 09:14:46 +02:00
|
|
|
}
|
|
|
|
}
|