Skip to content

Commit 1c62f2d

Browse files
committed
Added function to read data from byte array instead of BIN file
1 parent a3b3638 commit 1c62f2d

File tree

2 files changed

+250
-154
lines changed

2 files changed

+250
-154
lines changed

com/ip2proxy/IP2Proxy.java

Lines changed: 98 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.ip2proxy;
22

3-
import java.io.IOException;
4-
import java.io.RandomAccessFile;
3+
import java.io.*;
54
import java.math.BigInteger;
65
import java.net.Inet4Address;
76
import java.net.Inet6Address;
@@ -98,6 +97,7 @@ private enum Modes {
9897

9998
private boolean _UseMemoryMappedFile = false;
10099
private String _IPDatabasePath = "";
100+
private FileLike.Supplier binFile;
101101

102102
private int COUNTRY_POSITION_OFFSET;
103103
private int REGION_POSITION_OFFSET;
@@ -125,12 +125,29 @@ private enum Modes {
125125
private boolean THREAT_ENABLED;
126126
private boolean PROVIDER_ENABLED;
127127

128-
private static final String _ModuleVersion = "3.3.0";
128+
private static final String _ModuleVersion = "3.4.0";
129129

130130
public IP2Proxy() {
131131

132132
}
133133

134+
interface FileLike {
135+
136+
interface Supplier {
137+
FileLike open() throws IOException;
138+
139+
boolean isValid();
140+
}
141+
142+
int read(byte[] buffer) throws IOException;
143+
144+
int read(byte b[], int off, int len) throws IOException;
145+
146+
void seek(long pos) throws IOException;
147+
148+
void close() throws IOException;
149+
}
150+
134151
/**
135152
* This function returns the module version.
136153
*
@@ -389,11 +406,11 @@ private void CreateMappedBytes(FileChannel InChannel) throws IOException {
389406

390407
private boolean LoadBIN() throws IOException {
391408
boolean LoadOK = false;
392-
RandomAccessFile aFile = null;
409+
FileLike aFile = null;
393410

394411
try {
395-
if (_IPDatabasePath.length() > 0) {
396-
aFile = new RandomAccessFile(_IPDatabasePath, "r");
412+
if (binFile.isValid()) {
413+
aFile = binFile.open();
397414
byte[] _HeaderData = new byte[64];
398415
aFile.read(_HeaderData);
399416
ByteBuffer _HeaderBuffer = ByteBuffer.wrap(_HeaderData);
@@ -521,6 +538,74 @@ public int Open(String DatabasePath, IOModes IOMode) throws IOException {
521538
_UseMemoryMappedFile = true;
522539
}
523540

541+
binFile = new FileLike.Supplier() {
542+
public FileLike open() throws IOException {
543+
return new FileLike() {
544+
private final RandomAccessFile aFile = new RandomAccessFile(DatabasePath, "r");
545+
546+
public int read(byte[] buffer) throws IOException {
547+
return aFile.read(buffer);
548+
}
549+
550+
public int read(byte[] b, int off, int len) throws IOException {
551+
return aFile.read(b, off, len);
552+
}
553+
554+
public void seek(long pos) throws IOException {
555+
aFile.seek(pos);
556+
}
557+
558+
public void close() throws IOException {
559+
aFile.close();
560+
}
561+
};
562+
}
563+
564+
public boolean isValid() {
565+
return DatabasePath.length() > 0;
566+
}
567+
};
568+
569+
if (!LoadBIN()) {
570+
return -1;
571+
} else {
572+
return 0;
573+
}
574+
} else {
575+
return 0;
576+
}
577+
}
578+
579+
public int Open(byte[] db) throws IOException {
580+
if (_DBType == 0) {
581+
binFile = new FileLike.Supplier() {
582+
public FileLike open() {
583+
return new FileLike() {
584+
private final ByteArrayInputStream stream = new ByteArrayInputStream(db);
585+
586+
public int read(byte[] buffer) throws IOException {
587+
return stream.read(buffer);
588+
}
589+
590+
public int read(byte[] b, int off, int len) {
591+
return stream.read(b, off, len);
592+
}
593+
594+
public void seek(long pos) {
595+
stream.reset();
596+
stream.skip(pos);
597+
}
598+
599+
public void close() throws IOException {
600+
stream.close();
601+
}
602+
};
603+
}
604+
605+
public boolean isValid() {
606+
return db.length > 0;
607+
}
608+
};
524609
if (!LoadBIN()) {
525610
return -1;
526611
} else {
@@ -544,7 +629,7 @@ public ProxyResult ProxyQuery(String IPAddress) throws IOException {
544629

545630
public ProxyResult ProxyQuery(String IPAddress, Modes Mode) throws IOException {
546631
ProxyResult Result = new ProxyResult();
547-
RandomAccessFile RF = null;
632+
FileLike RF = null;
548633
ByteBuffer Buf = null;
549634
ByteBuffer DataBuf = null;
550635
byte[] Row;
@@ -645,7 +730,7 @@ public ProxyResult ProxyQuery(String IPAddress, Modes Mode) throws IOException {
645730
}
646731
} else {
647732
DestroyMappedBytes();
648-
RF = new RandomAccessFile(_IPDatabasePath, "r");
733+
RF = binFile.open();
649734
}
650735

651736
if (IPType == 4) { // IPv4
@@ -1091,7 +1176,7 @@ private void Reverse(byte[] Arr) {
10911176
}
10921177
}
10931178

1094-
private byte[] ReadRow(final long Position, final long MyLen, final ByteBuffer Buf, final RandomAccessFile RH) throws IOException {
1179+
private byte[] ReadRow(final long Position, final long MyLen, final ByteBuffer Buf, final FileLike RH) throws IOException {
10951180
byte[] Row = new byte[(int) MyLen];
10961181
if (_UseMemoryMappedFile) {
10971182
Buf.position((int) Position);
@@ -1110,7 +1195,7 @@ private BigInteger Read32Or128Row(byte[] Row, final int From, final int Len) thr
11101195
return new BigInteger(1, Buf);
11111196
}
11121197

1113-
private BigInteger Read32Or128(final long Position, final int IPType, final ByteBuffer Buf, final RandomAccessFile RH) throws IOException {
1198+
private BigInteger Read32Or128(final long Position, final int IPType, final ByteBuffer Buf, final FileLike RH) throws IOException {
11141199
if (IPType == 4) {
11151200
return Read32(Position, Buf, RH);
11161201
} else if (IPType == 6) {
@@ -1119,7 +1204,7 @@ private BigInteger Read32Or128(final long Position, final int IPType, final Byte
11191204
return BigInteger.ZERO;
11201205
}
11211206

1122-
private BigInteger Read128(final long Position, final ByteBuffer Buf, final RandomAccessFile RH) throws IOException {
1207+
private BigInteger Read128(final long Position, final ByteBuffer Buf, final FileLike RH) throws IOException {
11231208
BigInteger RetVal;
11241209
final int BSize = 16;
11251210
byte[] Bytes = new byte[BSize];
@@ -1144,7 +1229,7 @@ private BigInteger Read32Row(byte[] Row, final int From) {
11441229
return new BigInteger(1, Bytes);
11451230
}
11461231

1147-
private BigInteger Read32(final long Position, final ByteBuffer Buf, final RandomAccessFile RH) throws IOException {
1232+
private BigInteger Read32(final long Position, final ByteBuffer Buf, final FileLike RH) throws IOException {
11481233
if (_UseMemoryMappedFile) {
11491234
// simulate unsigned int by using long
11501235
return BigInteger.valueOf(Buf.getInt((int) Position) & 0xffffffffL); // use absolute offset to be thread-safe
@@ -1158,7 +1243,7 @@ private BigInteger Read32(final long Position, final ByteBuffer Buf, final Rando
11581243
}
11591244
}
11601245

1161-
private String ReadStr(long Position, final ByteBuffer DataBuf, final RandomAccessFile FileHandle) throws IOException {
1246+
private String ReadStr(long Position, final ByteBuffer DataBuf, final FileLike FileHandle) throws IOException {
11621247
int Size = 256; // max size of string field + 1 byte for the length
11631248
final int Len;
11641249
final byte[] Data = new byte[Size];

0 commit comments

Comments
 (0)