Skip to content

Commit 811112c

Browse files
committed
Fix bug in ByteBankByteArray(int) ctor
The size was being initialized as non-zero. Thanks to Gabriel Einsdorf for noticing.
1 parent 7c345eb commit 811112c

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/main/java/org/scijava/io/ByteArrayByteBank.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public ByteArrayByteBank() {
5858
* @param initialCapacity the initial capacity of this {@link ByteBank}
5959
*/
6060
public ByteArrayByteBank(final int initialCapacity) {
61-
this(new ByteArray(initialCapacity));
61+
this(emptyByteArrayOfCapacity(initialCapacity));
6262
}
6363

6464
/**
@@ -148,4 +148,10 @@ public long size() {
148148
private void updateSize(final long newSize) {
149149
size = newSize > size ? newSize : size;
150150
}
151+
152+
private static ByteArray emptyByteArrayOfCapacity(final int capacity) {
153+
final ByteArray byteArray = new ByteArray(new byte[capacity]);
154+
byteArray.setSize(0);
155+
return byteArray;
156+
}
151157
}

src/test/java/org/scijava/io/ByteArrayByteBankTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
package org.scijava.io;
3434

35+
import org.junit.Test;
36+
3537
/**
3638
* Tests {@link ByteArrayByteBank}
3739
*
@@ -44,4 +46,10 @@ public class ByteArrayByteBankTest extends ByteBankTest {
4446
public ByteBank createByteBank() {
4547
return new ByteArrayByteBank();
4648
}
49+
50+
@Test(expected = ArrayIndexOutOfBoundsException.class)
51+
public void testCreateByteBankWithCapacity() {
52+
ByteArrayByteBank bank = new ByteArrayByteBank(50);
53+
bank.getByte(0);
54+
}
4755
}

0 commit comments

Comments
 (0)