资源简介
手机号码归属地数据库,2019-8月更新。总计录数:428971
数据格式为dat,这是完整的java解析代码。
代码片段和文件信息
package cn.kbyte.utils.phone;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
/**
* Created by fengjiajie on 16/10/12.
*/
public class PhoneNumberGeo {
private static final String[] PHONE_NUMBER_TYPE = {null “移动“ “联通“ “电信“ “电信虚拟运营商“ “联通虚拟运营商“ “移动虚拟运营商“};
private static final int INDEX_SEGMENT_LENGTH = 9;
private static final int DATA_FILE_LENGTH_HINT = 3747505;
private static final String PHONE_DAT_FILE_PATH = “phone-number-geo/phone.dat“;
private static byte[] dataByteArray;
private static int indexAreaOffset;
private static int phoneRecordCount;
private ByteBuffer byteBuffer;
private static synchronized void initData() {
if (dataByteArray == null) {
ByteArrayOutputStream byteData = new ByteArrayOutputStream(DATA_FILE_LENGTH_HINT);
byte[] buffer = new byte[1024];
int readBytesLength;
try (InputStream inputStream = PhoneNumberGeo.class.getClassLoader().getResourceAsStream(PHONE_DAT_FILE_PATH)) {
while ((readBytesLength = inputStream.read(buffer)) != -1) {
byteData.write(buffer 0 readBytesLength);
}
} catch (Exception e) {
System.err.println(“Can‘t find phone.dat in classpath: “ + PHONE_DAT_FILE_PATH);
e.printStackTrace();
throw new RuntimeException(e);
}
dataByteArray = byteData.toByteArray();
ByteBuffer byteBuffer = ByteBuffer.wrap(dataByteArray);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
int dataVersion = byteBuffer.getInt();
indexAreaOffset = byteBuffer.getInt();
phoneRecordCount = (dataByteArray.length - indexAreaOffset) / INDEX_SEGMENT_LENGTH;
// print data version
// System.out.println(dataVersion);
// print record count
// System.out.println(phoneRecordCount);
}
}
public PhoneNumberGeo() {
initData();
byteBuffer = ByteBuffer.wrap(dataByteArray);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
}
public PhoneNumberInfo lookup(String phoneNumber) {
if (phoneNumber == null || phoneNumber.length() > 11 || phoneNumber.length() < 7) {
return null;
}
int phoneNumberPrefix;
try {
phoneNumberPrefix = Integer.parseInt(phoneNumber.substring(0 7));
} catch (Exception e) {
return null;
}
int left = 0;
int right = phoneRecordCount;
while (left <= right) {
int middle = (left + right) >> 1;
int currentOffset = indexAreaOffset + middle * INDEX_SEGMENT_LENGTH;
if (currentOffset >= dataByteArray.length) {
return null;
}
byteBuffer.position(currentOffset);
int currentPrefix = byteBuffer.getInt();
if (currentPrefix > phoneNumberPrefix) {
right = middle - 1;
} else if (currentPrefix < phoneNumberPrefix) {
left = middle + 1;
} else {
int infoBeginOffset = byteBuffer.getInt();
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1026 2019-08-12 15:30 pom.xm
目录 0 2019-08-12 15:25 src\
目录 0 2019-08-12 15:25 src\main\
目录 0 2019-08-12 15:25 src\main\java\
目录 0 2019-08-12 15:25 src\main\java\cn\
目录 0 2019-08-12 15:25 src\main\java\cn\kbyte\
目录 0 2019-08-12 15:25 src\main\java\cn\kbyte\utils\
目录 0 2019-08-12 15:44 src\main\java\cn\kbyte\utils\phone\
文件 4608 2019-08-12 15:44 src\main\java\cn\kbyte\utils\phone\PhoneNumberGeo.java
文件 1390 2018-10-30 19:12 src\main\java\cn\kbyte\utils\phone\PhoneNumberInfo.java
目录 0 2019-08-12 15:25 src\main\resources\
目录 0 2019-08-12 15:25 src\main\resources\phone-number-geo\
文件 3870885 2019-08-12 15:03 src\main\resources\phone-number-geo\phone.dat
目录 0 2019-08-12 15:25 src\test\
目录 0 2019-08-12 15:25 src\test\java\
目录 0 2019-08-12 15:25 src\test\java\cn\
目录 0 2019-08-12 15:25 src\test\java\cn\kbyte\
目录 0 2019-08-12 15:25 src\test\java\cn\kbyte\utils\
目录 0 2019-08-12 16:02 src\test\java\cn\kbyte\utils\phone\
文件 1170 2019-08-12 16:02 src\test\java\cn\kbyte\utils\phone\PhoneNumberGeoTest.java
评论
共有 条评论