38 |
|
|
39 |
package java.net; |
package java.net; |
40 |
|
|
41 |
|
import java.util.Collection; |
42 |
|
import java.util.Collections; |
43 |
import java.util.Enumeration; |
import java.util.Enumeration; |
44 |
|
import java.util.HashMap; |
45 |
|
import java.util.Iterator; |
46 |
|
import java.util.Map; |
47 |
import java.util.Vector; |
import java.util.Vector; |
48 |
|
|
49 |
/** |
/** |
148 |
public static NetworkInterface getByName(String name) |
public static NetworkInterface getByName(String name) |
149 |
throws SocketException |
throws SocketException |
150 |
{ |
{ |
151 |
Vector networkInterfaces = VMNetworkInterface.getInterfaces(); |
for (Enumeration e = getNetworkInterfaces(); e.hasMoreElements();) |
|
|
|
|
for (Enumeration e = networkInterfaces.elements(); e.hasMoreElements();) |
|
152 |
{ |
{ |
153 |
NetworkInterface tmp = (NetworkInterface) e.nextElement(); |
NetworkInterface tmp = (NetworkInterface) e.nextElement(); |
154 |
|
|
173 |
public static NetworkInterface getByInetAddress(InetAddress addr) |
public static NetworkInterface getByInetAddress(InetAddress addr) |
174 |
throws SocketException |
throws SocketException |
175 |
{ |
{ |
176 |
Vector networkInterfaces = VMNetworkInterface.getInterfaces(); |
for (Enumeration interfaces = getNetworkInterfaces(); |
|
|
|
|
for (Enumeration interfaces = networkInterfaces.elements(); |
|
177 |
interfaces.hasMoreElements();) |
interfaces.hasMoreElements();) |
178 |
{ |
{ |
179 |
NetworkInterface tmp = (NetworkInterface) interfaces.nextElement(); |
NetworkInterface tmp = (NetworkInterface) interfaces.nextElement(); |
189 |
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"); |
190 |
} |
} |
191 |
|
|
192 |
|
static private Collection condense(Collection interfaces) |
193 |
|
{ |
194 |
|
final Map condensed = new HashMap(); |
195 |
|
|
196 |
|
final Iterator interfs = interfaces.iterator(); |
197 |
|
while (interfs.hasNext()) { |
198 |
|
|
199 |
|
final NetworkInterface face = (NetworkInterface) interfs.next(); |
200 |
|
final String name = face.getName(); |
201 |
|
|
202 |
|
if (condensed.containsKey(name)) |
203 |
|
{ |
204 |
|
final NetworkInterface conface = (NetworkInterface) condensed.get(name); |
205 |
|
if (!conface.inetAddresses.containsAll(face.inetAddresses)) |
206 |
|
{ |
207 |
|
final Iterator faceAddresses = face.inetAddresses.iterator(); |
208 |
|
while (faceAddresses.hasNext()) |
209 |
|
{ |
210 |
|
final InetAddress faceAddress = (InetAddress) faceAddresses.next(); |
211 |
|
if (!conface.inetAddresses.contains(faceAddress)) |
212 |
|
{ |
213 |
|
conface.inetAddresses.add(faceAddress); |
214 |
|
} |
215 |
|
} |
216 |
|
} |
217 |
|
} |
218 |
|
else |
219 |
|
{ |
220 |
|
condensed.put(name, face); |
221 |
|
} |
222 |
|
} |
223 |
|
|
224 |
|
return condensed.values(); |
225 |
|
} |
226 |
|
|
227 |
/** |
/** |
228 |
* Return an <code>Enumeration</code> of all available network interfaces |
* Return an <code>Enumeration</code> of all available network interfaces |
229 |
* |
* |
238 |
if (networkInterfaces.isEmpty()) |
if (networkInterfaces.isEmpty()) |
239 |
return null; |
return null; |
240 |
|
|
241 |
return networkInterfaces.elements(); |
Collection condensed = condense(networkInterfaces); |
242 |
|
|
243 |
|
return Collections.enumeration(condensed); |
244 |
} |
} |
245 |
|
|
246 |
/** |
/** |