Skip to content

Commit 57d8c60

Browse files
committed
Fix Reader testing streams
1 parent 4a4a80d commit 57d8c60

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,16 @@ final class BufferHolder {
4949
* @throws NullPointerException if you provide a NULL InputStream
5050
*/
5151
BufferHolder(InputStream stream) throws IOException {
52+
this(stream, MultiBuffer.DEFAULT_CHUNK_SIZE);
53+
}
54+
55+
BufferHolder(InputStream stream, int chunkSize) throws IOException {
5256
if (null == stream) {
5357
throw new NullPointerException("Unable to use a NULL InputStream");
5458
}
5559
List<ByteBuffer> chunks = new ArrayList<>();
5660
long total = 0;
57-
byte[] tmp = new byte[MultiBuffer.DEFAULT_CHUNK_SIZE];
61+
byte[] tmp = new byte[chunkSize];
5862
int read;
5963

6064
while (-1 != (read = stream.read(tmp))) {
@@ -65,7 +69,7 @@ final class BufferHolder {
6569
total += read;
6670
}
6771

68-
if (total <= MultiBuffer.DEFAULT_CHUNK_SIZE) {
72+
if (total <= chunkSize) {
6973
byte[] data = new byte[(int) total];
7074
int pos = 0;
7175
for (ByteBuffer chunk : chunks) {
@@ -74,7 +78,7 @@ final class BufferHolder {
7478
}
7579
this.buffer = SingleBuffer.wrap(data);
7680
} else {
77-
this.buffer = MultiBuffer.wrap(chunks.toArray(new ByteBuffer[0]));
81+
this.buffer = new MultiBuffer(chunks.toArray(new ByteBuffer[0]), chunkSize);
7882
}
7983
}
8084

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public Reader(InputStream source) throws IOException {
8080
this(source, NoCache.getInstance());
8181
}
8282

83+
Reader(InputStream source, int chunkSize) throws IOException {
84+
this(source, NoCache.getInstance(), chunkSize);
85+
}
86+
8387
/**
8488
* Constructs a Reader with the specified backing cache, as if in mode
8589
* {@link FileMode#MEMORY}, without using a <code>File</code> instance.
@@ -89,7 +93,11 @@ public Reader(InputStream source) throws IOException {
8993
* @throws IOException if there is an error reading from the Stream.
9094
*/
9195
public Reader(InputStream source, NodeCache cache) throws IOException {
92-
this(new BufferHolder(source), "<InputStream>", cache);
96+
this(source, cache, MultiBuffer.DEFAULT_CHUNK_SIZE);
97+
}
98+
99+
Reader(InputStream source, NodeCache cache, int chunkSize) throws IOException {
100+
this(new BufferHolder(source, chunkSize), "<InputStream>", cache);
93101
}
94102

95103
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void multipleMmapOpens() throws InterruptedException,
2929
@Test
3030
public void streamThreadTest() throws IOException, InterruptedException,
3131
ExecutionException {
32-
try (Reader reader = new Reader(ReaderTest.getStream("MaxMind-DB-test-decoder.mmdb"))) {
32+
try (Reader reader = new Reader(ReaderTest.getStream("MaxMind-DB-test-decoder.mmdb"), 2048)) {
3333
MultiThreadedTest.threadTest(reader);
3434
}
3535
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ public void testNoIpV4SearchTreeFile() throws IOException {
459459

460460
@Test
461461
public void testNoIpV4SearchTreeStream() throws IOException {
462-
this.testReader = new Reader(getStream("MaxMind-DB-no-ipv4-search-tree.mmdb"));
462+
this.testReader = new Reader(getStream("MaxMind-DB-no-ipv4-search-tree.mmdb"), 2048);
463463
this.testNoIpV4SearchTree(this.testReader);
464464
}
465465

@@ -480,7 +480,7 @@ public void testDecodingTypesFile() throws IOException {
480480

481481
@Test
482482
public void testDecodingTypesStream() throws IOException {
483-
this.testReader = new Reader(getStream("MaxMind-DB-test-decoder.mmdb"));
483+
this.testReader = new Reader(getStream("MaxMind-DB-test-decoder.mmdb"), 2048);
484484
this.testDecodingTypes(this.testReader, true);
485485
this.testDecodingTypesIntoModelObject(this.testReader, true);
486486
this.testDecodingTypesIntoModelObjectBoxed(this.testReader, true);
@@ -1140,7 +1140,7 @@ public void testBrokenDatabaseFile() throws IOException {
11401140

11411141
@Test
11421142
public void testBrokenDatabaseStream() throws IOException {
1143-
this.testReader = new Reader(getStream("GeoIP2-City-Test-Broken-Double-Format.mmdb"));
1143+
this.testReader = new Reader(getStream("GeoIP2-City-Test-Broken-Double-Format.mmdb"), 2048);
11441144
this.testBrokenDatabase(this.testReader);
11451145
}
11461146

@@ -1160,7 +1160,7 @@ public void testBrokenSearchTreePointerFile() throws IOException {
11601160

11611161
@Test
11621162
public void testBrokenSearchTreePointerStream() throws IOException {
1163-
this.testReader = new Reader(getStream("MaxMind-DB-test-broken-pointers-24.mmdb"));
1163+
this.testReader = new Reader(getStream("MaxMind-DB-test-broken-pointers-24.mmdb"), 2048);
11641164
this.testBrokenSearchTreePointer(this.testReader);
11651165
}
11661166

@@ -1178,7 +1178,7 @@ public void testBrokenDataPointerFile() throws IOException {
11781178

11791179
@Test
11801180
public void testBrokenDataPointerStream() throws IOException {
1181-
this.testReader = new Reader(getStream("MaxMind-DB-test-broken-pointers-24.mmdb"));
1181+
this.testReader = new Reader(getStream("MaxMind-DB-test-broken-pointers-24.mmdb"), 2048);
11821182
this.testBrokenDataPointer(this.testReader);
11831183
}
11841184

0 commit comments

Comments
 (0)