Skip to content

Commit aafbae6

Browse files
committed
Change MultiBuffer wrap to actually wrap chunks
1 parent 66a3c79 commit aafbae6

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.IOException;
66
import java.io.InputStream;
77
import java.io.RandomAccessFile;
8+
import java.nio.ByteBuffer;
89
import java.nio.channels.FileChannel;
910
import java.util.ArrayList;
1011
import java.util.List;
@@ -51,25 +52,26 @@ final class BufferHolder {
5152
if (null == stream) {
5253
throw new NullPointerException("Unable to use a NULL InputStream");
5354
}
54-
final int chunk_size = 16 * 1024;
55-
List<byte[]> chunks = new ArrayList<>();
55+
final int chunkSize = Integer.MAX_VALUE;
56+
List<ByteBuffer> chunks = new ArrayList<>();
5657
long total = 0;
58+
byte[] tmp = new byte[chunkSize];
5759
int read;
58-
byte[] tmp = new byte[chunk_size];
5960

6061
while (-1 != (read = stream.read(tmp))) {
61-
byte[] copy = new byte[read];
62-
System.arraycopy(tmp, 0, copy, 0, read);
63-
chunks.add(copy);
62+
ByteBuffer chunk = ByteBuffer.allocateDirect(read);
63+
chunk.put(tmp, 0, read);
64+
chunk.flip();
65+
chunks.add(chunk);
6466
total += read;
6567
}
6668

6769
if (total <= Integer.MAX_VALUE) {
6870
byte[] data = new byte[(int) total];
6971
int pos = 0;
70-
for (byte[] chunk : chunks) {
71-
System.arraycopy(chunk, 0, data, pos, chunk.length);
72-
pos += chunk.length;
72+
for (ByteBuffer chunk : chunks) {
73+
System.arraycopy(chunk.array(), 0, data, pos, chunk.capacity());
74+
pos += chunk.capacity();
7375
}
7476
this.buffer = SingleBuffer.wrap(data);
7577
} else {

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -272,22 +272,21 @@ public String decode(CharsetDecoder decoder)
272272
* @param chunks the byte arrays to wrap
273273
* @return a new {@code MultiBuffer} backed by the arrays
274274
*/
275-
public static MultiBuffer wrap(List<byte[]> chunks) {
276-
List<ByteBuffer> buffers = new ArrayList<>();
277-
278-
for (byte[] chunk : chunks) {
279-
int offset = 0;
280-
int remaining = chunk.length;
281-
282-
if (remaining > 0) {
283-
ByteBuffer buf = ByteBuffer.allocate(remaining);
284-
buf.put(chunk, offset, remaining);
285-
buf.flip();
286-
buffers.add(buf);
275+
public static MultiBuffer wrap(List<ByteBuffer> chunks) {
276+
for (int i = 0; i < chunks.size(); i++) {
277+
ByteBuffer chunk = chunks.get(i);
278+
if (chunk.capacity() == CHUNK_SIZE) {
279+
continue;
287280
}
281+
if (i == chunks.size() - 1) {
282+
// The last chunk can have a different size
283+
continue;
284+
}
285+
throw new IllegalArgumentException("Chunk at index " + i
286+
+ " is smaller than expected chunk size");
288287
}
289288

290-
return new MultiBuffer(buffers);
289+
return new MultiBuffer(chunks);
291290
}
292291

293292
/**

0 commit comments

Comments
 (0)