From 333fd0ef807170b4dbf3f3b410a2186323bed200 Mon Sep 17 00:00:00 2001 From: Burning_TNT <88144530+burningtnt@users.noreply.github.com> Date: Sun, 31 Dec 2023 22:51:06 +0800 Subject: [PATCH] Fix #2375 Fix network NPE (#2579) * Fix #2375 * Fix * Fix * Simply codes. --- .../authlibinjector/AuthlibInjectorDownloader.java | 10 +++------- .../authlibinjector/AuthlibInjectorServer.java | 12 +++++------- .../org/jackhuang/hmcl/util/io/HttpRequest.java | 14 +++++++++----- .../java/org/jackhuang/hmcl/util/io/IOUtils.java | 6 ++++++ 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorDownloader.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorDownloader.java index 4961ab716..551e0d630 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorDownloader.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorDownloader.java @@ -22,8 +22,7 @@ import com.google.gson.annotations.SerializedName; import org.jackhuang.hmcl.download.DownloadProvider; import org.jackhuang.hmcl.task.FileDownloadTask; import org.jackhuang.hmcl.task.FileDownloadTask.IntegrityCheck; -import org.jackhuang.hmcl.util.gson.JsonUtils; -import org.jackhuang.hmcl.util.io.NetworkUtils; +import org.jackhuang.hmcl.util.io.HttpRequest; import java.io.IOException; import java.net.URL; @@ -102,7 +101,7 @@ public class AuthlibInjectorDownloader implements AuthlibInjectorArtifactProvide Optional.ofNullable(latest.checksums.get("sha256")) .map(checksum -> new IntegrityCheck("SHA-256", checksum)) .orElse(null)) - .run(); + .run(); } catch (Exception e) { throw new IOException("Failed to download authlib-injector", e); } @@ -112,10 +111,7 @@ public class AuthlibInjectorDownloader implements AuthlibInjectorArtifactProvide private AuthlibInjectorVersionInfo getLatestArtifactInfo() throws IOException { try { - return JsonUtils.fromNonNullJson( - NetworkUtils.doGet( - new URL(downloadProvider.get().injectURL(LATEST_BUILD_URL))), - AuthlibInjectorVersionInfo.class); + return HttpRequest.GET(downloadProvider.get().injectURL(LATEST_BUILD_URL)).getJson(AuthlibInjectorVersionInfo.class); } catch (JsonParseException e) { throw new IOException("Malformed response", e); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServer.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServer.java index 5d124509d..f5102cbe5 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServer.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/auth/authlibinjector/AuthlibInjectorServer.java @@ -17,12 +17,9 @@ */ package org.jackhuang.hmcl.auth.authlibinjector; -import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Collections.emptyMap; import static org.jackhuang.hmcl.util.Lang.tryCast; import static org.jackhuang.hmcl.util.Logging.LOG; -import static org.jackhuang.hmcl.util.io.IOUtils.readFullyAsByteArray; -import static org.jackhuang.hmcl.util.io.IOUtils.readFullyWithoutClosing; import java.io.IOException; import java.lang.reflect.Type; @@ -35,6 +32,8 @@ import java.util.Optional; import java.util.logging.Level; import org.jackhuang.hmcl.auth.yggdrasil.YggdrasilService; +import org.jackhuang.hmcl.util.io.HttpRequest; +import org.jackhuang.hmcl.util.io.IOUtils; import org.jackhuang.hmcl.util.javafx.ObservableHelper; import org.jetbrains.annotations.Nullable; @@ -77,7 +76,7 @@ public class AuthlibInjectorServer implements Observable { try { AuthlibInjectorServer server = new AuthlibInjectorServer(url); - server.refreshMetadata(readFullyWithoutClosing(conn.getInputStream())); + server.refreshMetadata(IOUtils.readFullyAsStringWithClosing(conn.getInputStream())); return server; } finally { conn.disconnect(); @@ -159,12 +158,11 @@ public class AuthlibInjectorServer implements Observable { } public void refreshMetadata() throws IOException { - refreshMetadata(readFullyAsByteArray(new URL(url).openStream())); + refreshMetadata(HttpRequest.GET(url).getString()); } - private void refreshMetadata(byte[] rawResponse) throws IOException { + private void refreshMetadata(String text) throws IOException { long timestamp = System.currentTimeMillis(); - String text = new String(rawResponse, UTF_8); try { setMetadataResponse(text, timestamp); } catch (JsonParseException e) { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/HttpRequest.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/HttpRequest.java index 5414a7c40..4f2016ce7 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/HttpRequest.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/HttpRequest.java @@ -29,7 +29,6 @@ import java.io.OutputStream; import java.lang.reflect.Type; import java.net.HttpURLConnection; import java.net.MalformedURLException; -import java.net.SocketTimeoutException; import java.net.URL; import java.util.HashMap; import java.util.HashSet; @@ -218,16 +217,21 @@ public abstract class HttpRequest { } private static String getStringWithRetry(ExceptionalSupplier supplier, int retryTimes) throws IOException { - SocketTimeoutException exception = null; + Throwable exception = null; for (int i = 0; i < retryTimes; i++) { try { return supplier.get(); - } catch (SocketTimeoutException e) { + } catch (Throwable e) { exception = e; } } - if (exception != null) - throw exception; + if (exception != null) { + if (exception instanceof IOException) { + throw (IOException) exception; + } else { + throw new IOException(exception); + } + } throw new IOException("retry 0"); } diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/IOUtils.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/IOUtils.java index 3b926719a..5afe2c6cc 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/IOUtils.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/IOUtils.java @@ -44,6 +44,12 @@ public final class IOUtils { return result.toByteArray(); } + public static String readFullyAsStringWithClosing(InputStream stream) throws IOException { + ByteArrayOutputStream result = new ByteArrayOutputStream(Math.max(stream.available(), 32)); + copyTo(stream, result); + return result.toString("UTF-8"); + } + /** * Read all bytes to a buffer from given input stream, and close the input stream finally. *