io.datakernel.codec.binary.BinaryStructuredOutput Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datakernel-codec Show documentation
Show all versions of datakernel-codec Show documentation
Tools for encoding/decoding of primitives and objects.
package io.datakernel.codec.binary;
import io.datakernel.bytebuf.ByteBuf;
import io.datakernel.bytebuf.ByteBufPool;
import io.datakernel.codec.StructuredEncoder;
import io.datakernel.codec.StructuredOutput;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import static java.nio.charset.StandardCharsets.UTF_8;
public final class BinaryStructuredOutput implements StructuredOutput {
private ByteBuf buf = ByteBufPool.allocate(256);
public ByteBuf getBuf() {
return buf;
}
@Override
public void writeBoolean(boolean value) {
buf = ByteBufPool.ensureWriteRemaining(buf, 1);
buf.writeBoolean(value);
}
@Override
public void writeByte(byte value) {
buf = ByteBufPool.ensureWriteRemaining(buf, 1);
buf.writeByte(value);
}
@Override
public void writeInt(int value) {
buf = ByteBufPool.ensureWriteRemaining(buf, 5);
buf.writeVarInt(value);
}
@Override
public void writeLong(long value) {
buf = ByteBufPool.ensureWriteRemaining(buf, 9);
buf.writeVarLong(value);
}
@Override
public void writeInt32(int value) {
buf = ByteBufPool.ensureWriteRemaining(buf, 4);
buf.writeInt(value);
}
@Override
public void writeLong64(long value) {
buf = ByteBufPool.ensureWriteRemaining(buf, 8);
buf.writeLong(value);
}
@Override
public void writeFloat(float value) {
buf = ByteBufPool.ensureWriteRemaining(buf, 4);
buf.writeFloat(value);
}
@Override
public void writeDouble(double value) {
buf = ByteBufPool.ensureWriteRemaining(buf, 8);
buf.writeDouble(value);
}
@Override
public void writeBytes(byte[] bytes, int off, int len) {
buf = ByteBufPool.ensureWriteRemaining(buf, 5 + len);
buf.writeVarInt(bytes.length);
buf.write(bytes, off, len);
}
@Override
public void writeString(String value) {
buf = ByteBufPool.ensureWriteRemaining(buf, 5 + value.length() * 5);
byte[] bytes = value.getBytes(UTF_8);
buf.writeVarInt(bytes.length);
buf.write(bytes);
}
@Override
public void writeNull() {
writeBoolean(false);
}
@Override
public void writeNullable(StructuredEncoder encoder, T value) {
if (value != null) {
writeBoolean(true);
encoder.encode(this, value);
} else {
writeBoolean(false);
}
}
@Override
public void writeList(StructuredEncoder encoder, List list) {
writeInt(list.size());
for (T item : list) {
encoder.encode(this, item);
}
}
@Override
public void writeMap(StructuredEncoder keyEncoder, StructuredEncoder valueEncoder, Map map) {
writeInt(map.size());
for (Map.Entry entry : map.entrySet()) {
keyEncoder.encode(this, entry.getKey());
valueEncoder.encode(this, entry.getValue());
}
}
@Override
public void writeTuple(StructuredEncoder encoder, T value) {
encoder.encode(this, value);
}
@Override
public void writeObject(StructuredEncoder encoder, T value) {
encoder.encode(this, value);
}
@Override
public void writeKey(String field) {
writeString(field);
}
@Override
public void writeCustom(Type type, T value) {
throw new UnsupportedOperationException("No custom type writers");
}
}