2929import java .io .File ;
3030import java .io .FileOutputStream ;
3131import java .io .IOException ;
32- import java .net .URL ;
32+ import java .io .InputStream ;
33+ import java .net .URI ;
3334import java .nio .file .*;
3435import java .nio .file .attribute .BasicFileAttributes ;
3536import 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 */
0 commit comments