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 閱讀(472) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 宁德市| 丰镇市| 龙州县| 昌邑市| 宜章县| 常宁市| 远安县| 克东县| 江达县| 岳阳市| 喀喇沁旗| 汕头市| 阿拉尔市| 桐庐县| 始兴县| 新余市| 江川县| 区。| 永修县| 长葛市| 扶沟县| 南昌市| 景德镇市| 江口县| 克什克腾旗| 定结县| 金昌市| 时尚| 靖边县| 大关县| 太和县| 九龙城区| 宜川县| 四子王旗| 马鞍山市| 泸水县| 康平县| 荆州市| 民和| 紫金县| 奉节县|