<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    主站蜘蛛池模板: 成人一a毛片免费视频| 最近高清中文字幕免费| 国产无遮挡吃胸膜奶免费看视频| 亚洲精品中文字幕乱码影院| 无码人妻久久一区二区三区免费 | 国产h视频在线观看免费| 亚洲国色天香视频| 我要看免费的毛片| 精品国产_亚洲人成在线| 亚洲国产精品成人久久蜜臀 | 国产成人免费AV在线播放| 久久精品亚洲综合专区| 全部免费毛片在线播放| 亚洲综合激情另类小说区| 麻豆一区二区免费播放网站| 亚洲欧美成人综合久久久| 免费久久精品国产片香蕉| 一级做a爱过程免费视频高清| 亚洲日韩v无码中文字幕| 未满十八18禁止免费无码网站| 亚洲视频在线观看不卡| 成人毛片手机版免费看| 免费夜色污私人影院网站电影| 亚洲欧洲日产国码av系列天堂| 特级精品毛片免费观看| 亚洲免费福利在线视频| xvideos亚洲永久网址| 久久精品免费观看| 亚洲中文字幕一区精品自拍| 国产成人精品男人免费| 大地资源网高清在线观看免费| 亚洲欧洲日本国产| 国产伦一区二区三区免费| 免费无码作爱视频| 亚洲精品国产精品| 国产国拍亚洲精品mv在线观看| 成人免费视频77777| 国产99视频精品免费视频76| 亚洲精品综合久久中文字幕| 免费一看一级毛片全播放| 91人人区免费区人人|