Created
April 23, 2018 21:38
-
-
Save darkevilmac/1174375d361201c911ac36a38c977ec2 to your computer and use it in GitHub Desktop.
dank ass boolean compression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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