Skip to content

Commit

Permalink
Specify time unit as variable suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
safris committed Dec 17, 2024
1 parent 9b86fc5 commit 8b969dd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/libj/net/DelegateURLConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public void connect() throws IOException {
}

@Override
public void setConnectTimeout(final int timeout) {
target.setConnectTimeout(timeout);
public void setConnectTimeout(final int timeoutMs) {
target.setConnectTimeout(timeoutMs);
}

@Override
Expand All @@ -70,8 +70,8 @@ public int getConnectTimeout() {
}

@Override
public void setReadTimeout(final int timeout) {
target.setReadTimeout(timeout);
public void setReadTimeout(final int timeoutMs) {
target.setReadTimeout(timeoutMs);
}

@Override
Expand Down
40 changes: 20 additions & 20 deletions src/main/java/org/libj/net/Downloads.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ public static HttpURLConnection downloadFile(final String fromUrl, final File to
*
* @param fromUrl The URL from which to download.
* @param toFile The destination {@link File}.
* @param connectTimeout Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the
* @param connectTimeoutMs Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the
* resource referenced by the {@link URLConnection} to {@code fromUrl}. If the timeout expires before the connection can be
* established, a {@link java.net.SocketTimeoutException} is raised. A timeout of zero is interpreted as an infinite
* timeout.
* @param readTimeout Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource
* referenced by the {@link URLConnection} to {@code fromUrl}. If the timeout expires before the connection can be
* @param readTimeoutMs Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the
* resource referenced by the {@link URLConnection} to {@code fromUrl}. If the timeout expires before the connection can be
* established, a {@link java.net.SocketTimeoutException} is raised. A timeout of zero is interpreted as an infinite
* timeout.
* @param options Options specifying how the download should be done.
* @return The <b>closed</b> {@link HttpURLConnection} that was used to download the file.
* @throws IOException If an I/O error has occurred.
* @throws NullPointerException If the provided {@code fromUrl}, {@code toFile}, or {@code options} is null.
*/
public static HttpURLConnection downloadFile(final String fromUrl, final File toFile, final int connectTimeout, final int readTimeout, final CopyOption ... options) throws IOException {
return downloadFile(new URL(fromUrl), toFile, connectTimeout, readTimeout, options);
public static HttpURLConnection downloadFile(final String fromUrl, final File toFile, final int connectTimeoutMs, final int readTimeoutMs, final CopyOption ... options) throws IOException {
return downloadFile(new URL(fromUrl), toFile, connectTimeoutMs, readTimeoutMs, options);
}

/**
Expand All @@ -100,22 +100,22 @@ public static HttpURLConnection downloadFile(final URL fromUrl, final File toFil
*
* @param fromUrl The {@link URL} from which to download.
* @param toFile The destination {@link File}.
* @param connectTimeout Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the
* @param connectTimeoutMs Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the
* resource referenced by the {@link URLConnection} to {@code fromUrl}. If the timeout expires before the connection can be
* established, a {@link java.net.SocketTimeoutException} is raised. A timeout of zero is interpreted as an infinite
* timeout.
* @param readTimeout Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource
* referenced by the {@link URLConnection} to {@code fromUrl}. If the timeout expires before the connection can be
* @param readTimeoutMs Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the
* resource referenced by the {@link URLConnection} to {@code fromUrl}. If the timeout expires before the connection can be
* established, a {@link java.net.SocketTimeoutException} is raised. A timeout of zero is interpreted as an infinite
* timeout.
* @param options Options specifying how the download should be done.
* @return The <b>closed</b> {@link HttpURLConnection} that was used to download the file.
* @throws IOException If an I/O error has occurred.
* @throws NullPointerException If {@code fromUrl}, {@code toFile}, or {@code options} is null.
* @throws IllegalArgumentException If the {@code connectTimeout} or {@code readTimeout} parameter is negative.
* @throws IllegalArgumentException If the {@code connectTimeoutMs} or {@code readTimeoutMs} parameter is negative.
*/
public static HttpURLConnection downloadFile(final URL fromUrl, final File toFile, final int connectTimeout, final int readTimeout, CopyOption ... options) throws IOException {
return downloadFile(fromUrl, toFile, connectTimeout, readTimeout, true, options);
public static HttpURLConnection downloadFile(final URL fromUrl, final File toFile, final int connectTimeoutMs, final int readTimeoutMs, CopyOption ... options) throws IOException {
return downloadFile(fromUrl, toFile, connectTimeoutMs, readTimeoutMs, true, options);
}

/**
Expand All @@ -125,23 +125,23 @@ public static HttpURLConnection downloadFile(final URL fromUrl, final File toFil
*
* @param fromUrl The {@link URL} from which to download.
* @param toFile The destination {@link File}.
* @param connectTimeout Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the
* @param connectTimeoutMs Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the
* resource referenced by the {@link URLConnection} to {@code fromUrl}. If the timeout expires before the connection can be
* established, a {@link java.net.SocketTimeoutException} is raised. A timeout of zero is interpreted as an infinite
* timeout.
* @param readTimeout Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource
* referenced by the {@link URLConnection} to {@code fromUrl}. If the timeout expires before the connection can be
* @param readTimeoutMs Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the
* resource referenced by the {@link URLConnection} to {@code fromUrl}. If the timeout expires before the connection can be
* established, a {@link java.net.SocketTimeoutException} is raised. A timeout of zero is interpreted as an infinite
* timeout.
* @param followRedirects Whether HTTP 301, HTTP 302, or HTTP 303 redirects should be followed.
* @param options Options specifying how the download should be done.
* @return The <b>closed</b> {@link HttpURLConnection} that was used to download the file.
* @throws IOException If an I/O error has occurred.
* @throws NullPointerException If {@code fromUrl}, {@code toFile}, or {@code options} is null.
* @throws IllegalArgumentException If the {@code connectTimeout} or {@code readTimeout} parameter is negative.
* @throws IllegalArgumentException If the {@code connectTimeoutMs} or {@code readTimeoutMs} parameter is negative.
*/
public static HttpURLConnection downloadFile(final URL fromUrl, final File toFile, final int connectTimeout, final int readTimeout, final boolean followRedirects, CopyOption ... options) throws IOException {
final HttpURLConnection connection = (HttpURLConnection)(followRedirects ? URLConnections.checkFollowRedirect(fromUrl.openConnection(), (final HttpURLConnection c) -> beforeDownloadFile(c, connectTimeout, readTimeout, toFile)) : fromUrl.openConnection());
public static HttpURLConnection downloadFile(final URL fromUrl, final File toFile, final int connectTimeoutMs, final int readTimeoutMs, final boolean followRedirects, CopyOption ... options) throws IOException {
final HttpURLConnection connection = (HttpURLConnection)(followRedirects ? URLConnections.checkFollowRedirect(fromUrl.openConnection(), (final HttpURLConnection c) -> beforeDownloadFile(c, connectTimeoutMs, readTimeoutMs, toFile)) : fromUrl.openConnection());
try {
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
try (final InputStream in = connection.getInputStream()) {
Expand All @@ -162,9 +162,9 @@ public static HttpURLConnection downloadFile(final URL fromUrl, final File toFil
}
}

private static void beforeDownloadFile(final HttpURLConnection connection, final int connectTimeout, final int readTimeout, final File toFile) {
connection.setConnectTimeout(connectTimeout);
connection.setReadTimeout(readTimeout);
private static void beforeDownloadFile(final HttpURLConnection connection, final int connectTimeoutMs, final int readTimeoutMs, final File toFile) {
connection.setConnectTimeout(connectTimeoutMs);
connection.setReadTimeout(readTimeoutMs);
if (toFile.exists())
connection.setIfModifiedSince(toFile.lastModified());
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/libj/net/URLs.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,12 @@ public static URL canonicalize(final URL url) {
* </ol>
*
* @param url The {@link URL} to test.
* @param timeout The timeout to be used when attempting to open a connection to the {@code url}.
* @param timeoutMs The timeout to be used when attempting to open a connection to the {@code url}.
* @return {@code true} if the specified {@link URL} references a resource that exists; otherwise {@code false}.
* @throws NullPointerException If {@code url} is null.
* @throws IllegalArgumentException If {@code timeout} is negative.
*/
public static boolean exists(final URL url, final int timeout) {
public static boolean exists(final URL url, final int timeoutMs) {
try {
if ("file".equals(url.getProtocol()))
return new File(url.toURI()).exists();
Expand All @@ -407,7 +407,7 @@ public static boolean exists(final URL url, final int timeout) {

try {
final URLConnection connection = url.openConnection();
connection.setConnectTimeout(timeout);
connection.setConnectTimeout(timeoutMs);
URLConnections.checkFollowRedirect(connection).getInputStream().close();
return true;
}
Expand Down

0 comments on commit 8b969dd

Please sign in to comment.