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

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

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

    java Source

      BlogJava :: 首頁 :: 聯(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 閱讀(464) | 評(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 閱讀(1640) | 評(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 閱讀(1495) | 評(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(
    "沒有找到設(shè)備,開始注冊(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;
        }
        
        //寫入空的注冊(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 閱讀(1388) | 評(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 閱讀(2261) | 評(píng)論 (2)編輯 收藏

    主站蜘蛛池模板: 亚洲另类无码专区首页| 中文字幕精品无码亚洲字| 69式国产真人免费视频 | 亚洲精品国产免费| 久久精品国产亚洲av麻豆色欲| 亚洲国产AV无码专区亚洲AV| 亚洲乱码国产乱码精品精| 亚洲欧洲日产国码无码久久99| 亚洲桃色AV无码| 久久亚洲精品中文字幕三区| 亚洲线精品一区二区三区影音先锋| 亚洲一区二区三区无码中文字幕| 亚洲精品你懂的在线观看| 亚洲精品无码鲁网中文电影| 亚洲AV无码乱码在线观看富二代 | 久久精品无码一区二区三区免费 | 99久久99这里只有免费的精品 | 亚洲av日韩av永久在线观看| 日韩亚洲综合精品国产| 免费福利资源站在线视频| 久久精品免费大片国产大片| 手机看片国产免费永久| 精品熟女少妇a∨免费久久| 国产在线观看免费观看不卡| 国产午夜无码视频免费网站| 国产成人高清亚洲| 亚洲欧洲精品无码AV| 久久久久久久亚洲Av无码 | 久久综合亚洲鲁鲁五月天| 亚洲天堂2016| 另类小说亚洲色图| a色毛片免费视频| 无码人妻一区二区三区免费手机| 小小影视日本动漫观看免费| 亚洲国产精品人人做人人爱| 亚洲动漫精品无码av天堂| 亚洲同性男gay网站在线观看| 久久久久亚洲AV无码去区首| 国产激情久久久久影院老熟女免费| 色欲色香天天天综合网站免费| 成人免费一区二区三区在线观看|