Package io.github.connellite.util
Class Net
java.lang.Object
io.github.connellite.util.Net
Small helpers for name resolution and streaming a URL to a local file.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Stringaddress2Host(String host) Resolves a hostname or literal IP to the primary numeric address (IPv4 or IPv6 string).static voidDownloads the response body offileUrlintofileName(overwrites if it exists).static intfindAvailablePort(String hostname, int startPort, int endPort) Finds an available port within a port range on a hoststatic Stringhost2Address(String ip) Performs a reverse lookup for a numeric IP or resolves a hostname to its canonical host name.static voidwaitUntilPortListening(String hostname, int port, int timeout) Waits until a port is listening on a given host.
-
Constructor Details
-
Net
public Net()
-
-
Method Details
-
address2Host
Resolves a hostname or literal IP to the primary numeric address (IPv4 or IPv6 string).- Parameters:
host- hostname or address accepted byInetAddress.getByName(String)- Returns:
InetAddress.getHostAddress()- Throws:
UnknownHostException- if the name cannot be resolved
-
host2Address
Performs a reverse lookup for a numeric IP or resolves a hostname to its canonical host name.- Parameters:
ip- hostname or address accepted byInetAddress.getByName(String)- Returns:
InetAddress.getHostName()- Throws:
UnknownHostException- if the name cannot be resolved
-
download
Downloads the response body offileUrlintofileName(overwrites if it exists).- Parameters:
fileUrl- HTTP/HTTPS URL (or other scheme supported byURL.openStream())fileName- destination path on the local filesystem- Throws:
IOException- if the connection or file write fails
-
findAvailablePort
Finds an available port within a port range on a host- Parameters:
hostname- Host namestartPort- Port number starting the rangeendPort- Port number ending the range- Returns:
- An available port number, or 0 if none is available.
-
waitUntilPortListening
public static void waitUntilPortListening(String hostname, int port, int timeout) throws IOException Waits until a port is listening on a given host. An exception is thrown if the timeout is excedeed.- Parameters:
hostname- Host nameport- Port numbertimeout- Timeout in seconds- Throws:
IOException- If a connection error occurs or if the timeout is exceeded
-