|
9 | 9 | import java.util.Arrays; |
10 | 10 |
|
11 | 11 | public class Reader { |
12 | | - private static int DATA_SECTION_SEPARATOR_SIZE = 16; |
13 | | - private static byte METADATE_START_MARKER[] = { (byte) 0xAB, (byte) 0xCD, |
| 12 | + private static final int DATA_SECTION_SEPARATOR_SIZE = 16; |
| 13 | + private static final byte[] METADATA_START_MARKER = { (byte) 0xAB, (byte) 0xCD, |
14 | 14 | (byte) 0xEF, 'M', 'a', 'x', 'M', 'i', 'n', 'd', '.', 'c', 'o', 'm' }; |
15 | 15 |
|
16 | | - private static final boolean DEBUG = false; |
| 16 | + private final boolean DEBUG; |
17 | 17 | private final Decoder decoder; |
18 | 18 | private final Metadata metadata; |
19 | | - private final long dataSectionEnd; |
20 | 19 | private final FileChannel fc; |
| 20 | + private final RandomAccessFile raf; |
21 | 21 |
|
22 | 22 | public Reader(File database) throws MaxMindDbException, IOException { |
23 | | - |
24 | | - RandomAccessFile raf = new RandomAccessFile(database, "r"); |
25 | | - this.fc = raf.getChannel(); |
| 23 | + this.DEBUG = System.getenv().get("MAXMIND_DB_READER_DEBUG") != null; |
| 24 | + this.raf = new RandomAccessFile(database, "r"); |
| 25 | + this.fc = this.raf.getChannel(); |
26 | 26 | // XXX - we will want |
27 | 27 | // MappedByteBuffer in = fc.map(FileChannel.MapMode.READ_ONLY, 0, |
28 | 28 | // fc.size()); |
@@ -50,9 +50,6 @@ public Reader(File database) throws MaxMindDbException, IOException { |
50 | 50 | + "). Is this a valid MaxMind DB file?"); |
51 | 51 | } |
52 | 52 |
|
53 | | - // XXX - right? |
54 | | - this.dataSectionEnd = start - METADATE_START_MARKER.length; |
55 | | - |
56 | 53 | Decoder metadataDecoder = new Decoder(this.fc, 0); |
57 | 54 |
|
58 | 55 | this.metadata = new Metadata(metadataDecoder.decode(start).getObject()); |
@@ -159,7 +156,7 @@ private long[] readNode(long nodeNumber) throws IOException, |
159 | 156 | private long[] splitNodeIntoRecords(ByteBuffer bytes) |
160 | 157 | throws MaxMindDbException { |
161 | 158 | long[] nodes = new long[2]; |
162 | | - switch (this.metadata.recordSize.intValue()) { |
| 159 | + switch (this.metadata.recordSize) { |
163 | 160 | case 24: |
164 | 161 | nodes[0] = Util.decodeLong(Arrays.copyOfRange(bytes.array(), 0, |
165 | 162 | 3)); |
@@ -215,11 +212,11 @@ private Object resolveDataPointer(long pointer) throws MaxMindDbException, |
215 | 212 | private long findMetadataStart() throws IOException { |
216 | 213 | long fileSize = this.fc.size(); |
217 | 214 |
|
218 | | - FILE: for (long i = 0; i < fileSize - METADATE_START_MARKER.length + 1; i++) { |
219 | | - for (int j = 0; j < METADATE_START_MARKER.length; j++) { |
| 215 | + FILE: for (long i = 0; i < fileSize - METADATA_START_MARKER.length + 1; i++) { |
| 216 | + for (int j = 0; j < METADATA_START_MARKER.length; j++) { |
220 | 217 | ByteBuffer b = ByteBuffer.wrap(new byte[1]); |
221 | 218 | this.fc.read(b, fileSize - i - j - 1); |
222 | | - if (b.get(0) != METADATE_START_MARKER[METADATE_START_MARKER.length |
| 219 | + if (b.get(0) != METADATA_START_MARKER[METADATA_START_MARKER.length |
223 | 220 | - j - 1]) { |
224 | 221 | continue FILE; |
225 | 222 | } |
|
0 commit comments