Skip to content

Commit fef90fe

Browse files
authored
feat: upgrade to Java 21 (#502)
1 parent 438984d commit fef90fe

3 files changed

Lines changed: 21 additions & 17 deletions

File tree

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ dependencies {
6565

6666
java {
6767
toolchain {
68-
languageVersion.set(JavaLanguageVersion.of(17))
68+
languageVersion.set(JavaLanguageVersion.of(21))
6969
}
70-
sourceCompatibility = JavaVersion.VERSION_17
71-
targetCompatibility = JavaVersion.VERSION_17
70+
sourceCompatibility = JavaVersion.VERSION_21
71+
targetCompatibility = JavaVersion.VERSION_21
7272
withJavadocJar()
7373
withSourcesJar()
7474
}

src/main/java/com/iexec/common/utils/FileHelper.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
import java.io.File;
3030
import java.io.FileOutputStream;
3131
import java.io.IOException;
32-
import java.net.URL;
32+
import java.io.InputStream;
33+
import java.net.URI;
3334
import java.nio.file.*;
3435
import java.nio.file.attribute.BasicFileAttributes;
3536
import java.util.Objects;
@@ -109,9 +110,10 @@ public static File createFileWithContent(String filePath, byte[] data) {
109110

110111
/**
111112
* Download file with custom name in specified directory
112-
* @param fileUrl URL of the file
113+
*
114+
* @param fileUrl URL of the file
113115
* @param parentFolderPath directory path where the file will be downloaded
114-
* @param outputFilename desired name for future downloaded file
116+
* @param outputFilename desired name for future downloaded file
115117
* @return downloaded file location path if successful download
116118
*/
117119
public static String downloadFile(String fileUrl,
@@ -132,7 +134,7 @@ public static String downloadFile(String fileUrl,
132134
return "";
133135
}
134136
byte[] fileBytes = readFileBytesFromUrl(fileUrl);
135-
if (fileBytes == null) {
137+
if (fileBytes.length == 0) {
136138
log.error("Failed to download file [fileUrl:{}]", fileUrl);
137139
return "";
138140
}
@@ -158,23 +160,25 @@ public static String downloadFile(String fileUrl,
158160

159161
/**
160162
* Read the content of the remote file located at the provided URL.
161-
* @param url of the file
163+
*
164+
* @param uri URI of the file
162165
* @return the content of the file in a byte array if success,
163-
* null otherwise.
166+
* an empty byte array otherwise.
164167
*/
165-
public static byte[] readFileBytesFromUrl(String url) {
166-
try {
167-
return new URL(url).openStream().readAllBytes();
168+
public static byte[] readFileBytesFromUrl(final String uri) {
169+
try (final InputStream inputStream = new URI(uri).toURL().openStream()) {
170+
return inputStream.readAllBytes();
168171
} catch (Exception e) {
169-
log.error("Failed to read file bytes from url [url:{}]", url, e);
170-
return null;
172+
log.error("Failed to read file bytes from URI [uri:{}]", uri, e);
173+
return new byte[0];
171174
}
172175
}
173176

174177
/**
175178
* Download file and returns downloaded file location path if successful.
176179
* Downloaded file name is inferred from uri end path
177-
* @param fileUri URI of the file
180+
*
181+
* @param fileUri URI of the file
178182
* @param downloadDirectoryPath directory path where the file will be downloaded
179183
* @return downloaded file location path if successful download
180184
*/

src/test/java/com/iexec/common/utils/FileHelperTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2025 IEXEC BLOCKCHAIN TECH
2+
* Copyright 2020-2026 IEXEC BLOCKCHAIN TECH
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -262,7 +262,7 @@ void shouldReadFileBytesFromUrl() {
262262
@Test
263263
void shouldNotReadFileBytesFromBadUrl() {
264264
byte[] bytes = FileHelper.readFileBytesFromUrl("http://bad-url");
265-
assertThat(bytes).isNull();
265+
assertThat(bytes).isEqualTo(new byte[0]);
266266
}
267267
// endregion
268268

0 commit comments

Comments
 (0)