Remove unnecessary allocation during encryption

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-09-09 23:15:57 +02:00
parent 344003f36a
commit 526108b896
2 changed files with 4 additions and 6 deletions

View File

@ -279,16 +279,15 @@ public class PlayerSocketConnection extends PlayerConnection {
if (encrypted) { if (encrypted) {
final Cipher cipher = encryptCipher; final Cipher cipher = encryptCipher;
// Encrypt data first // Encrypt data first
final int remainingBytes = localBuffer.readableBytes(); ByteBuffer cipherInput = localBuffer.asByteBuffer(0, localBuffer.writerOffset());
final byte[] bytes = localBuffer.readRemainingBytes(); ByteBuffer cipherOutput = getPooledBuffer().asByteBuffer(0, BUFFER_SIZE);
byte[] outTempArray = new byte[cipher.getOutputSize(remainingBytes)];
try { try {
cipher.update(bytes, 0, remainingBytes, outTempArray); cipher.update(cipherInput, cipherOutput);
} catch (ShortBufferException e) { } catch (ShortBufferException e) {
MinecraftServer.getExceptionManager().handleException(e); MinecraftServer.getExceptionManager().handleException(e);
} }
localBuffer.clear(); localBuffer.clear();
localBuffer.writeBytes(outTempArray); localBuffer.write(cipherOutput.flip());
} }
this.waitingBuffers.add(localBuffer); this.waitingBuffers.add(localBuffer);

View File

@ -14,7 +14,6 @@ import java.util.function.Consumer;
public final class EntityUtils { public final class EntityUtils {
private EntityUtils() { private EntityUtils() {
} }
public static void forEachRange(@NotNull Instance instance, @NotNull Point point, public static void forEachRange(@NotNull Instance instance, @NotNull Point point,