FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

基础类库重构 · sherlock111/java-code@c8d3dd9 · GitHub

Repository navigation

Commit c8d3dd9

Browse files
committed
基础类库重构
1 parent f179d5d commit c8d3dd9

3 files changed

Lines changed: 92 additions & 13 deletions

File tree

‎basicKnowledge/src/com/xdc/basic/commons/network/IpUtil.java‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package com.xdc.basic.commons.network;
22

3+
import java.net.Inet4Address;
4+
import java.net.InetAddress;
5+
import java.net.NetworkInterface;
6+
import java.net.SocketException;
7+
import java.util.ArrayList;
8+
import java.util.Enumeration;
9+
import java.util.List;
10+
import java.util.TreeSet;
11+
312
import org.apache.commons.lang3.StringUtils;
413
import org.apache.commons.lang3.math.NumberUtils;
514

@@ -140,4 +149,40 @@ private static int ipToInt(String ipString)
140149

141150
return ipInt;
142151
}
152+
153+
public static List<String> getIps() throws SocketException
154+
{
155+
// 使用TreeSet:保证唯一,保证升序。
156+
TreeSet<String> ipTreeSet = new TreeSet<String>();
157+
158+
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
159+
while (networkInterfaces.hasMoreElements())
160+
{
161+
NetworkInterface networkInterface = networkInterfaces.nextElement();
162+
if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp())
163+
{
164+
continue;
165+
}
166+
167+
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
168+
while (inetAddresses.hasMoreElements())
169+
{
170+
InetAddress inetAddress = inetAddresses.nextElement();
171+
if (!(inetAddress instanceof Inet4Address))
172+
{
173+
continue;
174+
}
175+
176+
ipTreeSet.add(inetAddress.getHostAddress());
177+
}
178+
}
179+
180+
List<String> ipList = new ArrayList<String>();
181+
for (String ip : ipTreeSet)
182+
{
183+
ipList.add(ip);
184+
}
185+
186+
return ipList;
187+
}
143188
}

‎basicKnowledge/src/com/xdc/basic/commons/network/MacUtil.java‎

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
import java.util.ArrayList;
66
import java.util.Enumeration;
77
import java.util.List;
8+
import java.util.TreeSet;
89

9-
import org.apache.commons.lang3.StringUtils;
10+
import org.apache.commons.lang3.ArrayUtils;
11+
12+
import com.xdc.basic.commons.StringUtil;
1013

1114
public class MacUtil
1215
{
@@ -32,40 +35,69 @@ public static boolean isMac(String mac)
3235

3336
public static boolean isMac(String mac, boolean filterSpecialMac)
3437
{
35-
if (StringUtils.isBlank(mac))
38+
if (!StringUtil.matches(mac, macRegExp))
3639
{
3740
return false;
3841
}
3942

40-
if (filterSpecialMac)
43+
if (filterSpecialMac && specialMacs.contains(mac))
4144
{
42-
if (specialMacs.contains(mac))
43-
{
44-
return false;
45-
}
45+
return false;
4646
}
4747

48-
return mac.matches(macRegExp);
48+
return true;
4949
}
5050

5151
public static List<String> getMacs() throws SocketException
5252
{
5353
return getMacs(true);
5454
}
5555

56+
/**
57+
* 参考:http://www.jb51.net/article/93343.htm
58+
* 参考:http://www.cnblogs.com/KnowledgeShare/p/6184383.html
59+
*/
5660
public static List<String> getMacs(boolean filterSpecialMac) throws SocketException
5761
{
58-
// TODO 待实现
59-
60-
List<String> macs = new ArrayList<String>();
62+
// 使用TreeSet:保证唯一,保证升序。
63+
TreeSet<String> macTreeSet = new TreeSet<String>();
6164

6265
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
6366
while (networkInterfaces.hasMoreElements())
6467
{
6568
NetworkInterface networkInterface = networkInterfaces.nextElement();
66-
byte[] hardwareAddress = networkInterface.getHardwareAddress();
69+
if (networkInterface.isLoopback() || networkInterface.isVirtual())
70+
{
71+
continue;
72+
}
73+
74+
byte[] macBytes = networkInterface.getHardwareAddress();
75+
if (ArrayUtils.isEmpty(macBytes))
76+
{
77+
continue;
78+
}
79+
80+
StringBuilder sb = new StringBuilder();
81+
for (int i = 0; i < macBytes.length; i++)
82+
{
83+
sb.append(String.format("%02X%s", macBytes[i], (i < macBytes.length - 1) ? "-" : ""));
84+
}
85+
String mac = sb.toString();
86+
87+
if (filterSpecialMac && specialMacs.contains(mac))
88+
{
89+
continue;
90+
}
91+
92+
macTreeSet.add(mac);
93+
}
94+
95+
List<String> macList = new ArrayList<String>();
96+
for (String mac : macTreeSet)
97+
{
98+
macList.add(mac);
6799
}
68100

69-
return macs;
101+
return macList;
70102
}
71103
}

‎basicKnowledge/test/com/xdc/basic/commons/network/IpUtilTest.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.xdc.basic.commons.network;
22

33
import org.junit.Assert;
4+
import org.junit.Ignore;
45
import org.junit.Test;
56

67
public class IpUtilTest
@@ -33,6 +34,7 @@ public void testIsIpv4Mask()
3334
Assert.assertEquals(false, IpUtil.isIpv4Ip("0.0.0.0hehe"));
3435
}
3536

37+
@Ignore("比较耗时,不执行了。")
3638
@Test
3739
public void testIsIpv4PortAndIsIpv4PortRegex()
3840
{

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL