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); } } }
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); } } }
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;
}
}
/*
* 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();
}
}
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());
}
}
|