Class Net

java.lang.Object
io.github.connellite.util.Net

public class Net extends Object
Small helpers for name resolution and streaming a URL to a local file.
  • Constructor Details

    • Net

      public Net()
  • Method Details

    • address2Host

      public static String address2Host(String host) throws UnknownHostException
      Resolves a hostname or literal IP to the primary numeric address (IPv4 or IPv6 string).
      Parameters:
      host - hostname or address accepted by InetAddress.getByName(String)
      Returns:
      InetAddress.getHostAddress()
      Throws:
      UnknownHostException - if the name cannot be resolved
    • host2Address

      public static String host2Address(String ip) throws UnknownHostException
      Performs a reverse lookup for a numeric IP or resolves a hostname to its canonical host name.
      Parameters:
      ip - hostname or address accepted by InetAddress.getByName(String)
      Returns:
      InetAddress.getHostName()
      Throws:
      UnknownHostException - if the name cannot be resolved
    • download

      public static void download(String fileUrl, String fileName) throws IOException
      Downloads the response body of fileUrl into fileName (overwrites if it exists).
      Parameters:
      fileUrl - HTTP/HTTPS URL (or other scheme supported by URL.openStream())
      fileName - destination path on the local filesystem
      Throws:
      IOException - if the connection or file write fails
    • findAvailablePort

      public static int findAvailablePort(String hostname, int startPort, int endPort)
      Finds an available port within a port range on a host
      Parameters:
      hostname - Host name
      startPort - Port number starting the range
      endPort - 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 name
      port - Port number
      timeout - Timeout in seconds
      Throws:
      IOException - If a connection error occurs or if the timeout is exceeded