/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.briup.run.common.util.ip;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;
import java.util.Map;
/**
* <pre>
* 关于IP数据库文件格式,请参考LumaQQ主页文档“纯真IP数据库格式详解”一文。
* </pre>
*
* @author luma
*/
public class IPSeeker {
/**
* <pre>
* 用来封装ip相关信息,目前只有两个字段,ip所在的国家和地区
* </pre>
*
* @author luma
*/
private class IPLocation {
public String country;
public String area;
public IPLocation() {
country = area = "";
}
public IPLocation getCopy() {
IPLocation ret = new IPLocation();
ret.country = country;
ret.area = area;
return ret;
}
}
// 一些固定常量,比如记录长度等等
private static final int IP_RECORD_LENGTH = 7;
private static final byte REDIRECT_MODE_1 = 0x01;
private static final byte REDIRECT_MODE_2 = 0x02;
// 用来做为cache,查询一个ip时首先查看cache,以减少不必要的重复查找
private Map<String, IPLocation> ipCache;
// 随机文件访问类
private RandomAccessFile ipFile;
// 单一模式实例
private static IPSeeker instance = new IPSeeker();
// 起始地区的开始和结束的绝对偏移
private long ipBegin, ipEnd;
// 为提高效率而采用的临时变量
private IPLocation loc;
private byte[] buf;
private byte[] b4;
private byte[] b3;
/**
* 私有构造函数
*/
private IPSeeker() {
ipCache = new HashMap<String, IPLocation>();
loc = new IPLocation();
buf = new byte[100];
b4 = new byte[4];
b3 = new byte[3];
try {
ipFile = new RandomAccessFile(Utils.IP_FILE, "r");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 如果打开文件成功,读取文件头信息
if(ipFile != null) {
try {
ipBegin = readLong4(0);
ipEnd = readLong4(4);
if(ipBegin == -1 || ipEnd == -1) {
ipFile.close();
ipFile = null;
}
} catch (IOException e) {
//IP地址信息文件格式有错误,IP显示功能将无法使用
e.printStackTrace();
ipFile = null;
}
}
}
/**
* @return 单一实例
*/
public static IPSeeker getInstance() {
return instance;
}
/**
* 根据IP得到国家名
* @param ip ip的字节数组形式
* @return 国家名字符串
*/
public String getCountry(byte[] ip) {
// 检查ip地址文件是否正常
if(ipFile == null)
return "getCountry(byte[] ip) error!";
// 保存ip,转换ip字节数组为字符串形式
String ipStr = Utils.getIpStringFromBytes(ip);
// 先检查cache中是否已经包含有这个ip的结果,没有再搜索文件
if(ipCache.containsKey(ipStr)) {
IPLocation ipLoc = ipCache.get(ipStr);
return ipLoc.country;
} else {
IPLocation ipLoc = getIPLocation(ip);
ipCache.put(ipStr, ipLoc.getCopy());
return ipLoc.country;
}
}
/**
* 根据IP得到国家名
* @param ip IP的字符串形式
* @return 国家名字符串
*/
public String getCountry(String ip) {
return getCountry(Utils.getIpByteArrayFromString(ip));
}
/**
* 根据IP得到地区名
* @param ip ip的字节数组形式
* @return 地区名字符串
*/
public String getArea(byte[] ip) {
// 检查ip地址文件是否正常
if(ipFile == null)
return "getArea(byte[] ip) error!";
// 保存ip,转换ip字节数组为字符串形式
String ipStr = Utils.getIpStringFromBytes(ip);
// 先检查cache中是否已经包含有这个ip的结果,没有再搜索文件
if(ipCache.containsKey(ipStr)) {
IPLocation ipLoc = ipCache.get(ipStr);
return ipLoc.area;
} else {
IPLocation ipLoc = getIPLocation(ip);
ipCache.put(ipStr, ipLoc.getCopy());
return ipLoc.area;
}
}
/**
* 根据IP得到地区名
* @param ip IP的字符串形式
* @return 地区名字符串
*/
public String getArea(String ip) {
return getArea(Utils.getIpByteArrayFromString(ip));
}
/**
* 根据ip搜索ip信息文件,得到IPLocation结构,所搜索的ip参数从类成员ip中得到
* @param ip 要查询的IP
* @return IPLocation结构
*/
private IPLocation getIPLocation(byte[] ip) {
IPLocation info = null;
long offset = locateIP(ip);
if(offset != -1)
info = getIPLocation(offset);
if(info == null) {
info = new IPLocation();
info.country = "unknown country";
info.area = "unknown area";
}
return info;
}
/**
* 从offset位置读取4个字节为一个long,因为java为big-endian格式,所以没办法
* 用了这么一个函数来做转换
* @param offset
* @return 读取的long值,返回-1表示读取文件失败
*/
private long readLong4(long offset) {
long ret = 0;
try {
ipFile.seek(offset);
ret |= (ipFile.readByte() & 0xFF);
ret |= ((ipFile.readByte() << 8) & 0xFF00);
ret |= ((ipFile.readByte() << 16) & 0xFF0000);
ret |= ((ipFile.readByte() << 24) & 0xFF000000);
return ret;
} catch (IOException e) {
return -1;
}
}
/**
* 从offset位置读取3个字节为一个long,因为java为big-endian格式,所以没办法
* 用了这么一个函数来做转换
* @param offset 整数的起始偏移
* @return 读取的long值,返回-1表示读取文件失败
*/
private long readLong3(long offset) {
long ret = 0;
try {
ipFile.seek(offset);
ipFile.readFully(b3);
ret |= (b3[0] & 0xFF);
ret |= ((b3[1] << 8) & 0xFF00);
ret |= ((b3[2] << 16) & 0xFF0000);
return ret;
} catch (IOException e) {
return -1;
}
}
/**
* 从当前位置读取3个字节转换成long
* @return 读取的long值,返回-1表示读取文件失败
*/
private long readLong3() {
long ret = 0;
try {
ipFile.readFully(b3);
ret |= (b3[0] & 0xFF);
ret |= ((b3[1] << 8) & 0xFF00);
ret |= ((b3[2] << 16) & 0xFF0000);
return ret;
} catch (IOException e) {
return -1;
}
}
/**
* 从offset位置读取四个字节的ip地址放入ip数组中,读取后的ip为big-endian格式,但是
* 文件中是little-endian形式,将会进行转换
* @param offset
* @param ip
*/
private void readIP(long offset, byte[] ip) {
try {
ipFile.seek(offset);
ipFile.readFully(ip);
byte temp = ip[0];
ip[0] = ip[3];
ip[3] = temp;
temp = ip[1];
ip[1] = ip[2];
ip[2] = temp;
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 把类成员ip和beginIp比较,注意这个beginIp是big-endian的
* @param ip 要查询的IP
* @param beginIp 和被查询IP相比较的IP
* @return 相等返回0,ip大于beginIp则返回1,小于返回-1。
*/
private int compareIP(byte[] ip, byte[] beginIp) {
for(int i = 0; i < 4; i++) {
int r = compareByte(ip[i], beginIp[i]);
if(r != 0)
return r;
}
return 0;
}
/**
* 把两个byte当作无符号数进行比较
* @param b1
* @param
没有合适的资源?快使用搜索试试~ 我知道了~
社区管理系统
共275个文件
class:79个
java:78个
jar:27个
5星 · 超过95%的资源 需积分: 42 86 下载量 33 浏览量
2012-05-18
23:27:21
上传
评论 9
收藏 13.9MB RAR 举报
温馨提示
java 做的社区管理系统,只有源代码和数据库
资源推荐
资源详情
资源评论
收起资源包目录
社区管理系统 (275个子文件)
MD5.class 8KB
MessengerServiceImpl.class 8KB
MemberServiceImpl.class 8KB
MessengerDaoImpl.class 8KB
IPSeeker.class 7KB
MemberDaoImpl.class 6KB
MemberInfo.class 5KB
AuthImg.class 4KB
SaveModifyAction.class 3KB
MatchFriendsAction.class 3KB
SendMessageAction.class 3KB
MakeXML.class 3KB
LoginAction.class 3KB
OutboxMessageAction.class 3KB
SaveModifyForm.class 3KB
ContextListener.class 3KB
RegisterForm.class 3KB
AddFriendAction.class 3KB
SearchMemberAction.class 3KB
AfterActionAction.class 3KB
MoveToBlackListAction.class 3KB
HibernateSessionFactory.class 3KB
LoadProvince.class 3KB
Utils.class 3KB
LookSpaceAction.class 2KB
MoveToBuddyListAction.class 2KB
DetailMessageAction.class 2KB
CreateSpaceAction.class 2KB
MemberSpace.class 2KB
CreateSpaceForm.class 2KB
MessageRecord.class 2KB
RandomChar.class 2KB
CheckAutoLoginAction.class 2KB
LoginFilter.class 2KB
DelBuddyAction.class 2KB
DelBlackAction.class 2KB
DelReceiveMessageAction.class 2KB
DelSendMessageAction.class 2KB
MatchOneFriendAction.class 2KB
ExitAction.class 2KB
AccessListener.class 2KB
ManageSpaceAction.class 2KB
BeanFactory.class 2KB
ModifyAction.class 2KB
IMessengerDao.class 2KB
GetbackPasswdForm.class 1KB
PointRecord.class 1KB
GradeRecord.class 1KB
LoginForm.class 1KB
MatchFriendsForm.class 1KB
IMessengerService.class 1KB
EncodeFilter.class 1KB
IMemberService.class 1KB
IMemberDao.class 1KB
CurrentPage.class 1KB
PointAction.class 1KB
CreateMessageAction.class 1KB
RegisterServlet.class 1KB
Util.class 1KB
FriendRecord.class 1KB
BlackRecord.class 1KB
Province.class 1KB
RegisterAction.class 1KB
GetbackPasswdAction.class 1021B
ForgetPasswdAction.class 1008B
BackActivityAction.class 1008B
MatchFriendAction.class 1005B
NewSpaceAction.class 1000B
HibernateTransaction.class 995B
InboxMessageAction.class 986B
BuddyListAction.class 977B
BlackListAction.class 977B
CheckAutoLoginForm.class 831B
IPSeeker$IPLocation.class 823B
MessengerException.class 772B
MemberException.class 763B
Counter.class 684B
Test.class 413B
Transaction.class 205B
.classpath 2KB
org.eclipse.wst.jsdt.ui.superType.container 49B
style.css 5KB
style1.css 4KB
ip.dat 5.06MB
ip.dat 5.06MB
hibernate3.jar 1.6MB
ojdbc14.jar 1.14MB
xerces-2.6.2.jar 987KB
struts.jar 531KB
antlr-2.7.5H3.jar 423KB
standard.jar 384KB
antlr.jar 350KB
log4j-1.2.9.jar 344KB
dom4j-1.6.jar 306KB
cglib-2.1.jar 275KB
jaxen-1.1-beta-4.jar 233KB
commons-beanutils.jar 184KB
commons-collections-2.1.1.jar 171KB
commons-digester.jar 164KB
xml-apis.jar 121KB
共 275 条
- 1
- 2
- 3
linianzhenti
- 粉丝: 6
- 资源: 34
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 常用工具集参考用于图像等数据处理
- 音乐展示网页、基于Stenography的图像数字水印添加与提取,以及基于颜色矩和Tamura算法的图像相似度评估算法py源码
- 基于EmguCV(OpenCV .net封装),图像数字水印加解密算法的实现,其中包含最低有效位算法,离散傅里叶变换算法+文档书
- 基于matlab+DWT的图像水印项目,数字水印+源代码+文档说明+图片+报告pdf
- (优秀毕业设计)基于python实现的数字图像可视化水印系统的设计与实现,多种数字算法实现+源代码+文档说明+理论演示pdf
- 基于DWT-DCT-SVD和deflate压缩的数字水印方法python源码+Gui界面+演示视频(高分毕业设计)
- 基于matlab实现DWT、DCT、SVD算法数字图像水印可视化系统+GUI界面+文档说明+详细注释(高分毕业设计)
- NCIAE-Data-Structure大一大二笔记
- 学习wireshark笔记
- digital-image-数据可视化笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页