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

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

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

    java Source

      BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
      14 Posts :: 24 Stories :: 8 Comments :: 0 Trackbacks

    2010年4月6日 #

    去除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 閱讀(469) | 評(píng)論 (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 閱讀(1643) | 評(píng)論 (0)編輯 收藏

    1.Library:RXTXComm.jar



    /*

     * GPSSerialPortScaner.java
     * Copyright (C) 2009  <JustinLei@gmail.com>
     *
     *        This program is free software; you can redistribute it and/or modify
     *        it under the terms of the GNU General Public License as published by
     *      the Free Software Foundation; either version 2 of the License, or
     *     (at your option) any later version.
     *
     *       This program is distributed in the hope that it will be useful,
     *      but WITHOUT ANY WARRANTY; without even the implied warranty of
     *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *        GNU General Public License for more details.
     *
     
    */
    package org.lambdasoft.serial.gps;
    import gnu.io.CommPort;
    import gnu.io.CommPortIdentifier;
    import gnu.io.PortInUseException;
    import gnu.io.SerialPort;
    import gnu.io.UnsupportedCommOperationException;

    import java.io.InputStream;
    import java.util.Enumeration;

    import org.lambdasoft.ui.dialog.BubbleDialog;
    import org.lambdasoft.ui.systray.statusbar.StatusBar;
    import org.lambdasoft.util.StringUtils;

    /**
     * 
    @author lei.tang (justinlei@gmail.com)
     * @date 2010-3-8
     * 
    @version
     
    */
    public class GPSSerialPortScaner {
        
    private static GPSSerialPortScaner finder;
        
    private static final int SCANTIME = 2000;
        
    private static final int[] PORTSPEED = new int[]{4800,9600};
        
    private GPSSerialPortScaner() {}
        
        
    public static GPSSerialPortScaner getScaner() {
            
    if(finder == null)
                finder 
    = new GPSSerialPortScaner();
            
    return finder;
        }

        
    public String scan() {
            BubbleDialog.getBubble().display(
    "GPS設(shè)備自動(dòng)搜索中",false,false);
            
    for (int speed : PORTSPEED) {
                String port 
    = scan(speed);
                
    if(StringUtils.isNotEmpty(port))
                    
    return port;
            }
            
    return null;
        }
        
        @SuppressWarnings(
    "unchecked")
        
    public String scan(Integer speed) {
            StatusBar statusBar 
    = StatusBar.getBar();
            Enumeration
    <CommPortIdentifier> commons = CommPortIdentifier.getPortIdentifiers();
            CommPortIdentifier com 
    = null;
            CommPort serialPort 
    = null;
            
    while (commons.hasMoreElements()) {
                com 
    = commons.nextElement();
                
    if(statusBar != null)
                    statusBar.setGPSInfo(
    "掃描GPS設(shè)備:" + com.getName() + " 掃描速率:" + speed);
                
    try {
                    serialPort 
    = com.open("PortOpener"10);
                } 
    catch (PortInUseException piue) {
                    serialPort 
    = null;
                    
    continue;
                }
                
    if (com.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                    SerialPort sp 
    = (SerialPort) serialPort;
                    
    try {
                        
    // Settings for B&G Hydra
                        sp.setSerialPortParams(speed.intValue(), SerialPort.DATABITS_8,
                                SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                    } 
    catch (UnsupportedCommOperationException ucoe) {
                        serialPort 
    = null;
                        
    continue;
                    }
                }
                
    try {
                    InputStream input 
    = serialPort.getInputStream();
                    
    int readChar = -1;
                    
    long currentTime = System.currentTimeMillis();
                    
    while((System.currentTimeMillis() - currentTime) <= SCANTIME) {
                        
    if((readChar = input.read()) == -1) {
                            
    continue;
                        }
                        
    if((char)readChar == '$') {
                            StringBuilder builder 
    = new StringBuilder();
                            
    for (int j = 0; j < 5; j++) {
                                builder.append((
    char)(input.read()));
                            }
                            String head 
    = builder.toString();
                            
    if(head.equals("GPGGA")) {
                                serialPort.close();
                                
    if(statusBar != null) {
                                    String successMsg 
    = "GPS設(shè)備在端口" + com.getName() +"初始化成功,速率: " + speed;
                                    BubbleDialog.getBubble().display(successMsg,
    false,true);
                                    statusBar.setGPSInfo(successMsg);
                                }
                                
    return com.getName();
                            }
                        }
                    }
                } 
    catch (Exception e) {
                    
    return null;
                }
                serialPort.close();
            }
            
    if(serialPort != null)
                serialPort.close();
            
    return null;
        }
    }
    posted @ 2010-04-06 16:47 JustinLei 閱讀(1500) | 評(píng)論 (0)編輯 收藏

    /*
     * DevicesAutoRegister.java
     * Copyright (C) 2009  <JustinLei@gmail.com>
     *
     *        This program is free software; you can redistribute it and/or modify
     *        it under the terms of the GNU General Public License as published by
     *      the Free Software Foundation; either version 2 of the License, or
     *     (at your option) any later version.
     *
     *       This program is distributed in the hope that it will be useful,
     *      but WITHOUT ANY WARRANTY; without even the implied warranty of
     *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *        GNU General Public License for more details.
     *
     
    */
    package org.lambdasoft.device.jmf;

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;

    import org.lambdasoft.ui.dialog.BubbleDialog;

    import com.sun.media.util.Registry;

    /**
     * 
    @author lei.tang (justinlei@gmail.com)
     * @date 
     * 
    @version
     
    */
    public class DevicesAutoRegister {
        
    private static DevicesAutoRegister autoRegister;
        
        
    private DevicesAutoRegister() {
        }
         
        
    public static final DevicesAutoRegister getRegister() {
            
    if(autoRegister == null)
                autoRegister 
    = new DevicesAutoRegister();
            
    return autoRegister;
        }
        
        
    public boolean regist(String args[]) throws IOException {
    //查找設(shè)備注冊(cè)文件在jmf.jar同級(jí)目錄
            File jarFile 
    = new File(Registry.class.getProtectionDomain().getCodeSource().getLocation().getFile());
            File confFile 
    = new File(jarFile.getParent() + File.separator + "jmf.properties");
            
    if(confFile.exists()) {
                
    return true;
            }
            writeConfigOrig(confFile);
            
    return _regist(args == null ? new String[0] : args);
        }
        
        
    private boolean _regist(String args[]){
            BubbleDialog.getBubble().display(
    "沒(méi)有找到設(shè)備,開(kāi)始注冊(cè)視頻采集設(shè)備",false,false);
            JMFInit jmfInit 
    = new JMFInit(args);
            
    boolean flag = jmfInit.isInitialError();
            jmfInit.dispose();
            
    if(flag != true)
                BubbleDialog.getBubble().display(
    "視頻采集設(shè)備注冊(cè)完畢",false,true);
            
    else 
                BubbleDialog.getBubble().display(
    "視頻采集設(shè)備注冊(cè)失敗,程序?qū)⑼顺?/span>",true,true);
            
    return !flag;
        }
        
        //寫(xiě)入空的注冊(cè)文件(JMF Configure Serializable Object)
        
    private void writeConfigOrig(File file) throws IOException{
            InputStream is 
    = DevicesAutoRegister.class.getResourceAsStream("jmf.properties.orig");
            
    byte[] buff = new byte[1024];
            FileOutputStream os 
    = new FileOutputStream(file);
            
    while(is.read(buff) != -1) {
                os.write(buff);
            }
            os.flush();
            os.close();
            is.close();
        }
    }
    posted @ 2010-04-06 16:35 JustinLei 閱讀(1392) | 評(píng)論 (0)編輯 收藏

    package org.lambdasoft.http;

    import java.util.Map;

    /**
     * 
    @author lei.tang (justinlei@gmail.com)
     * @date 
     * 
    @version
     
    */
    public interface HttpRequest {
        String execute(String url,Map
    <String, String> params) throws Exception;
    }

    package org.lambdasoft.http;

    import java.util.Map;
    import java.util.Set;

    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.commons.httpclient.methods.GetMethod;

    /**
     * 
    @author lei.tang (justinlei@gmail.com)
     * @date 
     * 
    @version
     
    */
    public class GetRequest implements HttpRequest{

        @Override
        
    public String execute(String url, Map<String, String> params) throws Exception {
            HttpClient client 
    = new HttpClient();
            GetMethod getMethod 
    = new GetMethod(url);
            
    if(params != null && params.size() != 0) {
                Set
    <String> keySet = params.keySet();
                NameValuePair[] nameValuePairs 
    = new NameValuePair[params.size()];
                
    int i = 0;
                
    for (String key : keySet) {
                    nameValuePairs[i] 
    = new NameValuePair(key, params.get(key));
                    i
    ++;
                }
                getMethod.setQueryString(nameValuePairs);
            }
            
    int statusCode = client.executeMethod(getMethod);
            
    if (statusCode != HttpStatus.SC_OK) {
                
    throw new Exception(getMethod.getStatusLine().toString());
            }
            
    return new String(getMethod.getResponseBody());
        }
    }




    package org.lambdasoft.http;

    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;

    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.HttpStatus;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.commons.httpclient.methods.PostMethod;

    /**
     * 
    @author lei.tang (justinlei@gmail.com)
     * @date 
     * 
    @version
     
    */
    public class PostRequest implements HttpRequest{

        @Override
        
    public String execute(String url, Map<String, String> params)
                
    throws Exception {
            HttpClient client 
    = new HttpClient();
            PostMethod postMethod 
    = new PostMethod(url);
            
            
    if(params != null && params.size() != 0) {
                Set
    <String> keySet = params.keySet();
                NameValuePair[] nameValuePairs 
    = new NameValuePair[params.size()];
                
    int i = 0;
                
    for (String key : keySet) {
                    nameValuePairs[i] 
    = new NameValuePair(key, params.get(key));
                    i
    ++;
                }
                postMethod.setQueryString(nameValuePairs);
            }
            
    int statusCode = client.executeMethod(postMethod);
            
    if (statusCode != HttpStatus.SC_OK) {
                
    throw new Exception(postMethod.getStatusLine().toString());
            }
            
    return new String(postMethod.getResponseBody());
        }

    }
    posted @ 2010-04-06 16:29 JustinLei 閱讀(2266) | 評(píng)論 (2)編輯 收藏

    主站蜘蛛池模板: 亚洲福利视频导航| 日韩a级毛片免费观看| 久久精品国产精品亚洲下载| 91亚洲性爱在线视频| 国产男女爽爽爽免费视频 | 国产亚洲精品自在线观看| 亚洲AV日韩综合一区尤物| 222www免费视频| 亚洲成AV人片在| 大地资源中文在线观看免费版| 亚洲AV无码乱码精品国产| 亚洲av日韩精品久久久久久a| 中文字幕无码视频手机免费看 | 成人免费无码大片a毛片| 亚洲日韩中文字幕天堂不卡| 日韩精品无码一区二区三区免费 | 亚洲人成网站影音先锋播放| a毛片全部免费播放| 亚洲av无码乱码国产精品| 无码成A毛片免费| 亚洲avav天堂av在线网爱情| 啦啦啦手机完整免费高清观看 | 久久精品国产亚洲AV麻豆不卡| 久久爰www免费人成| 亚洲冬月枫中文字幕在线看| 永久免费av无码网站韩国毛片| 亚洲精品人成网在线播放影院| 四虎影院永久免费观看| 一个人看的免费高清视频日本| 国产精品亚洲αv天堂无码| 精品国产麻豆免费人成网站| 久久亚洲日韩看片无码| 在线观看成人免费视频| 免费无遮挡无遮羞在线看| 亚洲欧洲国产日韩精品| 在线免费观看韩国a视频| a毛看片免费观看视频| 日本亚洲免费无线码 | 久久亚洲国产成人精品性色| 免费看片免费播放| 久久爰www免费人成|