//FileName:IpClassify.java
package xiaoqiang.main;
/**
* @author nKF65624
*
*/
public class IpClassify {
private String ip;
private String ipmask;
public IpClassify(String ip, String ipmask) {
this.ip = ip;
this.ipmask = ipmask;
}
public String[] getNetAndBoradAddr() {
// 设定A类子网位数最小为8
// 设定B类子网位数最小为16
// 设定C类子网位数最小为24
int netmaskcount = getNetBoradMask();
if (getSubNetKind() == 1) {
if (netmaskcount < 8) {
System.err.println("无效网络,A类网子网掩码需大于等于8位");
return(new String[]{"0.0.0.0","255.255.255.255"});
}else {
return(new String[]{getANetworkNumber(netmaskcount)[0],getANetworkNumber(netmaskcount)[1]});
}
} else if (getSubNetKind() == 2) {
if (netmaskcount < 16) {
System.err.println("无效网络,C类网子网掩码需大于等于16位");
return(new String[]{"0.0.0.0","255.255.255.255"});
}else {
return(new String[]{getANetworkNumber(netmaskcount)[0],getANetworkNumber(netmaskcount)[1]});
}
} else if (getSubNetKind() == 3) {
if (netmaskcount < 24) {
System.err.println("无效网络,C类网子网掩码需大于等于24位");
return(new String[]{"0.0.0.0","255.255.255.255"});
} else {
return(new String[]{getANetworkNumber(netmaskcount)[0],getANetworkNumber(netmaskcount)[1]});
}
}
else {
return(new String[]{"0.0.0.0","255.255.255.255"});
}
}
public String[] getANetworkNumber(int netmaskcount) {
char[] mybit= { '0', '0', '0', '0', '0', '0', '0', '0' };
int[] inIpArray = getStrBlock(ip);
if(netmaskcount>=24)
{
for (int i = 0; i < (netmaskcount - 24); i++) {
mybit[i] = '1';
}
String mystr="";
for(int i=0;i<mybit.length;i++)
{
mystr=mystr+mybit[i];
}
String returnStrnetwork=inIpArray[0]+"."+inIpArray[1]+"."+inIpArray[2];
String returnStrBoardAddr="";
int mytemp=256-Integer.parseInt(mystr, 2);
int temp=inIpArray[3]/mytemp;
returnStrBoardAddr=returnStrnetwork+"."+String.valueOf(mytemp*(temp+1)-1);
returnStrnetwork=returnStrnetwork+"."+(temp)*mytemp;
return new String[]{returnStrnetwork,returnStrBoardAddr};
}
else if(netmaskcount>=16)
{
for (int i = 0; i < (netmaskcount - 16); i++) {
mybit[i] = '1';
}
String mystr="";
for(int i=0;i<mybit.length;i++)
{
mystr=mystr+mybit[i];
}
String returnStrnetwork=inIpArray[0]+"."+inIpArray[1];
String returnStrBoardAddr="";
int mytemp=256-Integer.parseInt(mystr, 2);
int temp=inIpArray[2]/mytemp;
returnStrBoardAddr=returnStrnetwork+"."+String.valueOf(mytemp*(temp+1)-1)+".255";
returnStrnetwork=returnStrnetwork+"."+(temp)*mytemp+".0";
return new String[]{returnStrnetwork,returnStrBoardAddr};
}
else
{
for (int i = 0; i < (netmaskcount - 8); i++) {
mybit[i] = '1';
}
String mystr="";
for(int i=0;i<mybit.length;i++)
{
mystr=mystr+mybit[i];
}
String returnStrnetwork=String.valueOf(inIpArray[0]);
String returnStrBoardAddr="";
int mytemp=256-Integer.parseInt(mystr, 2);
int temp=inIpArray[2]/mytemp;
returnStrBoardAddr=returnStrnetwork+"."+String.valueOf(mytemp*(temp+1)-1)+".255.255";
returnStrnetwork=returnStrnetwork+"."+(temp)*mytemp+".0.0";
return new String[]{returnStrnetwork,returnStrBoardAddr};
}
}
public String[] getBNetworkNumber(int netmaskcount) {
char[] mybit= { '0', '0', '0', '0', '0', '0', '0', '0' };
int[] inIpArray = getStrBlock(ip);
if(netmaskcount>=24)
{
for (int i = 0; i < (netmaskcount - 24); i++) {
mybit[i] = '1';
}
String mystr="";
for(int i=0;i<mybit.length;i++)
{
mystr=mystr+mybit[i];
}
String returnStrnetwork=inIpArray[0]+"."+inIpArray[1]+"."+inIpArray[2];
String returnStrBoardAddr="";
int mytemp=256-Integer.parseInt(mystr, 2);
int temp=inIpArray[3]/mytemp;
returnStrBoardAddr=returnStrnetwork+"."+String.valueOf(mytemp*(temp+1)-1);
returnStrnetwork=returnStrnetwork+"."+(temp)*mytemp;
return new String[]{returnStrnetwork,returnStrBoardAddr};
}
else
{
for (int i = 0; i < (netmaskcount - 16); i++) {
mybit[i] = '1';
}
String mystr="";
for(int i=0;i<mybit.length;i++)
{
mystr=mystr+mybit[i];
}
String returnStrnetwork=inIpArray[0]+"."+inIpArray[1];
String returnStrBoardAddr="";
int mytemp=256-Integer.parseInt(mystr, 2);
int temp=inIpArray[2]/mytemp;
returnStrBoardAddr=returnStrnetwork+"."+String.valueOf(mytemp*(temp+1)-1)+".255";
returnStrnetwork=returnStrnetwork+"."+(temp)*mytemp+".0";
return new String[]{returnStrnetwork,returnStrBoardAddr};
}
}
public String[] getCNetworkNumber(int netmaskcount) {
char[] mybit= { '0', '0', '0', '0', '0', '0', '0', '0' };
for (int i = 0; i < (netmaskcount - 24); i++) {
mybit[i] = '1';
}
String mystr="";
for(int i=0;i<mybit.length;i++)
{
mystr=mystr+mybit[i];
}
int[] inIpArray = getStrBlock(ip);
String returnStrnetwork=inIpArray[0]+"."+inIpArray[1]+"."+inIpArray[2];
String returnStrBoardAddr="";
int mytemp=256-Integer.parseInt(mystr, 2);
int temp=inIpArray[3]/mytemp;
returnStrBoardAddr=returnStrnetwork+"."+String.valueOf(mytemp*(temp+1)-1);
returnStrnetwork=returnStrnetwork+"."+(temp)*mytemp;
return new String[]{returnStrnetwork,returnStrBoardAddr};
}
/**
* @function 得到网络类型(A,B,C,其他)1-A,2-B,3-C,0-其他
* @return
*/
public int getSubNetKind() {
if (getStrBlock(ip)[0] < 128) {
return 1;
} else if (getStrBlock(ip)[0] < 192) {
return 2;
} else if (getStrBlock(ip)[0] < 224) {
return 3;
} else {
return 0;
}
}
/**
* @function 得到子网掩码位数
* @return
*/
public int getNetBoradMask() {
int count = 0;
int[] maskBlockArray = getStrBlock(ipmask);
for (int i = 0; i < maskBlockArray.length; i++) {
count = count + getBit(maskBlockArray[i]);
}
return count;
}
/**
* @function 将数值转化为2进制
* @param num
* @return
*/
private int getBit(int num) {
int count = 0;
String strBit = Integer.toBinaryString(num);
char[] charBit = strBit.toCharArray();
// 判断子网掩码的有限性
for (int i = 0; i < charBit.length; i++) {
if (charBit[i] == '0') {
int ss = 0;
for (int j = i; j < charBit.length; j++) {
ss = ss + Integer.valueOf(String.valueOf(charBit[j]));
}
if (ss > 0) {
System.err.println(num + "子网掩码错误");
return count;
} else {
return count;
}
}
if (charBit[i] == '1') {
count++;
}
}
return count;
}
/**
* @function 分割ip或者子网掩码为四段,返回int数组
* @param mystr
* @return
*/
private int[] getStrBlock(String mystr) {
int[] myArray = new int[4];
String strReplace = mystr.replace('.', '-');
String[] myArrayStr = strReplace.split("-");
for (int i = 0; i < myArrayStr.length; i++) {
myArray[i] = Integer.valueOf(myArrayStr[i]);
}
return myArray;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getIpmask() {
return ipmask;
}
public void setIpmask(String ipmask) {
this.ipmask = ipmask;
}
}
//Junit4用例
//Filename:TestIpClassify.java
package com.xiaoqiang.ut;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.junit.BeforeClass;
import org.junit.Test;
import xiaoqiang.main.IpClassify;
public class TestIpClassify {
@BeforeClass
public static void init() {
}
@Test
public void testprivateIp() throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
IpClassify a = new IpClassify("10.10.10.10", "255.255.255.0");
Field f = a.getClass().getDeclaredField("ip");
f.setAccessible(true);
String ip = (String) f.get(a);
assertTrue(ip.equals("10.10.10.10"));
}
@Test
public void testprivateMask() throws SecurityException,
NoSuchFieldException, IllegalArgumentException,
IllegalAccessException {
IpClassify a = new IpClassify("10.10.10.10", "255.255.255.0");
Field f = a.getClass().getDeclaredField("ipmask");
f.setAccessible(true);
String ip = (String) f.get(a);
assertTrue(ip.equals("255.255.255.0"));
}
@Test
public void testNew() {
IpClassify a = new IpClassify("10.10.10.10", "255.255.255.0");
assertNotNull(a);
}
@Test
public void testgetANetworkNumber() throws Exception {
String[] myIp = { "10.10.10.10,255.255.255.0",
"10.10.10.10,255.255.0.0", "10.10.10.10,255.0.0.0",
"10.148.54.175,255.255.254.0", "10.10.10.195,255.255.255.128" };
String[] myNetNum = { "10.10.10.0,10.10.10.255",
"10.10.0.0,10.10.255.255", "10.0.0.0,10.255.255.255",
"10.148.54.0,10.148.55.255", "10.10.10.128,10.10.10.255" };
for (int i = 0; i < myIp.length; i++) {
IpClassify a = new IpClassify(myIp[i].split(",")[0], myIp[i].split(",")[1]);
Method mo = a.getClass().getDeclaredMethod("getBit", int.class);
mo.setAccessible(true);
Integer countA[] = { (Integer) mo.invoke(a, 255),
(Integer) mo.invoke(a, 255), (Integer) mo.invoke(a, 255),
(Integer) mo.invoke(a, 0) };
String[] myNetworkNum = a.getANetworkNumber(countA[0] + countA[1]
+ countA[2] + countA[3]);
assertTrue(myNetworkNum[0].trim().equals(myNetNum[i].split(",")[0].trim()));
System.out.println(myNetworkNum[0] +" \t"+myNetNum[i].split(",")[0]);
// assertTrue(myNetworkNum[1].trim().equals(myNetNum[i].split(",")[1].trim()));
// System.out.println(myNetworkNum[1] +" \t"+myNetNum[i].split(",")[1]);
}
}
@Test
public void testPrivategetBit() {
try {
IpClassify a = new IpClassify("10.10.10.10", "255.255.255.0");
Method mo = a.getClass().getDeclaredMethod("getBit", int.class);
mo.setAccessible(true);
int myNum[] = { 0, 128, 192, 224, 240, 248, 252, 254, 255 };
for (int i = 0; i < myNum.length; i++) {
Object obj = mo.invoke(a, myNum[i]);
Integer count = (Integer) obj;
assertTrue(i == count);
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
@Test
public void testgetip() {
IpClassify a = new IpClassify("10.10.10.10", "255.255.255.0");
assertTrue(a.getIp().equals("10.10.10.10"));
}
@Test
public void testgetmask() {
IpClassify a = new IpClassify("10.10.10.10", "255.255.255.0");
assertTrue(a.getIpmask().equals("255.255.255.0"));
}
@Test
public void testprivategetStrBlock() {
try {
IpClassify a = new IpClassify("10.10.10.10", "255.255.255.0");
Method mo = a.getClass().getDeclaredMethod("getStrBlock",
Class.forName("java.lang.String"));
mo.setAccessible(true);
Object obj = mo.invoke(a, "10.10.10.10");
int[] myip = (int[]) obj;
for (int i = 0; i < myip.length; i++) {
assertTrue(myip[i] == 10);
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
@Test
public void testgetSubNetKind() {
IpClassify a0 = new IpClassify("0.10.10.10", "255.255.255.0");
IpClassify a1 = new IpClassify("10.10.10.10", "255.255.255.0");
IpClassify a2 = new IpClassify("127.10.10.10", "255.255.255.0");
IpClassify b0 = new IpClassify("128.125.14.54", "255.255.255.0");
IpClassify b1 = new IpClassify("172.125.14.54", "255.255.255.0");
IpClassify b2 = new IpClassify("191.125.14.54", "255.255.255.0");
IpClassify c0 = new IpClassify("192.168.1.1", "255.255.255.0");
IpClassify c1 = new IpClassify("193.168.1.1", "255.255.255.0");
IpClassify c2 = new IpClassify("223.168.1.1", "255.255.255.0");
IpClassify t0 = new IpClassify("224.168.1.1", "255.255.255.0");
IpClassify t1 = new IpClassify("235.168.1.1", "255.255.255.0");
IpClassify t2 = new IpClassify("255.168.1.1", "255.255.255.0");
assertEquals(a0.getSubNetKind(), 1);
assertEquals(a1.getSubNetKind(), 1);
assertEquals(a2.getSubNetKind(), 1);
assertEquals(b0.getSubNetKind(), 2);
assertEquals(b1.getSubNetKind(), 2);
assertEquals(b2.getSubNetKind(), 2);
assertEquals(c0.getSubNetKind(), 3);
assertEquals(c1.getSubNetKind(), 3);
assertEquals(c2.getSubNetKind(), 3);
assertEquals(t0.getSubNetKind(), 0);
assertEquals(t1.getSubNetKind(), 0);
assertEquals(t2.getSubNetKind(), 0);
}
@Test
public void testsetip() {
IpClassify a = new IpClassify("10.10.10.10", "255.255.255.0");
a.setIp("10.10.10.13");
assertTrue(a.getIp().equals("10.10.10.13"));
}
@Test
public void testsetmask() {
IpClassify a = new IpClassify("10.10.10.10", "255.255.255.0");
a.setIpmask("255.255.254.0");
assertTrue(a.getIpmask().equals("255.255.254.0"));
}
}