fix network NPE

This commit is contained in:
Tungstend 2024-01-06 21:19:31 +08:00
parent d6f9861e30
commit a569f3a953
4 changed files with 23 additions and 15 deletions

View File

@ -23,8 +23,7 @@ import com.google.gson.JsonParseException;
import com.google.gson.annotations.SerializedName;
import com.tungsten.fclcore.download.DownloadProvider;
import com.tungsten.fclcore.task.FileDownloadTask;
import com.tungsten.fclcore.util.gson.JsonUtils;
import com.tungsten.fclcore.util.io.NetworkUtils;
import com.tungsten.fclcore.util.io.HttpRequest;
import java.io.IOException;
import java.net.URL;
@ -98,7 +97,7 @@ public class AuthlibInjectorDownloader implements AuthlibInjectorArtifactProvide
Optional.ofNullable(latest.checksums.get("sha256"))
.map(checksum -> new FileDownloadTask.IntegrityCheck("SHA-256", checksum))
.orElse(null))
.run();
.run();
} catch (Exception e) {
throw new IOException("Failed to download authlib-injector", e);
}
@ -108,10 +107,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);
}

View File

@ -48,6 +48,8 @@ import com.tungsten.fclcore.auth.yggdrasil.YggdrasilService;
import com.tungsten.fclcore.fakefx.beans.InvalidationListener;
import com.tungsten.fclcore.fakefx.beans.Observable;
import com.tungsten.fclcore.util.fakefx.ObservableHelper;
import com.tungsten.fclcore.util.io.HttpRequest;
import com.tungsten.fclcore.util.io.IOUtils;
@JsonAdapter(AuthlibInjectorServer.Deserializer.class)
public class AuthlibInjectorServer implements Observable {
@ -74,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();
@ -156,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) {

View File

@ -219,16 +219,21 @@ public abstract class HttpRequest {
}
private static String getStringWithRetry(ExceptionalSupplier<String, IOException> 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");
}

View File

@ -42,6 +42,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.
*