se.arkalix.dto.util.BinaryWriterWriteCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kalix-dto-processor Show documentation
Show all versions of kalix-dto-processor Show documentation
Contains an annotation processor for automatically generating data transmission objects specified using the annotations of the kalix-dto library.
The newest version!
package se.arkalix.dto.util;
import com.squareup.javapoet.MethodSpec;
import java.nio.charset.StandardCharsets;
public class BinaryWriterWriteCache {
private final StringBuilder builder = new StringBuilder();
private final String writerName;
public BinaryWriterWriteCache(final String writerName) {
this.writerName = writerName;
}
public BinaryWriterWriteCache append(final char c) {
builder.append(c);
return this;
}
public BinaryWriterWriteCache append(final String string) {
builder.append(string);
return this;
}
public void clear() {
builder.setLength(0);
}
public void addWrite(final MethodSpec.Builder methodBuilder) {
final var input = builder.toString().getBytes(StandardCharsets.UTF_8);
if (input.length == 1) {
methodBuilder.addStatement(writerName + ".writeS8((byte) " + literalOf(input[0]) + ")");
}
else {
final var output = new StringBuilder(builder.length() * 4);
output.append(writerName).append(".write(new byte[]{");
for (var i = 0; i < input.length; ++i) {
if (i != 0) {
output.append(", ");
}
output.append(literalOf(input[i]));
}
output.append("})");
methodBuilder.addStatement(output.toString());
}
builder.setLength(0);
}
public void addWriteIfNotEmpty(final MethodSpec.Builder methodBuilder) {
if (builder.length() > 0) {
addWrite(methodBuilder);
}
}
private String literalOf(final byte b) {
if (b >= 0x20 && b <= 0x7E) {
return "'" + ((char) b) + "'";
}
else {
return "0x" + Integer.toHexString(b);
}
}
}