Skip to content

Instantly share code, notes, and snippets.

@RebieKong RebieKong/Test.java
Created Mar 29, 2018

Embed
What would you like to do?
IPIP.net的DATX遍历进行中
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
public class Test {
private final static Integer INDEX_START = 262148;
private static byte[] data;
private static long indexSize;
public static void main(String... args) throws IOException {
String name = "/path/to/17monipdb.datx";
Path path = Paths.get(name);
data = Files.readAllBytes(path);
indexSize = bytesToLong(data[0], data[1], data[2], data[3]);
int dataSize = new Long((indexSize - 262144 - 262148) / 9).intValue() - 1;
for (int i = 0; i < dataSize; i++) {
getDt(i * 9);
}
}
private static void getDt(int pos) {
byte b = 0;
long off = bytesToLong(b, data[INDEX_START + pos + 6], data[INDEX_START + pos + 5], data[INDEX_START + pos + 4]);
long len = bytesToLong(b, b, data[INDEX_START + pos + 7], data[INDEX_START + pos + 8]);
int offset = new Long(off - 262144 + indexSize).intValue();
byte[] loc = Arrays.copyOfRange(data, offset, offset + new Long(len).intValue());
String[] S = new String(loc, Charset.forName("UTF-8")).split("\t", -1);
long l = bytesToLong(data[INDEX_START + pos], data[INDEX_START + pos + 1], data[INDEX_START + pos + 2], data[INDEX_START + pos + 3]);
System.out.println(longToIp(l));
System.out.println(off);
System.out.println(len);
Arrays.stream(S).forEach(System.out::println);
}
private static long bytesToLong(byte a, byte b, byte c, byte d) {
return int2long((((a & 0xff) << 24) | ((b & 0xff) << 16) | ((c & 0xff) << 8) | (d & 0xff)));
}
private static long int2long(int i) {
long l = i & 0x7fffffffL;
if (i < 0) {
l |= 0x080000000L;
}
return l;
}
public static String longToIp(Long l) {
String s1 = String.valueOf(l >> 24 & 255);
String s2 = String.valueOf(l >> 16 & 255);
String s3 = String.valueOf(l >> 8 & 255);
String s4 = String.valueOf(l & 255);
return s1 + "." + s2 + "." + s3 + "." + s4;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.