Skip to content

Commit b008178

Browse files
committed
Move chunk length check from wrap to constructor
1 parent 8becd0e commit b008178

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,20 @@ public MultiBuffer(long capacity) {
5050
* @param buffers the backing buffers (cloned into an internal array)
5151
* @param chunkSize the size of each buffer chunk
5252
*/
53-
private MultiBuffer(ByteBuffer[] buffers, int chunkSize) {
53+
MultiBuffer(ByteBuffer[] buffers, int chunkSize) {
54+
for (int i = 0; i < buffers.length; i++) {
55+
ByteBuffer chunk = buffers[i];
56+
if (chunk.capacity() == chunkSize) {
57+
continue;
58+
}
59+
if (i == buffers.length - 1) {
60+
// The last chunk can have a different size
61+
continue;
62+
}
63+
throw new IllegalArgumentException("Chunk at index " + i
64+
+ " is smaller than expected chunk size");
65+
}
66+
5467
this.buffers = buffers.clone();
5568
long capacity = 0;
5669
for (ByteBuffer buffer : buffers) {
@@ -306,29 +319,18 @@ public String decode(CharsetDecoder decoder)
306319
}
307320

308321
/**
309-
* Wraps the given byte arrays in a new {@code MultiBuffer}.
322+
* Wraps the given {@link ByteBuffer}s in a new {@code MultiBuffer}.
310323
*
311-
* <p>If any array exceeds {@link #DEFAULT_CHUNK_SIZE}, it will be split across multiple
312-
* underlying {@link ByteBuffer}s. The data is copied into new buffers so the
313-
* returned {@code MultiBuffer} is fully independent.
324+
* <p>All chunks must have size {@link #DEFAULT_CHUNK_SIZE}, except that
325+
* the final chunk may be smaller. The buffers are used directly and not
326+
* copied.
314327
*
315-
* @param chunks the byte arrays to wrap
316-
* @return a new {@code MultiBuffer} backed by the arrays
328+
* @param chunks the backing buffers
329+
* @return a new {@code MultiBuffer} backed by the given buffers
330+
* @throws IllegalArgumentException if a non-final chunk is smaller than
331+
* {@link #DEFAULT_CHUNK_SIZE}
317332
*/
318333
public static MultiBuffer wrap(ByteBuffer[] chunks) {
319-
for (int i = 0; i < chunks.length; i++) {
320-
ByteBuffer chunk = chunks[i];
321-
if (chunk.capacity() == DEFAULT_CHUNK_SIZE) {
322-
continue;
323-
}
324-
if (i == chunks.length - 1) {
325-
// The last chunk can have a different size
326-
continue;
327-
}
328-
throw new IllegalArgumentException("Chunk at index " + i
329-
+ " is smaller than expected chunk size");
330-
}
331-
332334
return new MultiBuffer(chunks, DEFAULT_CHUNK_SIZE);
333335
}
334336

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -241,22 +241,22 @@ public void testDuplicate() {
241241
@Test
242242
public void testWrapValidChunks() {
243243
ByteBuffer[] chunks = new ByteBuffer[] {
244-
ByteBuffer.allocateDirect(MultiBuffer.DEFAULT_CHUNK_SIZE),
245-
ByteBuffer.allocateDirect(500)
244+
ByteBuffer.allocate(8),
245+
ByteBuffer.allocate(3)
246246
};
247247

248-
MultiBuffer buffer = MultiBuffer.wrap(chunks);
249-
assertEquals(MultiBuffer.DEFAULT_CHUNK_SIZE + 500, buffer.capacity());
248+
MultiBuffer buffer = new MultiBuffer(chunks, 8);
249+
assertEquals(11, buffer.capacity());
250250
}
251251

252252
@Test
253253
public void testWrapInvalidChunkSize() {
254254
ByteBuffer[] chunks = new ByteBuffer[] {
255-
ByteBuffer.allocateDirect(500),
256-
ByteBuffer.allocateDirect(MultiBuffer.DEFAULT_CHUNK_SIZE)
255+
ByteBuffer.allocate(3),
256+
ByteBuffer.allocate(8)
257257
};
258258

259-
assertThrows(IllegalArgumentException.class, () -> MultiBuffer.wrap(chunks));
259+
assertThrows(IllegalArgumentException.class, () -> new MultiBuffer(chunks, 8));
260260
}
261261

262262
@Test

0 commit comments

Comments
 (0)