Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
28 changes: 16 additions & 12 deletions src/main/java/com/iexec/common/utils/FileHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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 "";
}
Expand All @@ -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
*/
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/iexec/common/utils/FileHelperTests.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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

Expand Down
Loading