From 8df3eb9bb10ddcf1a4b3fd72fa8a48902e06f088 Mon Sep 17 00:00:00 2001 From: Jeremy Bernard Date: Tue, 12 May 2026 17:17:10 +0200 Subject: [PATCH] feat: upgrade to Java 21 --- build.gradle | 6 ++-- .../com/iexec/common/utils/FileHelper.java | 28 +++++++++++-------- .../iexec/common/utils/FileHelperTests.java | 4 +-- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/build.gradle b/build.gradle index d7e52c78..d2d88cc1 100644 --- a/build.gradle +++ b/build.gradle @@ -65,10 +65,10 @@ dependencies { java { toolchain { - languageVersion.set(JavaLanguageVersion.of(17)) + languageVersion.set(JavaLanguageVersion.of(21)) } - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 + sourceCompatibility = JavaVersion.VERSION_21 + targetCompatibility = JavaVersion.VERSION_21 withJavadocJar() withSourcesJar() } diff --git a/src/main/java/com/iexec/common/utils/FileHelper.java b/src/main/java/com/iexec/common/utils/FileHelper.java index 631610b9..17ad545a 100644 --- a/src/main/java/com/iexec/common/utils/FileHelper.java +++ b/src/main/java/com/iexec/common/utils/FileHelper.java @@ -29,7 +29,8 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; -import java.net.URL; +import java.io.InputStream; +import java.net.URI; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; import java.util.Objects; @@ -109,9 +110,10 @@ public static File createFileWithContent(String filePath, byte[] data) { /** * Download file with custom name in specified directory - * @param fileUrl URL of the file + * + * @param fileUrl URL of the file * @param parentFolderPath directory path where the file will be downloaded - * @param outputFilename desired name for future downloaded file + * @param outputFilename desired name for future downloaded file * @return downloaded file location path if successful download */ public static String downloadFile(String fileUrl, @@ -132,7 +134,7 @@ public static String downloadFile(String fileUrl, return ""; } byte[] fileBytes = readFileBytesFromUrl(fileUrl); - if (fileBytes == null) { + if (fileBytes.length == 0) { log.error("Failed to download file [fileUrl:{}]", fileUrl); return ""; } @@ -158,23 +160,25 @@ public static String downloadFile(String fileUrl, /** * Read the content of the remote file located at the provided URL. - * @param url of the file + * + * @param uri URI of the file * @return the content of the file in a byte array if success, - * null otherwise. + * an empty byte array otherwise. */ - public static byte[] readFileBytesFromUrl(String url) { - try { - return new URL(url).openStream().readAllBytes(); + public static byte[] readFileBytesFromUrl(final String uri) { + try (final InputStream inputStream = new URI(uri).toURL().openStream()) { + return inputStream.readAllBytes(); } catch (Exception e) { - log.error("Failed to read file bytes from url [url:{}]", url, e); - return null; + log.error("Failed to read file bytes from URI [uri:{}]", uri, e); + return new byte[0]; } } /** * Download file and returns downloaded file location path if successful. * Downloaded file name is inferred from uri end path - * @param fileUri URI of the file + * + * @param fileUri URI of the file * @param downloadDirectoryPath directory path where the file will be downloaded * @return downloaded file location path if successful download */ diff --git a/src/test/java/com/iexec/common/utils/FileHelperTests.java b/src/test/java/com/iexec/common/utils/FileHelperTests.java index 111d80f9..c8970601 100644 --- a/src/test/java/com/iexec/common/utils/FileHelperTests.java +++ b/src/test/java/com/iexec/common/utils/FileHelperTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2020-2025 IEXEC BLOCKCHAIN TECH + * Copyright 2020-2026 IEXEC BLOCKCHAIN TECH * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -262,7 +262,7 @@ void shouldReadFileBytesFromUrl() { @Test void shouldNotReadFileBytesFromBadUrl() { byte[] bytes = FileHelper.readFileBytesFromUrl("http://bad-url"); - assertThat(bytes).isNull(); + assertThat(bytes).isEqualTo(new byte[0]); } // endregion