|
@@ -22,8 +22,10 @@ import java.io.InputStream;
|
|
|
import java.io.OutputStream;
|
|
|
import java.net.InetAddress;
|
|
|
import java.net.InetSocketAddress;
|
|
|
+import java.net.NetworkInterface;
|
|
|
import java.net.Socket;
|
|
|
import java.net.SocketAddress;
|
|
|
+import java.net.SocketException;
|
|
|
import java.net.URI;
|
|
|
import java.net.UnknownHostException;
|
|
|
import java.nio.channels.SocketChannel;
|
|
@@ -281,7 +283,7 @@ public class NetUtils {
|
|
|
* case, the timeout argument is ignored and the timeout set with
|
|
|
* {@link Socket#setSoTimeout(int)} applies for reads.<br><br>
|
|
|
*
|
|
|
- * Any socket created using socket factories returned by {@link #NetUtils},
|
|
|
+ * Any socket created using socket factories returned by {@link NetUtils},
|
|
|
* must use this interface instead of {@link Socket#getInputStream()}.
|
|
|
*
|
|
|
* @see #getInputStream(Socket, long)
|
|
@@ -303,7 +305,7 @@ public class NetUtils {
|
|
|
* case, the timeout argument is ignored and the timeout set with
|
|
|
* {@link Socket#setSoTimeout(int)} applies for reads.<br><br>
|
|
|
*
|
|
|
- * Any socket created using socket factories returned by {@link #NetUtils},
|
|
|
+ * Any socket created using socket factories returned by {@link NetUtils},
|
|
|
* must use this interface instead of {@link Socket#getInputStream()}.
|
|
|
*
|
|
|
* @see Socket#getChannel()
|
|
@@ -332,7 +334,7 @@ public class NetUtils {
|
|
|
* case, the timeout argument is ignored and the write will wait until
|
|
|
* data is available.<br><br>
|
|
|
*
|
|
|
- * Any socket created using socket factories returned by {@link #NetUtils},
|
|
|
+ * Any socket created using socket factories returned by {@link NetUtils},
|
|
|
* must use this interface instead of {@link Socket#getOutputStream()}.
|
|
|
*
|
|
|
* @see #getOutputStream(Socket, long)
|
|
@@ -354,7 +356,7 @@ public class NetUtils {
|
|
|
* case, the timeout argument is ignored and the write will wait until
|
|
|
* data is available.<br><br>
|
|
|
*
|
|
|
- * Any socket created using socket factories returned by {@link #NetUtils},
|
|
|
+ * Any socket created using socket factories returned by {@link NetUtils},
|
|
|
* must use this interface instead of {@link Socket#getOutputStream()}.
|
|
|
*
|
|
|
* @see Socket#getChannel()
|
|
@@ -441,4 +443,27 @@ public class NetUtils {
|
|
|
}
|
|
|
return hostNames;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Checks if {@code host} is a local host name and return {@link InetAddress}
|
|
|
+ * corresponding to that address.
|
|
|
+ *
|
|
|
+ * @param host the specified host
|
|
|
+ * @return a valid local {@link InetAddress} or null
|
|
|
+ * @throws SocketException if an I/O error occurs
|
|
|
+ */
|
|
|
+ public static InetAddress getLocalInetAddress(String host)
|
|
|
+ throws SocketException {
|
|
|
+ if (host == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ InetAddress addr = null;
|
|
|
+ try {
|
|
|
+ addr = InetAddress.getByName(host);
|
|
|
+ if (NetworkInterface.getByInetAddress(addr) == null) {
|
|
|
+ addr = null; // Not a local address
|
|
|
+ }
|
|
|
+ } catch (UnknownHostException ignore) { }
|
|
|
+ return addr;
|
|
|
+ }
|
|
|
}
|