Skip to content

Commit b459699

Browse files
committed
BytesLocation: use ByteBank for backing bytes
This is more flexible than ByteBuffer, since ByteBank is resizable.
1 parent 1951551 commit b459699

File tree

2 files changed

+63
-19
lines changed

2 files changed

+63
-19
lines changed

src/main/java/org/scijava/io/location/BytesLocation.java

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,71 @@
3131

3232
package org.scijava.io.location;
3333

34-
import java.nio.ByteBuffer;
34+
import org.scijava.io.ByteArrayByteBank;
35+
import org.scijava.io.ByteBank;
36+
import org.scijava.util.ByteArray;
3537

3638
/**
37-
* {@link Location} backed by a {@link ByteBuffer}.
39+
* {@link Location} backed by a {@link ByteBank}.
3840
*
3941
* @author Curtis Rueden
42+
* @author Gabriel Einsdorf
4043
*/
4144
public class BytesLocation extends AbstractLocation {
4245

43-
private final ByteBuffer bytes;
46+
private final ByteBank bytes;
4447

45-
public BytesLocation(final ByteBuffer bytes) {
48+
/**
49+
* Creates a {@link BytesLocation} backed by the specified
50+
* {@link ByteBank}.
51+
*
52+
* @param bytes the {@link ByteBank} that will back this {@link Location}
53+
*/
54+
public BytesLocation(final ByteBank bytes) {
4655
this.bytes = bytes;
4756
}
4857

58+
/**
59+
* Creates a {@link BytesLocation} backed by a {@link ByteArrayByteBank}
60+
* with the specified initial capacity.
61+
*/
62+
public BytesLocation(final int initialCapacity) {
63+
this.bytes = new ByteArrayByteBank(initialCapacity);
64+
}
65+
66+
/**
67+
* Creates a {@link BytesLocation} backed by a {@link ByteArrayByteBank}
68+
* that wraps the specified {@link ByteArray}.
69+
*/
70+
public BytesLocation(final ByteArray bytes) {
71+
this.bytes = new ByteArrayByteBank(bytes);
72+
}
73+
74+
/**
75+
* Creates a {@link BytesLocation} backed by a {@link ByteArrayByteBank}
76+
* which wraps the specified array.
77+
*
78+
* @param bytes the array to wrap
79+
*/
4980
public BytesLocation(final byte[] bytes) {
50-
this(ByteBuffer.wrap(bytes));
81+
this.bytes = new ByteArrayByteBank(bytes);
5182
}
5283

53-
public BytesLocation(final byte[] bytes, final int offset, final int length) {
54-
this(ByteBuffer.wrap(bytes, offset, length));
84+
/**
85+
* Creates a {@link BytesLocation} backed by a {@link ByteArrayByteBank} with
86+
* the specified initial capacity.
87+
*/
88+
public BytesLocation(final byte[] bytes, final int offset,
89+
final int length)
90+
{
91+
this.bytes = new ByteArrayByteBank(length);
92+
this.bytes.setBytes(0l, bytes, offset, length);
5593
}
5694

5795
// -- BytesLocation methods --
5896

59-
/** Gets the associated {@link ByteBuffer}. */
60-
public ByteBuffer getByteBuffer() {
97+
/** Gets the backing {@link ByteBank}. */
98+
public ByteBank getByteBank() {
6199
return bytes;
62100
}
63101

src/test/java/org/scijava/io/location/BytesLocationTest.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@
3131

3232
package org.scijava.io.location;
3333

34+
import static org.junit.Assert.assertArrayEquals;
3435
import static org.junit.Assert.assertEquals;
35-
import static org.junit.Assert.assertSame;
3636

3737
import org.junit.Test;
38-
import org.scijava.io.location.BytesLocation;
3938

4039
/**
4140
* Tests {@link BytesLocation}.
@@ -47,22 +46,29 @@ public class BytesLocationTest {
4746
/** Tests {@link BytesLocation#BytesLocation(byte[])}. */
4847
@Test
4948
public void testBytes() {
50-
final byte[] digits = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9};
49+
final byte[] digits = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9 };
5150
final BytesLocation loc = new BytesLocation(digits);
52-
assertSame(digits, loc.getByteBuffer().array());
53-
assertEquals(0, loc.getByteBuffer().position());
54-
assertEquals(digits.length, loc.getByteBuffer().remaining());
51+
52+
final byte[] testDigits = new byte[digits.length];
53+
loc.getByteBank().getBytes(0, testDigits);
54+
assertEquals(digits.length, loc.getByteBank().getMaxPos());
55+
assertArrayEquals(digits, testDigits);
5556
}
5657

5758
/** Tests {@link BytesLocation#BytesLocation(byte[], int, int)}. */
5859
@Test
5960
public void testBytesOffsetLength() {
60-
final byte[] digits = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9};
61+
final byte[] digits = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9 };
6162
final int offset = 3, length = 5;
6263
final BytesLocation loc = new BytesLocation(digits, offset, length);
63-
assertSame(digits, loc.getByteBuffer().array());
64-
assertEquals(offset, loc.getByteBuffer().position());
65-
assertEquals(length, loc.getByteBuffer().remaining());
64+
65+
final byte[] testDigits = new byte[digits.length];
66+
loc.getByteBank().getBytes(0, testDigits);
67+
assertEquals(length, loc.getByteBank().getMaxPos());
68+
69+
final byte[] expectedDigits = new byte[digits.length];
70+
System.arraycopy(digits, offset, expectedDigits, 0, length);
71+
assertArrayEquals(expectedDigits, testDigits);
6672
}
6773

6874
}

0 commit comments

Comments
 (0)