diff --git a/src/main/java/net/pixtaded/crab/client/CrabClient.java b/src/main/java/net/pixtaded/crab/client/CrabClient.java index 5ae46d1..6355e80 100644 --- a/src/main/java/net/pixtaded/crab/client/CrabClient.java +++ b/src/main/java/net/pixtaded/crab/client/CrabClient.java @@ -18,6 +18,7 @@ public class CrabClient implements Crab { private BufferedReader in; private int lastBufferLength = 0; private String nickname; + private Logs cache = new Logs(0, ""); public CrabClient() { this.nickname = ""; @@ -111,9 +112,12 @@ public class CrabClient implements Crab { int response = in.read(buffer); lastBufferLength = Integer.parseInt(new String(buffer).trim()); } case LOGS -> { - byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength); - System.out.print("\033[H\033[2J"); - System.out.print(Sanitizer.sanitizeString(new String(bytes, StandardCharsets.UTF_8), false)); + if (cache.sizeInBytes() != lastBufferLength) { + byte[] bytes = socket.getInputStream().readNBytes(lastBufferLength); + cache = new Logs(lastBufferLength, new String(bytes, StandardCharsets.UTF_8)); + } + clearScreen(); + System.out.print(Sanitizer.sanitizeString(cache.content(), false)); } default -> { } } @@ -135,4 +139,9 @@ public class CrabClient implements Crab { sendPacket(LOGS_SIZE, ""); sendPacket(LOGS, ""); } + + private void clearScreen() { + System.out.print("\033[H\033[2J"); + } + } diff --git a/src/main/java/net/pixtaded/crab/client/Logs.java b/src/main/java/net/pixtaded/crab/client/Logs.java new file mode 100644 index 0000000..479e41c --- /dev/null +++ b/src/main/java/net/pixtaded/crab/client/Logs.java @@ -0,0 +1,4 @@ +package net.pixtaded.crab.client; + +public record Logs(int sizeInBytes, String content) { +} diff --git a/src/main/java/net/pixtaded/crab/server/CrabServer.java b/src/main/java/net/pixtaded/crab/server/CrabServer.java index d5705a4..3af44dc 100644 --- a/src/main/java/net/pixtaded/crab/server/CrabServer.java +++ b/src/main/java/net/pixtaded/crab/server/CrabServer.java @@ -12,7 +12,7 @@ public class CrabServer implements Crab { private Socket socket; private boolean isStopped = false; private int port; - private Database db; + private final Database db; public CrabServer() { this.db = new Database("data.db");