java Source

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            14 Posts :: 24 Stories :: 8 Comments :: 0 Trackbacks
          去除0.0.0.255和0.0.0.0
          import java.net.Inet4Address;
          import java.net.InetAddress;
          import java.util.ArrayList;
          import java.util.Arrays;
          import java.util.List;

          /**
           * 
          @author justinlei (justinlei@gmail.com)
           * @date Nov 17, 2011
           * 
          @version 1.0
           
          */
          public class InetAddressUtil {
              
          public static void main(String[] args) throws Exception {
                  Inet4Address start 
          = (Inet4Address)Inet4Address.getByName("172.20.0.254");
                  Inet4Address end 
          = (Inet4Address)Inet4Address.getByName("172.20.10.1");
                  List
          <byte[]> list = getRangeIps(null,start.getAddress(), end.getAddress());
                  
          for (byte[] bs : list) {
                      System.out.println(InetAddress.getByAddress(bs).getHostAddress());
                  }
              }
              
              
          public static List<byte[]> getRangeIps(List<byte[]> list,byte[] addrBytes,byte[] endBytes) throws Exception {
                  
          byte[] _b = new byte[addrBytes.length];
                  System.arraycopy(addrBytes, 
          0, _b, 0, addrBytes.length);
                  
          if(_b[3== -1)
                      _b[
          3= -2;
                  
          if(list == null) {
                      list 
          = new ArrayList<byte[]>();
                      list.add(_b);
                  }
                  
          if(list.size() == 5000)
                      
          return list;
                  
          if(addrBytes[2== -1 && addrBytes[3== -2)
                      
          return list;
                  
          if(Arrays.equals(addrBytes, endBytes)) {
                      
          return list;
                  }
                  
          int lastpart = addrBytes[addrBytes.length - 1];
                  
          if(lastpart != -2) {
                      addrBytes[addrBytes.length 
          - 1= (byte)(addrBytes[3+ 1);
                      _b 
          = new byte[addrBytes.length];
                      System.arraycopy(addrBytes, 
          0, _b, 0, addrBytes.length);
                      list.add(_b);
                  } 
          else if(lastpart == -2) {
                      addrBytes[addrBytes.length 
          - 1= 1;
                      list.add(plus(addrBytes, addrBytes.length 
          - 2));
                  }
          //        System.out.println(InetAddress.getByAddress(addrBytes).getHostAddress());
                  return getRangeIps(list,addrBytes, endBytes);
              }
              
              
          public static byte[] plus(byte[] addrBytes,int index) throws Exception {
                  
          if(index == 0)
                      
          return addrBytes;
                  
          int pluspart = addrBytes[index];
                  
          if(pluspart != -2) {
                      addrBytes[index] 
          = (byte)(pluspart + 1);
                      
          for (int i = index + 1; i < addrBytes.length - 1; i++) {
                          addrBytes[i] 
          = 0;
                      }
                      
          byte[] _b = new byte[addrBytes.length];
                      System.arraycopy(addrBytes, 
          0, _b, 0, addrBytes.length);
                      
          return _b;
                  } 
          else {
                      
          byte[] _b = new byte[addrBytes.length];
                      System.arraycopy(addrBytes, 
          0, _b, 0, addrBytes.length);
                      
          return plus(_b, index -1);
                  }
              }
          }
          posted on 2011-12-04 03:08 JustinLei 閱讀(466) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 赤峰市| 崇明县| 全椒县| 金寨县| 永城市| 桦甸市| 嘉荫县| 中卫市| 金门县| 喜德县| 望奎县| 郓城县| 岳普湖县| 铁岭市| 庄河市| 镇江市| 闵行区| 丰县| 化隆| 年辖:市辖区| 朔州市| 河东区| 葫芦岛市| 洛宁县| 鸡西市| 蓝田县| 仙桃市| 肃宁县| 吴旗县| 桦甸市| 尼玛县| 奉贤区| 左贡县| 芮城县| 卫辉市| 英吉沙县| 贵溪市| 封丘县| 忻城县| 西宁市| 阿尔山市|