From d5fc5c0a8c962ca93a89f9b0a63eeeed15a12944 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 8 Apr 2024 06:38:43 +0100 Subject: [PATCH] cache the skin image not the skin image bytes --- src/main/java/cc/fascinated/model/player/Skin.java | 11 ++++++----- src/main/java/cc/fascinated/util/PlayerUtils.java | 10 +++++++--- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main/java/cc/fascinated/model/player/Skin.java b/src/main/java/cc/fascinated/model/player/Skin.java index cd593c0..e80c326 100644 --- a/src/main/java/cc/fascinated/model/player/Skin.java +++ b/src/main/java/cc/fascinated/model/player/Skin.java @@ -8,6 +8,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; import lombok.extern.log4j.Log4j2; +import java.awt.image.BufferedImage; import java.util.HashMap; import java.util.Map; @@ -20,19 +21,19 @@ public class Skin { Model.DEFAULT); /** - * The URL of the skin + * The URL for the skin */ private final String url; /** - * The model of the skin + * The model for the skin */ private final Model model; /** - * The skin data of the skin + * The skin image for the skin */ - private final byte[] skinData; + private final BufferedImage skinImage; /** * The part URLs of the skin @@ -44,7 +45,7 @@ public class Skin { this.url = url; this.model = model; - this.skinData = PlayerUtils.getSkinData(url); + this.skinImage = PlayerUtils.getSkinImage(url); } /** diff --git a/src/main/java/cc/fascinated/util/PlayerUtils.java b/src/main/java/cc/fascinated/util/PlayerUtils.java index e134218..ab32914 100644 --- a/src/main/java/cc/fascinated/util/PlayerUtils.java +++ b/src/main/java/cc/fascinated/util/PlayerUtils.java @@ -26,13 +26,17 @@ public class PlayerUtils { */ @SneakyThrows @JsonIgnore - public static byte[] getSkinData(String url) { + public static BufferedImage getSkinImage(String url) { HttpRequest request = HttpRequest.newBuilder() .uri(new URI(url)) .GET() .build(); - return Main.HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofByteArray()).body(); + byte[] body = Main.HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofByteArray()).body(); + if (body == null) { + return null; + } + return ImageIO.read(new ByteArrayInputStream(body)); } /** @@ -46,7 +50,7 @@ public class PlayerUtils { } try { - BufferedImage image = ImageIO.read(new ByteArrayInputStream(skin.getSkinData())); + BufferedImage image = skin.getSkinImage(); if (image == null) { return null; }