Skip to content

Commit 598af21

Browse files
committed
Make the ByteBank consistent
Treatment and tracking of getMaxPos() was inconsistent in the codebase. It has been renamed to size(), and reflects the index to the bank's END, rather than the index of the last populated index. Personally, I find this convention less confusing.
1 parent 6844914 commit 598af21

File tree

6 files changed

+61
-57
lines changed

6 files changed

+61
-57
lines changed

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

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,50 +36,49 @@
3636

3737
/**
3838
* {@link ByteBank} implementation backed by a {@link ByteArray}. Self-growing
39-
* up to a maximum capacity of {@link Integer#MAX_VALUE}
39+
* up to a maximum capacity of {@link Integer#MAX_VALUE}.
4040
*
4141
* @author Gabriel Einsdorf
4242
*/
4343
public class ByteArrayByteBank implements ByteBank {
4444

4545
private final ByteArray buffer;
46-
private long maxBufferedPos = -1;
46+
private long size;
4747

4848
/**
49-
* Creates a {@link ByteArrayByteBank}
49+
* Creates a {@link ByteArrayByteBank}.
5050
*/
5151
public ByteArrayByteBank() {
52-
buffer = new ByteArray();
52+
this(new ByteArray());
5353
}
5454

5555
/**
56-
* Creates a {@link ByteArrayByteBank} with the specified initial capacity
56+
* Creates a {@link ByteArrayByteBank} with the specified initial capacity.
5757
*
5858
* @param initialCapacity the initial capacity of this {@link ByteBank}
5959
*/
6060
public ByteArrayByteBank(final int initialCapacity) {
61-
buffer = new ByteArray(initialCapacity);
61+
this(new ByteArray(initialCapacity));
6262
}
6363

6464
/**
65-
* Creates a {@link ByteArrayByteBank} that wraps the specified
66-
* {@link ByteArray}.
65+
* Creates a {@link ByteArrayByteBank} that wraps the provided byte array.
6766
*
68-
* @param bytes the {@link ByteArray} to wrap
67+
* @param bytes the bytes to wrap
6968
*/
70-
public ByteArrayByteBank(final ByteArray bytes) {
71-
buffer = bytes;
72-
maxBufferedPos = bytes.size();
69+
public ByteArrayByteBank(final byte[] bytes) {
70+
this(new ByteArray(bytes));
7371
}
7472

7573
/**
76-
* Creates a {@link ByteArrayByteBank} that wraps the provided byte array
74+
* Creates a {@link ByteArrayByteBank} that wraps the specified
75+
* {@link ByteArray}.
7776
*
78-
* @param bytes the bytes to wrap
77+
* @param bytes the {@link ByteArray} to wrap
7978
*/
80-
public ByteArrayByteBank(final byte[] bytes) {
81-
buffer = new ByteArray(bytes);
82-
maxBufferedPos = bytes.length;
79+
public ByteArrayByteBank(final ByteArray bytes) {
80+
buffer = bytes;
81+
size = bytes.size();
8382
}
8483

8584
@Override
@@ -93,13 +92,13 @@ public void setBytes(final long startpos, final byte[] bytes,
9392
{
9493
// ensure we have space
9594
checkWritePos(startpos, startpos + length);
96-
final int neededCapacity = (int) (Math.max(maxBufferedPos, 0) + length);
95+
final int neededCapacity = (int) (size + length);
9796
buffer.ensureCapacity(neededCapacity);
9897

9998
// copy the data
10099
System.arraycopy(bytes, offset, buffer.getArray(), (int) startpos, length);
101100
buffer.setSize(neededCapacity);
102-
updateMaxPos(startpos + length - 1);
101+
updateSize(startpos + length);
103102
}
104103

105104
@Override
@@ -111,17 +110,13 @@ public void setByte(final long pos, final byte b) {
111110
buffer.setSize((int) (pos + 1));
112111
}
113112
buffer.setValue((int) pos, b);
114-
updateMaxPos(pos);
115-
}
116-
117-
private void updateMaxPos(final long pos) {
118-
maxBufferedPos = pos > maxBufferedPos ? pos : maxBufferedPos;
113+
updateSize(pos + 1);
119114
}
120115

121116
@Override
122117
public void clear() {
123118
buffer.clear();
124-
maxBufferedPos = 0;
119+
size = 0;
125120
}
126121

127122
@Override
@@ -138,13 +133,19 @@ public int getBytes(final long startPos, final byte[] b, final int offset,
138133
{
139134
checkReadPos(startPos, startPos + length);
140135
// ensure we don't try to read data which is not in the buffer
141-
final int readLength = (int) Math.min(getMaxPos() - startPos + 1, length);
136+
final int readLength = (int) Math.min(size() - startPos, length);
142137
System.arraycopy(buffer.getArray(), (int) startPos, b, offset, readLength);
143138
return readLength;
144139
}
145140

146141
@Override
147-
public long getMaxPos() {
148-
return maxBufferedPos;
142+
public long size() {
143+
return size;
144+
}
145+
146+
// -- Helper methods --
147+
148+
private void updateSize(final long newSize) {
149+
size = newSize > size ? newSize : size;
149150
}
150151
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ default void appendBytes(byte[] bytes, int length) {
9292
* @param length the number of elements to append from the bytes array
9393
*/
9494
default void appendBytes(byte[] bytes, int offset, int length) {
95-
setBytes(getMaxPos() + 1, bytes, offset, length);
95+
setBytes(size(), bytes, offset, length);
9696
}
9797

9898
/**
@@ -103,9 +103,9 @@ default void appendBytes(byte[] bytes, int offset, int length) {
103103
*/
104104
default void checkReadPos(final long start, final long end) {
105105
basicRangeCheck(start, end);
106-
if (start > getMaxPos()) {
106+
if (start > size()) {
107107
throw new IndexOutOfBoundsException("Requested position: " + start +
108-
" is larger than the maximally buffered postion: " + getMaxPos());
108+
" is outside the buffer: " + size());
109109
}
110110
}
111111

@@ -117,18 +117,18 @@ default void checkReadPos(final long start, final long end) {
117117
* @throws IndexOutOfBoundsException if
118118
*/
119119
default void checkWritePos(final long start, final long end) {
120-
if (start > getMaxPos() + 1) { // we can't have holes in the buffer
120+
if (start > size() + 1) { // we can't have holes in the buffer
121121
throw new IndexOutOfBoundsException("Requested start position: " + start +
122122
" would leave a hole in the buffer, largest legal position is: " +
123-
getMaxPos() + 1);
123+
size());
124124
}
125125
if (end < start) {
126126
throw new IllegalArgumentException(
127127
"Invalid range, end is smaller than start!");
128128
}
129129
if (end > getMaxBufferSize()) {
130-
throw new IndexOutOfBoundsException("Requested postion " + end +
131-
" is larger than the maximal buffer size: " + getMaxPos());
130+
throw new IndexOutOfBoundsException("Requested position " + end +
131+
" is larger than the maximal buffer size: " + getMaxBufferSize());
132132
}
133133
}
134134

@@ -139,9 +139,9 @@ default void checkWritePos(final long start, final long end) {
139139
* @param end the end of the range
140140
*/
141141
default void basicRangeCheck(final long start, final long end) {
142-
if (start > getMaxPos()) {
143-
throw new IndexOutOfBoundsException("Requested postion " + start +
144-
" is larger than the maximal buffer size: " + getMaxPos());
142+
if (start > size()) {
143+
throw new IndexOutOfBoundsException("Requested position: " + start +
144+
" is outside the buffer: " + size());
145145
}
146146
if (end < start) {
147147
throw new IllegalArgumentException(
@@ -155,9 +155,9 @@ default void basicRangeCheck(final long start, final long end) {
155155
void clear();
156156

157157
/**
158-
* @return the position of the last byte in this ByteBank
158+
* @return the offset which follows the last byte stored in this ByteBank
159159
*/
160-
long getMaxPos();
160+
long size();
161161

162162
/**
163163
* Sets the byte at the given position

src/main/java/org/scijava/io/handle/BytesHandle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public long offset() {
6060

6161
@Override
6262
public long length() {
63-
return bytes().getMaxPos();
63+
return bytes().size();
6464
}
6565

6666
@Override

src/main/java/org/scijava/io/nio/ByteBufferByteBank.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class ByteBufferByteBank implements ByteBank {
4949

5050
private ByteBuffer buffer;
5151

52-
private int maxBufferedPos = -1;
52+
private int size;
5353

5454
private Function<Integer, ByteBuffer> provider;
5555

@@ -86,15 +86,15 @@ public void setBytes(final long startpos, final byte[] bytes,
8686
{
8787
// ensure we have space
8888
checkWritePos(startpos, startpos + length);
89-
final int neededCapacity = Math.max(maxBufferedPos, 0) + length;
89+
final int neededCapacity = size + length;
9090
ensureCapacity(neededCapacity);
9191

9292
// copy the data
9393
buffer.position((int) startpos);
9494
buffer.put(bytes, offset, length);
9595

9696
// update the maxpos
97-
updateMaxPos(startpos + length - 1);
97+
updateSize(startpos + length);
9898
}
9999

100100
@Override
@@ -104,17 +104,13 @@ public void setByte(final long pos, final byte b) {
104104
ensureCapacity((int) pos + 1);
105105
}
106106
buffer.put((int) pos, b);
107-
updateMaxPos(pos);
108-
}
109-
110-
private void updateMaxPos(final long pos) {
111-
maxBufferedPos = (int) (pos > maxBufferedPos ? pos : maxBufferedPos);
107+
updateSize(pos + 1);
112108
}
113109

114110
@Override
115111
public void clear() {
116112
buffer.clear();
117-
maxBufferedPos = 0;
113+
size = 0;
118114
}
119115

120116
@Override
@@ -132,16 +128,16 @@ public int getBytes(final long startPos, final byte[] b, final int offset,
132128
{
133129
checkReadPos(startPos, startPos + length);
134130
// ensure we don't try to read data which is not in the buffer
135-
final int readLength = (int) Math.min(getMaxPos() - startPos + 1, length);
131+
final int readLength = (int) Math.min(size() - startPos, length);
136132
buffer.position((int) startPos);
137133
buffer.get(b, offset, readLength);
138134

139135
return readLength;
140136
}
141137

142138
@Override
143-
public long getMaxPos() {
144-
return maxBufferedPos;
139+
public long size() {
140+
return size;
145141
}
146142

147143
@Override
@@ -155,6 +151,8 @@ public boolean isReadOnly() {
155151
className.equals("java.nio.DirectByteBufferR");
156152
}
157153

154+
// -- Helper methods --
155+
158156
private void ensureCapacity(final int minCapacity) {
159157
final int oldCapacity = buffer.capacity();
160158
if (minCapacity <= oldCapacity) return; // no need to grow
@@ -177,4 +175,9 @@ private void ensureCapacity(final int minCapacity) {
177175
newBuffer.put(buffer);
178176
buffer = newBuffer;
179177
}
178+
179+
private void updateSize(final long newSize) {
180+
size = (int) (newSize > size ? newSize : size);
181+
}
182+
180183
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ public void testSetGetByte() {
9393
@Test
9494
public void testClear() {
9595
bank.setBytes(0, testBytes, 0, testBytes.length);
96-
assertEquals(testBytes.length - 1, bank.getMaxPos());
96+
assertEquals(testBytes.length, bank.size());
9797

9898
bank.clear();
99-
assertEquals(0, bank.getMaxPos());
99+
assertEquals(0, bank.size());
100100
}
101101

102102
@Test

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void testBytes() {
5252

5353
final byte[] testDigits = new byte[digits.length];
5454
loc.getByteBank().getBytes(0, testDigits);
55-
assertEquals(digits.length, loc.getByteBank().getMaxPos());
55+
assertEquals(digits.length, loc.getByteBank().size());
5656
assertArrayEquals(digits, testDigits);
5757
}
5858

@@ -65,7 +65,7 @@ public void testBytesOffsetLength() {
6565

6666
final byte[] testDigits = new byte[digits.length];
6767
loc.getByteBank().getBytes(0, testDigits);
68-
assertEquals(length - 1, loc.getByteBank().getMaxPos());
68+
assertEquals(length, loc.getByteBank().size());
6969

7070
final byte[] expectedDigits = new byte[digits.length];
7171
System.arraycopy(digits, offset, expectedDigits, 0, length);

0 commit comments

Comments
 (0)