From 526108b896e15010640fbb15b49fad4772268174 Mon Sep 17 00:00:00 2001 From: TheMode Date: Thu, 9 Sep 2021 23:15:57 +0200 Subject: [PATCH] Remove unnecessary allocation during encryption Signed-off-by: TheMode --- .../server/network/player/PlayerSocketConnection.java | 9 ++++----- .../net/minestom/server/utils/entity/EntityUtils.java | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java index 6737ada68..2645db0d0 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerSocketConnection.java @@ -279,16 +279,15 @@ public class PlayerSocketConnection extends PlayerConnection { if (encrypted) { final Cipher cipher = encryptCipher; // Encrypt data first - final int remainingBytes = localBuffer.readableBytes(); - final byte[] bytes = localBuffer.readRemainingBytes(); - byte[] outTempArray = new byte[cipher.getOutputSize(remainingBytes)]; + ByteBuffer cipherInput = localBuffer.asByteBuffer(0, localBuffer.writerOffset()); + ByteBuffer cipherOutput = getPooledBuffer().asByteBuffer(0, BUFFER_SIZE); try { - cipher.update(bytes, 0, remainingBytes, outTempArray); + cipher.update(cipherInput, cipherOutput); } catch (ShortBufferException e) { MinecraftServer.getExceptionManager().handleException(e); } localBuffer.clear(); - localBuffer.writeBytes(outTempArray); + localBuffer.write(cipherOutput.flip()); } this.waitingBuffers.add(localBuffer); diff --git a/src/main/java/net/minestom/server/utils/entity/EntityUtils.java b/src/main/java/net/minestom/server/utils/entity/EntityUtils.java index 68d3cf547..4169f209c 100644 --- a/src/main/java/net/minestom/server/utils/entity/EntityUtils.java +++ b/src/main/java/net/minestom/server/utils/entity/EntityUtils.java @@ -14,7 +14,6 @@ import java.util.function.Consumer; public final class EntityUtils { private EntityUtils() { - } public static void forEachRange(@NotNull Instance instance, @NotNull Point point,