java Source

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            14 Posts :: 24 Stories :: 8 Comments :: 0 Trackbacks

          2011年12月4日 #

          去除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 @ 2011-12-04 03:08 JustinLei 閱讀(466) | 評論 (0)編輯 收藏

          import java.io.InputStream;
          import java.net.Socket;

          /**
           * 
           * 
          @author ray (justinlei@gmail.com)
           * @date 2009-8-27
           * 
          @version 1.0
           
          */
          public class TimeUtil {
              
          public static final int DEFAULT_PORT = 37;
              
          public static final String DEFAULT_HOST = "time-nw.nist.gov";
              
          private TimeUtil() {
              };

              
          public static long currentTimeMillis(Boolean sync) {
                  
          if (sync != null && sync.booleanValue() != true)
                      
          return System.currentTimeMillis();
                  
          try {
                      
          return syncCurrentTime();
                  } 
          catch (Exception e) {
                      
          return System.currentTimeMillis();
                  }
              }

              
          public static long syncCurrentTime()  throws Exception {
                  
          // The time protocol sets the epoch at 1900,
                  
          // the java Date class at 1970. This number
                  
          // converts between them.
                  long differenceBetweenEpochs = 2208988800L;

                  
          // If you'd rather not use the magic number uncomment
                  
          // the following section which calculates it directly.

                  
          /*
                   * TimeZone gmt = TimeZone.getTimeZone("GMT"); Calendar epoch1900 =
                   * Calendar.getInstance(gmt); epoch1900.set(1900, 01, 01, 00, 00, 00);
                   * long epoch1900ms = epoch1900.getTime().getTime(); Calendar epoch1970
                   * = Calendar.getInstance(gmt); epoch1970.set(1970, 01, 01, 00, 00, 00);
                   * long epoch1970ms = epoch1970.getTime().getTime();
                   * 
                   * long differenceInMS = epoch1970ms - epoch1900ms; long
                   * differenceBetweenEpochs = differenceInMS/1000;
                   
          */

                  InputStream raw 
          = null;
                  
          try {
                      Socket theSocket 
          = new Socket(DEFAULT_HOST, DEFAULT_PORT);
                      raw 
          = theSocket.getInputStream();

                      
          long secondsSince1900 = 0;
                      
          for (int i = 0; i < 4; i++) {
                          secondsSince1900 
          = (secondsSince1900 << 8| raw.read();
                      }
                      
          if (raw != null)
                          raw.close();
                      
          long secondsSince1970 = secondsSince1900 - differenceBetweenEpochs;
                      
          long msSince1970 = secondsSince1970 * 1000;
                      
          return msSince1970;
                  } 
          catch (Exception e) {
                      
          throw new Exception(e);
                  }
              }
          }
          posted @ 2011-12-04 02:54 JustinLei 閱讀(1641) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 大安市| 江华| 阿克苏市| 元谋县| 高州市| 闽侯县| 昌图县| 务川| 页游| 长春市| 巴塘县| 石林| 珲春市| 祥云县| 宁河县| 盘锦市| 潍坊市| 徐闻县| 罗江县| 股票| 买车| 台东县| 思茅市| 张掖市| 关岭| 松潘县| 恩平市| 固镇县| 余姚市| 历史| 丹巴县| 乌兰察布市| 德惠市| 台湾省| 修水县| 大理市| 丰台区| 铅山县| 怀仁县| 武山县| 阿坝县|