Actually Java 11

This commit is contained in:
Minecon724 2024-11-10 09:57:04 +01:00
parent d183e5286d
commit f0cf9c2298
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
4 changed files with 22 additions and 22 deletions

View file

@ -8,5 +8,5 @@
</list> </list>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="temurin-21" project-jdk-type="JavaSDK" /> <component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="temurin-11" project-jdk-type="JavaSDK" />
</project> </project>

View file

@ -17,7 +17,6 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.SignatureException; import java.security.SignatureException;
import java.util.Arrays; import java.util.Arrays;
import java.util.HexFormat;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException; import java.util.concurrent.CompletionException;
@ -59,12 +58,12 @@ public class SimpleDownloader implements Downloader {
} }
} }
if (!Arrays.equals(messageDigest.digest(), HexFormat.of().parseHex(sha256hex))) if (!Arrays.equals(messageDigest.digest(), hexStringToByteArray(sha256hex)))
throw new SignatureException(); // This is not outside try because impact is too small to justify more code throw new SignatureException(); // This is not outside try because impact is too small to justify more code
return downloadedFile; return downloadedFile;
} catch (IOException | NoSuchAlgorithmException | SignatureException e) { // TODO nosuchalgo should not be thrown } catch (IOException | NoSuchAlgorithmException | SignatureException e) {
throw new CompletionException(e); throw new CompletionException(e);
} }
@ -85,4 +84,13 @@ public class SimpleDownloader implements Downloader {
}); });
} }
private static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
} }

View file

@ -1,20 +1,18 @@
package eu.m724.jarupdater.live; package eu.m724.jarupdater.live;
import com.google.gson.Gson;
import eu.m724.jarupdater.object.Version;
import java.io.IOException; import java.io.IOException;
import java.net.ProxySelector; import java.net.ProxySelector;
import java.net.URI; import java.net.URI;
import java.net.http.HttpClient; import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.net.http.HttpClient.Redirect;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException; import java.util.concurrent.CompletionException;
import java.util.stream.Collectors;
import com.google.gson.Gson;
import eu.m724.jarupdater.object.Version;
public class GiteaMetadataDAO implements MetadataDAO { public class GiteaMetadataDAO implements MetadataDAO {
private final String url; private final String url;
@ -32,9 +30,9 @@ public class GiteaMetadataDAO implements MetadataDAO {
return makeRequest(url).thenApply(response -> { return makeRequest(url).thenApply(response -> {
if (response.statusCode() != 200) if (response.statusCode() != 200)
throw new CompletionException(new IOException("Server returned status code %d".formatted(response.statusCode()))); throw new CompletionException(new IOException("Server returned status code " + response.statusCode()));
return response.body().lines().toList(); return response.body().lines().collect(Collectors.toList());
}); });
} }
@ -46,7 +44,7 @@ public class GiteaMetadataDAO implements MetadataDAO {
return makeRequest(url).thenApply(response -> { return makeRequest(url).thenApply(response -> {
if (response.statusCode() != 200) if (response.statusCode() != 200)
throw new CompletionException(new IOException("Server returned status code %d".formatted(response.statusCode()))); throw new CompletionException(new IOException("Server returned status code " + response.statusCode()));
// not throwing nosuchversionexecpion because it's not possible to tell if the server is broken or it really doesn't exist // not throwing nosuchversionexecpion because it's not possible to tell if the server is broken or it really doesn't exist
return new Gson().fromJson(response.body(), Version.class); return new Gson().fromJson(response.body(), Version.class);
@ -61,12 +59,11 @@ public class GiteaMetadataDAO implements MetadataDAO {
.build(); .build();
HttpClient.Builder httpClientBuilder = HttpClient.newBuilder() HttpClient.Builder httpClientBuilder = HttpClient.newBuilder()
.followRedirects(Redirect.NORMAL) .followRedirects(HttpClient.Redirect.NORMAL)
.proxy(ProxySelector.getDefault()); .proxy(ProxySelector.getDefault());
try (HttpClient httpClient = httpClientBuilder.build()) { HttpClient httpClient = httpClientBuilder.build();
return httpClient.sendAsync(request, BodyHandlers.ofString()); return httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString());
}
} }
private String getFileUrl(String... paths) { private String getFileUrl(String... paths) {

View file

@ -1,13 +1,8 @@
package eu.m724.jarupdater.object; package eu.m724.jarupdater.object;
import java.io.IOException; import java.io.IOException;
import java.io.Serial;
public class NoSuchVersionException extends IOException { public class NoSuchVersionException extends IOException {
@Serial
private static final long serialVersionUID = 8435964987348892266L;
public NoSuchVersionException(String version) { public NoSuchVersionException(String version) {
super(version); super(version);
} }