Skip to content

Instantly share code, notes, and snippets.

@darkevilmac
Created April 23, 2018 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkevilmac/1174375d361201c911ac36a38c977ec2 to your computer and use it in GitHub Desktop.
Save darkevilmac/1174375d361201c911ac36a38c977ec2 to your computer and use it in GitHub Desktop.
dank ass boolean compression
public void toBytes(Message msg, ByteBuf target) {
List<MessageField> booleanFields = messageFields.stream()
.filter(mf -> mf.getType() == Boolean.class).sorted(
Comparator.comparing(o -> o.getField().getName())).collect(Collectors.toList());
if (!booleanFields.isEmpty()) {
byte currentByte = 0x00000000;
int byteIndex = 0;
for (int i = 0; i < booleanFields.size(); i++) {
if (i == 8) {
byteIndex = 0;
currentByte = 0x00000000;
}
MessageField<Boolean> msgField = booleanFields.get(i);
Boolean fieldValue = msgField.getValue(msg);
if (fieldValue) {
currentByte |= (1 << byteIndex);
}
if (i + 1 == booleanFields.size() || currentByte == 7) {
target.writeByte(currentByte);
}
}
}
for (int i = 0; i < messageFields.size(); i++) {
MessageField msgField = messageFields.get(i);
if (msgField.getType() != Boolean.class) {
msgField.writeField(msg, target);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment