java Source

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            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 閱讀(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)編輯 收藏

          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設備自動搜索中",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設備:" + 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設備在端口" + 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 閱讀(1497) | 評論 (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 {
          //查找設備注冊文件在jmf.jar同級目錄
                  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(
          "沒有找到設備,開始注冊視頻采集設備",false,false);
                  JMFInit jmfInit 
          = new JMFInit(args);
                  
          boolean flag = jmfInit.isInitialError();
                  jmfInit.dispose();
                  
          if(flag != true)
                      BubbleDialog.getBubble().display(
          "視頻采集設備注冊完畢",false,true);
                  
          else 
                      BubbleDialog.getBubble().display(
          "視頻采集設備注冊失敗,程序將退出",true,true);
                  
          return !flag;
              }
              
              //寫入空的注冊文件(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 閱讀(1389) | 評論 (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 閱讀(2263) | 評論 (2)編輯 收藏

          主站蜘蛛池模板: 海晏县| 黑水县| 萍乡市| 北碚区| 夏河县| 崇信县| 南雄市| 阿拉善盟| 陇西县| 桂阳县| 青铜峡市| 闸北区| 金川县| 嘉黎县| 乌恰县| 海兴县| 抚顺县| 清丰县| 鄱阳县| 龙门县| 合山市| 青神县| 吉木乃县| 壶关县| 凉城县| 凌海市| 渭南市| 丘北县| 玉溪市| 永平县| 綦江县| 隆回县| 萍乡市| 罗城| 黔东| 焦作市| 开阳县| 邳州市| 临澧县| 奇台县| 鹿泉市|