From a00a8763d6ebc240ab06380c04a762d10dfc7939 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 4 Jun 2020 18:33:42 +0200 Subject: [PATCH] Avoid additional copy on Java text parsing Directly pass the buffer range to the String constructor. --- .../java/com/genymobile/scrcpy/ControlMessageReader.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java b/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java index f80ffebb..b8fba824 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java +++ b/server/src/main/java/com/genymobile/scrcpy/ControlMessageReader.java @@ -21,7 +21,6 @@ public class ControlMessageReader { private final byte[] rawBuffer = new byte[MESSAGE_MAX_SIZE]; private final ByteBuffer buffer = ByteBuffer.wrap(rawBuffer); - private final byte[] textBuffer = new byte[CLIPBOARD_TEXT_MAX_LENGTH]; public ControlMessageReader() { // invariant: the buffer is always in "get" mode @@ -111,8 +110,10 @@ public class ControlMessageReader { if (buffer.remaining() < len) { return null; } - buffer.get(textBuffer, 0, len); - return new String(textBuffer, 0, len, StandardCharsets.UTF_8); + int position = buffer.position(); + // Move the buffer position to consume the text + buffer.position(position + len); + return new String(rawBuffer, position, len, StandardCharsets.UTF_8); } private ControlMessage parseInjectText() {