cache the skin image not the skin image bytes

This commit is contained in:
Lee 2024-04-08 06:38:43 +01:00
parent d3adb6815d
commit d5fc5c0a8c
2 changed files with 13 additions and 8 deletions

@ -8,6 +8,7 @@ import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import java.awt.image.BufferedImage;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -20,19 +21,19 @@ public class Skin {
Model.DEFAULT); Model.DEFAULT);
/** /**
* The URL of the skin * The URL for the skin
*/ */
private final String url; private final String url;
/** /**
* The model of the skin * The model for the skin
*/ */
private final Model model; 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 * The part URLs of the skin
@ -44,7 +45,7 @@ public class Skin {
this.url = url; this.url = url;
this.model = model; this.model = model;
this.skinData = PlayerUtils.getSkinData(url); this.skinImage = PlayerUtils.getSkinImage(url);
} }
/** /**

@ -26,13 +26,17 @@ public class PlayerUtils {
*/ */
@SneakyThrows @SneakyThrows
@JsonIgnore @JsonIgnore
public static byte[] getSkinData(String url) { public static BufferedImage getSkinImage(String url) {
HttpRequest request = HttpRequest.newBuilder() HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(url)) .uri(new URI(url))
.GET() .GET()
.build(); .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 { try {
BufferedImage image = ImageIO.read(new ByteArrayInputStream(skin.getSkinData())); BufferedImage image = skin.getSkinImage();
if (image == null) { if (image == null) {
return null; return null;
} }