java Source

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            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 閱讀(465) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 札达县| 湖北省| 普格县| 正蓝旗| 江山市| 山西省| 高阳县| 高州市| 米林县| 吉木萨尔县| 舟曲县| 阿城市| 明光市| 武城县| 乃东县| 满城县| 务川| 娱乐| 泸水县| 绥德县| 定结县| 金沙县| 保康县| 灌南县| 高碑店市| 黔南| 岳池县| 北安市| 秦安县| 和平县| 景谷| 台州市| 石家庄市| 犍为县| 绥芬河市| 浦北县| 新竹县| 项城市| 措美县| 石林| 阜新市|