diff options
Diffstat (limited to 'libjava/java/net/NetworkInterface.java')
-rw-r--r-- | libjava/java/net/NetworkInterface.java | 134 |
1 files changed, 64 insertions, 70 deletions
diff --git a/libjava/java/net/NetworkInterface.java b/libjava/java/net/NetworkInterface.java index f6d37489836..e7f7290febc 100644 --- a/libjava/java/net/NetworkInterface.java +++ b/libjava/java/net/NetworkInterface.java @@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. - + GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU @@ -41,6 +41,7 @@ import gnu.classpath.Configuration; import java.util.Enumeration; import java.util.Vector; + /** * This class models a network interface on the host computer. A network * interface contains a name (typically associated with a specific @@ -54,25 +55,22 @@ import java.util.Vector; public final class NetworkInterface { static - { - if (Configuration.INIT_LOAD_LIBRARY) - { - System.loadLibrary ("javanet"); - } - } + { + if (Configuration.INIT_LOAD_LIBRARY) + System.loadLibrary("javanet"); + } private String name; - private Vector inetAddresses; - private NetworkInterface (String name, InetAddress address) + private NetworkInterface(String name, InetAddress address) { this.name = name; - this.inetAddresses = new Vector (1, 1); - this.inetAddresses.add (address); + this.inetAddresses = new Vector(1, 1); + this.inetAddresses.add(address); } - private native static Vector getRealNetworkInterfaces () + private static native Vector getRealNetworkInterfaces() throws SocketException; /** @@ -80,45 +78,45 @@ public final class NetworkInterface * * @return The name of the interface. */ - public String getName () + public String getName() { return name; } /** * Returns all available addresses of the network interface - * + * * If a @see SecurityManager is available all addresses are checked * with @see SecurityManager::checkConnect() if they are available. - * Only <code>InetAddresses</code> are returned where the security manager + * Only <code>InetAddresses</code> are returned where the security manager * doesn't throw an exception. - * + * * @return An enumeration of all addresses. */ - public Enumeration getInetAddresses () + public Enumeration getInetAddresses() { - SecurityManager s = System.getSecurityManager (); + SecurityManager s = System.getSecurityManager(); if (s == null) - return inetAddresses.elements (); + return inetAddresses.elements(); - Vector tmpInetAddresses = new Vector (1, 1); + Vector tmpInetAddresses = new Vector(1, 1); - for (Enumeration addresses = inetAddresses.elements (); - addresses.hasMoreElements (); ) + for (Enumeration addresses = inetAddresses.elements(); + addresses.hasMoreElements();) { - InetAddress addr = (InetAddress) addresses.nextElement (); + InetAddress addr = (InetAddress) addresses.nextElement(); try { - s.checkConnect (addr.getHostAddress (), 58000); - tmpInetAddresses.add (addr); + s.checkConnect(addr.getHostAddress(), 58000); + tmpInetAddresses.add(addr); } catch (SecurityException e) { } - } + } - return tmpInetAddresses.elements (); + return tmpInetAddresses.elements(); } /** @@ -126,7 +124,7 @@ public final class NetworkInterface * * @return The display name of the interface */ - public String getDisplayName () + public String getDisplayName() { return name; } @@ -139,21 +137,20 @@ public final class NetworkInterface * @exception SocketException If an error occurs * @exception NullPointerException If the specified name is null */ - public static NetworkInterface getByName (String name) + public static NetworkInterface getByName(String name) throws SocketException { - Vector networkInterfaces = getRealNetworkInterfaces (); + Vector networkInterfaces = getRealNetworkInterfaces(); - for (Enumeration e = networkInterfaces.elements (); - e.hasMoreElements (); ) + for (Enumeration e = networkInterfaces.elements(); e.hasMoreElements();) { - NetworkInterface tmp = (NetworkInterface) e.nextElement (); - - if (name.equals (tmp.getName ())) - return tmp; + NetworkInterface tmp = (NetworkInterface) e.nextElement(); + + if (name.equals(tmp.getName())) + return tmp; } - throw new SocketException ("no network interface with this name exists"); + throw new SocketException("no network interface with this name exists"); } /** @@ -164,26 +161,25 @@ public final class NetworkInterface * @exception SocketException If an error occurs * @exception NullPointerException If the specified addess is null */ - public static NetworkInterface getByInetAddress (InetAddress addr) + public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException { - Vector networkInterfaces = getRealNetworkInterfaces (); - - for (Enumeration interfaces = networkInterfaces.elements (); - interfaces.hasMoreElements (); ) + Vector networkInterfaces = getRealNetworkInterfaces(); + + for (Enumeration interfaces = networkInterfaces.elements(); + interfaces.hasMoreElements();) { - NetworkInterface tmp = (NetworkInterface) interfaces.nextElement (); - - for (Enumeration addresses = tmp.inetAddresses.elements (); - addresses.hasMoreElements (); ) - { - if (addr.equals ((InetAddress) addresses.nextElement ())) - return tmp; - } + NetworkInterface tmp = (NetworkInterface) interfaces.nextElement(); + + for (Enumeration addresses = tmp.inetAddresses.elements(); + addresses.hasMoreElements();) + { + if (addr.equals((InetAddress) addresses.nextElement())) + return tmp; + } } - throw new SocketException ( - "no network interface is bound to such an IP address"); + throw new SocketException("no network interface is bound to such an IP address"); } /** @@ -191,8 +187,7 @@ public final class NetworkInterface * * @exception SocketException If an error occurs */ - public static Enumeration getNetworkInterfaces () - throws SocketException + public static Enumeration getNetworkInterfaces() throws SocketException { Vector networkInterfaces = getRealNetworkInterfaces(); @@ -206,44 +201,43 @@ public final class NetworkInterface * Checks if the current instance is equal to obj * * @param obj The object to compare with - */ - public boolean equals (Object obj) + */ + public boolean equals(Object obj) { - if (!(obj instanceof NetworkInterface)) + if (! (obj instanceof NetworkInterface)) return false; - + NetworkInterface tmp = (NetworkInterface) obj; - return (name.equals (tmp.name) - && inetAddresses.equals (tmp.inetAddresses)); + return (name.equals(tmp.name) && inetAddresses.equals(tmp.inetAddresses)); } /** * Returns the hashcode of the current instance */ - public int hashCode () + public int hashCode() { // FIXME: hash correctly - return name.hashCode () + inetAddresses.hashCode (); + return name.hashCode() + inetAddresses.hashCode(); } /** * Returns a string representation of the interface */ - public String toString () + public String toString() { // FIXME: check if this is correct String result; - String separator = System.getProperty ("line.separator"); + String separator = System.getProperty("line.separator"); - result = "name: " + getDisplayName () + " (" + getName () + - ") addresses:" + separator; + result = + "name: " + getDisplayName() + " (" + getName() + ") addresses:" + + separator; - for (Enumeration e = inetAddresses.elements (); - e.hasMoreElements (); ) + for (Enumeration e = inetAddresses.elements(); e.hasMoreElements();) { - InetAddress address = (InetAddress) e.nextElement (); - result += address.toString () + ";" + separator; + InetAddress address = (InetAddress) e.nextElement(); + result += address.toString() + ";" + separator; } return result; |