@blueislandx/island-qqwry-database
Version:
Island (小蓝岛) 纯真 IP 数据库 qqwry.dat 解析
44 lines (34 loc) • 1.06 kB
text/typescript
import * as fs from 'fs';
import { AbstractBuffer } from '../common/abstract.buffer';
export class FileBuffer implements AbstractBuffer {
fd: number;
max: number;
constructor(path: string) {
this.fd = fs.openSync(path, 'r');
this.max = fs.fstatSync(this.fd).size;
}
readBuffer(start: number, length: number) {
const buffer = Buffer.alloc(length);
fs.readSync(this.fd, buffer, 0, length, start);
return buffer;
}
readUIntLE(start: number, length: number) {
return this.readBuffer(start, length).readUIntLE(0, length);
}
getStringByteArray(start: number) {
const result = [];
for (var i = start; i < this.max; i++) {
const temp = this.readBuffer(i, 1)[0];
if (temp === 0) {
break;
}
result.push(temp);
}
return result;
}
close() {
if (this.fd !== null) {
fs.closeSync(this.fd);
}
}
}