Package org.biojava.nbio.core.util
Class FileDownloadUtils
java.lang.Object
org.biojava.nbio.core.util.FileDownloadUtils
-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
createValidationFiles
(URLConnection resourceUrlConnection, File localDestination, URL hashURL, FileDownloadUtils.Hash hash) Creates validation files beside a file to be downloaded.
Whenever possible, for afile.ext
file, it createsfile.ext.size
andfile.hash_XXXX
in the same folder wherefile.ext
exists (XXXX may be DM5, SHA1, or SHA256).static void
createValidationFiles
(URL url, File localDestination, URL hashURL, FileDownloadUtils.Hash hash) Creates validation files beside a file to be downloaded.
Whenever possible, for afile.ext
file, it createsfile.ext.size
andfile.hash
for in the same folder wherefile.ext
exists.static void
deleteDirectory
(String dir) Recursively delete a folder & contentsstatic void
deleteDirectory
(Path dir) Recursively delete a folder & contentsstatic void
downloadFile
(URL url, File destination) Download the content provided at URL url and store the result to a local file, using a temp file to cache the content in case something goes wrong in download.static String
expandUserHome
(String file) Expands ~ in paths to the user's home directory.static String
Gets the file extension of a file, excluding '.'.static String
Gets the file name up to and excluding the first '.' character.static boolean
Pings a HTTP URL.static URLConnection
prepareURLConnection
(String url, int timeout) PrepareURLConnection
with customised timeouts.static String
toUnixPath
(String path) Converts path to Unix convention and adds a terminating slash if it was omitted.static boolean
validateFile
(File localFile) Validate a local file based on pre-existing metadata files for size and hash.
If the passed inlocalFile
parameter is a file namedfile.ext
, the function searches in the same folder for:file.ext.size
: If found, it compares the size stored in it to the length oflocalFile
(in bytes).file.ext.hash_XXXX (where XXXX is DM5, SHA1, or SHA256)
: If found, it compares the size stored in it to the hash code oflocalFile
. If any of these comparisons fail, the function returnsfalse
. otherwise it returns true.
-
Constructor Details
-
FileDownloadUtils
public FileDownloadUtils()
-
-
Method Details
-
getFileExtension
Gets the file extension of a file, excluding '.'. If the file name has no extension the file name is returned.- Parameters:
f
- a File- Returns:
- The extension
-
getFilePrefix
Gets the file name up to and excluding the first '.' character. If there is no extension, the full filename is returned.- Parameters:
f
- A file- Returns:
- A possibly empty but non-null String.
-
downloadFile
Download the content provided at URL url and store the result to a local file, using a temp file to cache the content in case something goes wrong in download. A timeout of 60 seconds is hard-coded and 10 retries are attempted.- Parameters:
url
-destination
-- Throws:
IOException
-
createValidationFiles
public static void createValidationFiles(URL url, File localDestination, URL hashURL, FileDownloadUtils.Hash hash) Creates validation files beside a file to be downloaded.
Whenever possible, for afile.ext
file, it createsfile.ext.size
andfile.hash
for in the same folder wherefile.ext
exists. If the file connection size could not be deduced from the URL, no size file is created. IfhashURL
isnull
, no hash file is created.- Parameters:
url
- the remote file URL to downloadlocalDestination
- the local file to download intohashURL
- the URL of the hash file to download. Can benull
.hash
- The Hashing algorithm. Ignored ifhashURL
isnull
.
-
createValidationFiles
public static void createValidationFiles(URLConnection resourceUrlConnection, File localDestination, URL hashURL, FileDownloadUtils.Hash hash) Creates validation files beside a file to be downloaded.
Whenever possible, for afile.ext
file, it createsfile.ext.size
andfile.hash_XXXX
in the same folder wherefile.ext
exists (XXXX may be DM5, SHA1, or SHA256). If the file connection size could not be deduced from the resourceUrlConnectionURLConnection
, no size file is created. IfhashURL
isnull
, no hash file is created.
N.B. None of the hashing algorithms is implemented (yet), because we did not need any of them yet.- Parameters:
resourceUrlConnection
- the remote file URLConnection to downloadlocalDestination
- the local file to download intohashURL
- the URL of the hash file to download. Can benull
.hash
- The Hashing algorithm. Ignored ifhashURL
isnull
.- Since:
- 7.0.0
-
validateFile
Validate a local file based on pre-existing metadata files for size and hash.
If the passed inlocalFile
parameter is a file namedfile.ext
, the function searches in the same folder for:file.ext.size
: If found, it compares the size stored in it to the length oflocalFile
(in bytes).file.ext.hash_XXXX (where XXXX is DM5, SHA1, or SHA256)
: If found, it compares the size stored in it to the hash code oflocalFile
.
false
. otherwise it returns true.N.B. None of the 3 common verification hashing algorithms are implement yet.
- Parameters:
localFile
- The file to validate- Returns:
false
if any of the size or hash code metadata files exists but its contents does not match the expected value in the file,true
otherwise.- Since:
- 7.0.0
-
toUnixPath
Converts path to Unix convention and adds a terminating slash if it was omitted.- Parameters:
path
- original platform dependent path- Returns:
- path in Unix convention
- Since:
- 3.2
-
expandUserHome
Expands ~ in paths to the user's home directory.This does not work for some special cases for paths: Other users' homes (~user/...), and Tilde expansion within the path (/.../~/...). In these cases the original argument is returned.
- Parameters:
file
- A filepath starting with a tilde- Returns:
- An absolute path
-
ping
Pings a HTTP URL. This effectively sends a HEAD request and returnstrue
if the response code is in the 200-399 range.- Parameters:
url
- The HTTP URL to be pinged.timeout
- The timeout in millis for both the connection timeout and the response read timeout. Note that the total timeout is effectively two times the given timeout.- Returns:
true
if the given HTTP URL has returned response code 200-399 on a HEAD request within the given timeout, otherwisefalse
.
-
prepareURLConnection
PrepareURLConnection
with customised timeouts.- Parameters:
url
- The URLtimeout
- The timeout in millis for both the connection timeout and the response read timeout. Note that the total timeout is effectively two times the given timeout.Example of code.
UrlConnection conn = prepareURLConnection("http://www.google.com/", 20000); conn.connect(); conn.getInputStream();
NB. User should execute connect() method before getting input stream.
- Returns:
- Throws:
IOException
-
deleteDirectory
Recursively delete a folder & contents- Parameters:
dir
- directory to delete- Throws:
IOException
-
deleteDirectory
Recursively delete a folder & contents- Parameters:
dir
- directory to delete- Throws:
IOException
-