Skip to content

Commit df7f600

Browse files
committed
Formatting. Changed static factory method to constructor.
1 parent c3076d3 commit df7f600

4 files changed

Lines changed: 85 additions & 62 deletions

File tree

src/main/java/com/maxmind/db/Metadata.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public Metadata(JsonNode metadata) {
3636

3737
/*
3838
* (non-Javadoc)
39-
*
39+
*
4040
* @see java.lang.Object#toString()
4141
*/
4242
@Override

src/main/java/com/maxmind/db/Reader.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public enum FileMode {
4343
/**
4444
* Constructs a Reader for the MaxMind DB format. The file passed to it must
4545
* be a valid MaxMind DB file such as a GeoIP2 database file.
46-
*
46+
*
4747
* @param database
4848
* the MaxMind DB file to use.
4949
* @throws IOException
@@ -56,18 +56,20 @@ public Reader(File database) throws IOException {
5656
/**
5757
* Constructs a Reader as if in mode {@link FileMode#MEMORY}, without using
5858
* a <code>File</code> instance.
59-
* @param source the InputStream that contains the MaxMind DB file.
59+
*
60+
* @param source
61+
* the InputStream that contains the MaxMind DB file.
6062
* @throws IOException
6163
* if there is an error reading from the Stream.
6264
*/
6365
public Reader(InputStream source) throws IOException {
64-
this(ThreadBuffer.newInstance(source), "an InputStream");
66+
this(new ThreadBuffer(source), "<InputStream>");
6567
}
6668

6769
/**
6870
* Constructs a Reader for the MaxMind DB format. The file passed to it must
6971
* be a valid MaxMind DB file such as a GeoIP2 database file.
70-
*
72+
*
7173
* @param database
7274
* the MaxMind DB file to use.
7375
* @param fileMode
@@ -91,7 +93,7 @@ private Reader(ThreadBuffer buffer, String name) throws IOException {
9193

9294
/**
9395
* Looks up the <code>address</code> in the MaxMind DB.
94-
*
96+
*
9597
* @param ipAddress
9698
* the IP address to look up.
9799
* @return the record for the IP address.
@@ -206,7 +208,7 @@ private JsonNode resolveDataPointer(int pointer) throws IOException {
206208
/*
207209
* Apparently searching a file for a sequence is not a solved problem in
208210
* Java. This searches from the end of the file for metadata start.
209-
*
211+
*
210212
* This is an extremely naive but reasonably readable implementation. There
211213
* are much faster algorithms (e.g., Boyer-Moore) for this if speed is ever
212214
* an issue, but I suspect it won't be.
@@ -237,7 +239,7 @@ Metadata getMetadata() {
237239

238240
/**
239241
* Closes the MaxMind DB and returns resources to the system.
240-
*
242+
*
241243
* @throws IOException
242244
* if an I/O error occurs.
243245
*/

src/main/java/com/maxmind/db/ThreadBuffer.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import java.io.ByteArrayOutputStream;
44
import java.io.Closeable;
55
import java.io.File;
6-
import java.io.InputStream;
76
import java.io.IOException;
7+
import java.io.InputStream;
88
import java.io.RandomAccessFile;
99
import java.nio.ByteBuffer;
1010
import java.nio.channels.FileChannel;
@@ -13,27 +13,6 @@
1313
import com.maxmind.db.Reader.FileMode;
1414

1515
final class ThreadBuffer extends ThreadLocal<ByteBuffer> implements Closeable {
16-
/**
17-
* Construct a ThreadBuffer from the provided URL.
18-
* @param stream the source of my bytes.
19-
* @return a newly constructed instance based on the contents of your URL.
20-
* @throws IOException if unable to read from your source.
21-
* @throws NullPointerException if you provide a NULL InputStream
22-
*/
23-
public static ThreadBuffer newInstance(InputStream stream) throws IOException {
24-
if (null == stream) {
25-
throw new NullPointerException("Unable to use a NULL InputStream");
26-
}
27-
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
28-
final byte[] buffer = new byte[16 * 1024];
29-
int br;
30-
while (-1 != (br = stream.read(buffer))) {
31-
baos.write(buffer, 0, br);
32-
}
33-
final ByteBuffer bBuffer = ByteBuffer.wrap(baos.toByteArray());
34-
return new ThreadBuffer(bBuffer);
35-
}
36-
3716
// DO NOT PASS THESE OUTSIDE THIS CLASS. Doing so will remove thread
3817
// safety.
3918
private final ByteBuffer buffer;
@@ -51,6 +30,32 @@ public static ThreadBuffer newInstance(InputStream stream) throws IOException {
5130
}
5231
}
5332

33+
/**
34+
* Construct a ThreadBuffer from the provided URL.
35+
*
36+
* @param stream
37+
* the source of my bytes.
38+
* @return a newly constructed instance based on the contents of your URL.
39+
* @throws IOException
40+
* if unable to read from your source.
41+
* @throws NullPointerException
42+
* if you provide a NULL InputStream
43+
*/
44+
ThreadBuffer(InputStream stream) throws IOException {
45+
if (null == stream) {
46+
throw new NullPointerException("Unable to use a NULL InputStream");
47+
}
48+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
49+
final byte[] bytes = new byte[16 * 1024];
50+
int br;
51+
while (-1 != (br = stream.read(bytes))) {
52+
baos.write(bytes, 0, br);
53+
}
54+
this.buffer = ByteBuffer.wrap(baos.toByteArray());
55+
this.raf = null;
56+
this.fc = null;
57+
}
58+
5459
// This is just to ease unit testing
5560
ThreadBuffer(ByteBuffer buffer) {
5661
this.buffer = buffer;

src/test/java/com/maxmind/db/ReaderTest.java

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public class ReaderTest {
2828
private final ObjectMapper om = new ObjectMapper();
2929

3030
@Test
31-
public void test() throws IOException,
32-
URISyntaxException {
31+
public void test() throws IOException, URISyntaxException {
3332
for (long recordSize : new long[] { 24, 28, 32 }) {
3433
for (int ipVersion : new int[] { 4, 6 }) {
3534
URI file = ReaderTest.class.getResource(
@@ -52,24 +51,28 @@ public void test() throws IOException,
5251
}
5352

5453
@Test
55-
public void testNoIpV4SearchTreeFile() throws IOException, URISyntaxException {
54+
public void testNoIpV4SearchTreeFile() throws IOException,
55+
URISyntaxException {
5656
URI file = ReaderTest.class.getResource(
5757
"/maxmind-db/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb")
5858
.toURI();
5959

6060
Reader reader = new Reader(new File(file));
61-
testNoIpV4SearchTree(reader);
61+
this.testNoIpV4SearchTree(reader);
6262
}
63+
6364
@Test
64-
public void testNoIpV4SearchTreeURL() throws IOException, URISyntaxException {
65+
public void testNoIpV4SearchTreeURL() throws IOException,
66+
URISyntaxException {
6567
InputStream stream = ReaderTest.class.getResource(
6668
"/maxmind-db/test-data/MaxMind-DB-no-ipv4-search-tree.mmdb")
6769
.openStream();
6870
Reader reader = new Reader(stream);
69-
testNoIpV4SearchTree(reader);
71+
this.testNoIpV4SearchTree(reader);
7072
}
71-
private void testNoIpV4SearchTree(Reader reader) throws IOException, URISyntaxException {
7273

74+
private void testNoIpV4SearchTree(Reader reader) throws IOException,
75+
URISyntaxException {
7376

7477
assertEquals("::/64", reader.get(InetAddress.getByName("1.1.1.1"))
7578
.textValue());
@@ -83,18 +86,21 @@ public void testDecodingTypesFile() throws URISyntaxException, IOException {
8386
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb").toURI();
8487

8588
Reader reader = new Reader(new File(file));
86-
testDecodingTypes(reader);
89+
this.testDecodingTypes(reader);
8790
}
91+
8892
@Test
8993
public void testDecodingTypesURL() throws URISyntaxException, IOException {
9094
InputStream stream = ReaderTest.class.getResource(
9195
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb")
9296
.openStream();
9397

9498
Reader reader = new Reader(stream);
95-
testDecodingTypes(reader);
99+
this.testDecodingTypes(reader);
96100
}
97-
private void testDecodingTypes(Reader reader) throws URISyntaxException, IOException {
101+
102+
private void testDecodingTypes(Reader reader) throws URISyntaxException,
103+
IOException {
98104
JsonNode record = reader.get(InetAddress.getByName("::1.1.1.0"));
99105

100106
assertEquals(true, record.get("boolean").booleanValue());
@@ -143,18 +149,21 @@ public void testZerosFile() throws URISyntaxException, IOException {
143149
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb").toURI();
144150

145151
Reader reader = new Reader(new File(file));
146-
testZeros(reader);
152+
this.testZeros(reader);
147153
}
154+
148155
@Test
149156
public void testZerosURL() throws URISyntaxException, IOException {
150157
InputStream stream = ReaderTest.class.getResource(
151158
"/maxmind-db/test-data/MaxMind-DB-test-decoder.mmdb")
152159
.openStream();
153160

154161
Reader reader = new Reader(stream);
155-
testZeros(reader);
162+
this.testZeros(reader);
156163
}
157-
private void testZeros(Reader reader) throws URISyntaxException, IOException {
164+
165+
private void testZeros(Reader reader) throws URISyntaxException,
166+
IOException {
158167
JsonNode record = reader.get(InetAddress.getByName("::"));
159168

160169
assertEquals(false, record.get("boolean").booleanValue());
@@ -189,8 +198,9 @@ public void testBrokenDatabaseFile() throws URISyntaxException, IOException {
189198
.toURI();
190199

191200
Reader reader = new Reader(new File(file));
192-
testBrokenDatabase(reader);
201+
this.testBrokenDatabase(reader);
193202
}
203+
194204
@Test
195205
public void testBrokenDatabaseURL() throws URISyntaxException, IOException {
196206
InputStream stream = ReaderTest.class
@@ -199,9 +209,11 @@ public void testBrokenDatabaseURL() throws URISyntaxException, IOException {
199209
.openStream();
200210

201211
Reader reader = new Reader(stream);
202-
testBrokenDatabase(reader);
212+
this.testBrokenDatabase(reader);
203213
}
204-
private void testBrokenDatabase(Reader reader) throws URISyntaxException, IOException {
214+
215+
private void testBrokenDatabase(Reader reader) throws URISyntaxException,
216+
IOException {
205217

206218
this.thrown.expect(InvalidDatabaseException.class);
207219
this.thrown
@@ -219,8 +231,9 @@ public void testBrokenSearchTreePointerFile() throws URISyntaxException,
219231
.toURI();
220232

221233
Reader reader = new Reader(new File(file));
222-
testBrokenSearchTreePointer(reader);
234+
this.testBrokenSearchTreePointer(reader);
223235
}
236+
224237
@Test
225238
public void testBrokenSearchTreePointerURL() throws URISyntaxException,
226239
IOException {
@@ -230,10 +243,11 @@ public void testBrokenSearchTreePointerURL() throws URISyntaxException,
230243
.openStream();
231244

232245
Reader reader = new Reader(stream);
233-
testBrokenSearchTreePointer(reader);
246+
this.testBrokenSearchTreePointer(reader);
234247
}
235-
private void testBrokenSearchTreePointer(Reader reader) throws URISyntaxException,
236-
IOException {
248+
249+
private void testBrokenSearchTreePointer(Reader reader)
250+
throws URISyntaxException, IOException {
237251

238252
this.thrown.expect(InvalidDatabaseException.class);
239253
this.thrown
@@ -243,26 +257,31 @@ private void testBrokenSearchTreePointer(Reader reader) throws URISyntaxExceptio
243257
}
244258

245259
@Test
246-
public void testBrokenDataPointerFile() throws IOException, URISyntaxException {
260+
public void testBrokenDataPointerFile() throws IOException,
261+
URISyntaxException {
247262
URI file = ReaderTest.class
248263
.getResource(
249264
"/maxmind-db/test-data/MaxMind-DB-test-broken-pointers-24.mmdb")
250265
.toURI();
251266

252267
Reader reader = new Reader(new File(file));
253-
testBrokenDataPointer(reader);
268+
this.testBrokenDataPointer(reader);
254269
}
270+
255271
@Test
256-
public void testBrokenDataPointerURL() throws IOException, URISyntaxException {
272+
public void testBrokenDataPointerURL() throws IOException,
273+
URISyntaxException {
257274
InputStream stream = ReaderTest.class
258275
.getResource(
259-
"/maxmind-db/test-data/MaxMind-DB-test-broken-pointers-24.mmdb")
276+
"/maxmind-db/test-data/MaxMind-DB-test-broken-pointers-24.mmdb")
260277
.openStream();
261278

262279
Reader reader = new Reader(stream);
263-
testBrokenDataPointer(reader);
280+
this.testBrokenDataPointer(reader);
264281
}
265-
private void testBrokenDataPointer(Reader reader) throws IOException, URISyntaxException {
282+
283+
private void testBrokenDataPointer(Reader reader) throws IOException,
284+
URISyntaxException {
266285

267286
this.thrown.expect(InvalidDatabaseException.class);
268287
this.thrown
@@ -271,8 +290,7 @@ private void testBrokenDataPointer(Reader reader) throws IOException, URISyntaxE
271290
reader.get(InetAddress.getByName("1.1.1.16"));
272291
}
273292

274-
private void testMetadata(Reader reader, int ipVersion,
275-
long recordSize) {
293+
private void testMetadata(Reader reader, int ipVersion, long recordSize) {
276294

277295
Metadata metadata = reader.getMetadata();
278296

@@ -291,8 +309,7 @@ private void testMetadata(Reader reader, int ipVersion,
291309
assertEquals(recordSize, metadata.recordSize);
292310
}
293311

294-
private void testIpV4(Reader reader, URI file)
295-
throws IOException {
312+
private void testIpV4(Reader reader, URI file) throws IOException {
296313

297314
for (int i = 0; i <= 5; i++) {
298315
String address = "1.1.1." + (int) Math.pow(2, i);
@@ -325,8 +342,7 @@ private void testIpV4(Reader reader, URI file)
325342
}
326343

327344
// XXX - logic could be combined with above
328-
private void testIpV6(Reader reader, URI file)
329-
throws IOException {
345+
private void testIpV6(Reader reader, URI file) throws IOException {
330346
String[] subnets = new String[] { "::1:ffff:ffff", "::2:0:0",
331347
"::2:0:40", "::2:0:50", "::2:0:58" };
332348

0 commit comments

Comments
 (0)