From 30021fb1f41316aa38009ee484fd37069eabdd83 Mon Sep 17 00:00:00 2001 From: Niels NTG Date: Wed, 14 Dec 2022 23:19:59 +0100 Subject: [PATCH] Improve performance plain-text generation GET /blocks --- .../handlers/BlocksHandler.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/gdmc/httpinterfacemod/handlers/BlocksHandler.java b/src/main/java/com/gdmc/httpinterfacemod/handlers/BlocksHandler.java index 6799174..b8f3434 100644 --- a/src/main/java/com/gdmc/httpinterfacemod/handlers/BlocksHandler.java +++ b/src/main/java/com/gdmc/httpinterfacemod/handlers/BlocksHandler.java @@ -99,12 +99,12 @@ public void internalHandle(HttpExchange httpExchange) throws IOException { // construct response String method = httpExchange.getRequestMethod().toLowerCase(); - String responseString = ""; + String responseString; if (method.equals("put")) { InputStream bodyStream = httpExchange.getRequestBody(); List body = new BufferedReader(new InputStreamReader(bodyStream)) - .lines().collect(Collectors.toList()); + .lines().toList(); List returnValues = new LinkedList<>(); @@ -163,6 +163,7 @@ public void internalHandle(HttpExchange httpExchange) throws IOException { } } else if (method.equals("get")) { JsonArray jsonArray = new JsonArray(); + StringBuilder responseStringBuilder = new StringBuilder(); for (int rangeX = x; rangeX < x + dx; rangeX++) { for (int rangeY = y; rangeY < y + dy; rangeY++) { for (int rangeZ = z; rangeZ < z + dz; rangeZ++) { @@ -182,14 +183,20 @@ public void internalHandle(HttpExchange httpExchange) throws IOException { } jsonArray.add(json); } else { - responseString += rangeX + " " + rangeY + " " + rangeZ + " " + blockId; + responseStringBuilder.append(rangeX); + responseStringBuilder.append(' '); + responseStringBuilder.append(rangeY); + responseStringBuilder.append(' '); + responseStringBuilder.append(rangeZ); + responseStringBuilder.append(' '); + responseStringBuilder.append(getBlockAsStr(blockPos)); if (includeState) { - responseString += getBlockStateAsStr(blockPos); + responseStringBuilder.append(getBlockStateAsStr(blockPos)); } if (includeData) { - responseString += getBlockDataAsStr(blockPos); + responseStringBuilder.append(getBlockDataAsStr(blockPos)); } - responseString += "\n"; + responseStringBuilder.append('\n'); } } } @@ -197,7 +204,7 @@ public void internalHandle(HttpExchange httpExchange) throws IOException { if (returnJson) { responseString = new Gson().toJson(jsonArray); } else { - responseString = responseString.trim(); + responseString = responseStringBuilder.deleteCharAt(responseStringBuilder.length() - 1).toString(); } } else if (method.equals("options")) {