HBASE-26344 Fix Bug for MultiByteBuff.put(int, byte) (#3741)
Signed-off-by: Duo Zhang <zhangduo@apache.org>
This commit is contained in:
parent
c42d6ccc1a
commit
52d1dc0915
@ -181,6 +181,9 @@ public class MultiByteBuff extends ByteBuff {
|
|||||||
* Returns in which sub ByteBuffer, the given element index will be available.
|
* Returns in which sub ByteBuffer, the given element index will be available.
|
||||||
*/
|
*/
|
||||||
private int getItemIndex(int elemIndex) {
|
private int getItemIndex(int elemIndex) {
|
||||||
|
if (elemIndex < 0) {
|
||||||
|
throw new IndexOutOfBoundsException();
|
||||||
|
}
|
||||||
int index = 1;
|
int index = 1;
|
||||||
while (elemIndex >= this.itemBeginPos[index]) {
|
while (elemIndex >= this.itemBeginPos[index]) {
|
||||||
index++;
|
index++;
|
||||||
@ -721,15 +724,16 @@ public class MultiByteBuff extends ByteBuff {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Writes a byte to this MBB at the given index
|
* Writes a byte to this MBB at the given index and won't affect the position of any of the
|
||||||
* @param index
|
* buffers.
|
||||||
* @param b
|
|
||||||
* @return this object
|
* @return this object
|
||||||
|
* @throws IndexOutOfBoundsException If <tt>index</tt> is negative or not smaller than the
|
||||||
|
* {@link MultiByteBuff#limit}
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public MultiByteBuff put(int index, byte b) {
|
public MultiByteBuff put(int index, byte b) {
|
||||||
checkRefCount();
|
checkRefCount();
|
||||||
int itemIndex = getItemIndex(limit);
|
int itemIndex = getItemIndex(index);
|
||||||
ByteBuffer item = items[itemIndex];
|
ByteBuffer item = items[itemIndex];
|
||||||
item.put(index - itemBeginPos[itemIndex], b);
|
item.put(index - itemBeginPos[itemIndex], b);
|
||||||
return this;
|
return this;
|
||||||
|
@ -594,4 +594,58 @@ public class TestMultiByteBuff {
|
|||||||
assertTrue(e != null);
|
assertTrue(e != null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testPositionalPutByte() throws Exception {
|
||||||
|
ByteBuffer bb1 = ByteBuffer.allocate(50);
|
||||||
|
ByteBuffer bb2 = ByteBuffer.allocate(50);
|
||||||
|
ByteBuffer bb3 = ByteBuffer.allocate(50);
|
||||||
|
ByteBuffer bb4 = ByteBuffer.allocate(50);
|
||||||
|
MultiByteBuff srcMultiByteBuff = new MultiByteBuff(bb1, bb2, bb3, bb4);
|
||||||
|
for (int i = 1; i <= 200; i++) {
|
||||||
|
srcMultiByteBuff.put((byte) 0xff);
|
||||||
|
}
|
||||||
|
|
||||||
|
srcMultiByteBuff.put(20, (byte) 0);
|
||||||
|
byte val = srcMultiByteBuff.get(20);
|
||||||
|
assertTrue(val == 0);
|
||||||
|
|
||||||
|
srcMultiByteBuff.put(50, (byte) 0);
|
||||||
|
val = srcMultiByteBuff.get(50);
|
||||||
|
assertTrue(val == 0);
|
||||||
|
|
||||||
|
srcMultiByteBuff.put(80, (byte) 0);
|
||||||
|
val = srcMultiByteBuff.get(80);
|
||||||
|
assertTrue(val == 0);
|
||||||
|
|
||||||
|
srcMultiByteBuff.put(100, (byte) 0);
|
||||||
|
val = srcMultiByteBuff.get(100);
|
||||||
|
assertTrue(val == 0);
|
||||||
|
|
||||||
|
srcMultiByteBuff.put(121, (byte) 0);
|
||||||
|
val = srcMultiByteBuff.get(121);
|
||||||
|
assertTrue(val == 0);
|
||||||
|
|
||||||
|
srcMultiByteBuff.put(150, (byte) 0);
|
||||||
|
val = srcMultiByteBuff.get(150);
|
||||||
|
assertTrue(val == 0);
|
||||||
|
|
||||||
|
srcMultiByteBuff.put(180, (byte) 0);
|
||||||
|
val = srcMultiByteBuff.get(180);
|
||||||
|
assertTrue(val == 0);
|
||||||
|
|
||||||
|
try {
|
||||||
|
srcMultiByteBuff.put(200, (byte) 0);
|
||||||
|
fail();
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
assertTrue(e != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
srcMultiByteBuff.put(260, (byte) 0);
|
||||||
|
fail();
|
||||||
|
} catch (IndexOutOfBoundsException e) {
|
||||||
|
assertTrue(e != null);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1263,26 +1263,44 @@ public class TestOrderedBytes {
|
|||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
PositionedByteRange buff = new SimplePositionedMutableByteRange(1024);
|
PositionedByteRange buff = new SimplePositionedMutableByteRange(1024);
|
||||||
for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
|
for (Order ord : new Order[] { Order.ASCENDING, Order.DESCENDING }) {
|
||||||
int o;
|
OrderedBytes.encodeNull(buff, ord);
|
||||||
o = OrderedBytes.encodeNull(buff, ord); cnt++;
|
cnt++;
|
||||||
o = OrderedBytes.encodeNumeric(buff, negInf, ord); cnt++;
|
OrderedBytes.encodeNumeric(buff, negInf, ord);
|
||||||
o = OrderedBytes.encodeNumeric(buff, negLarge, ord); cnt++;
|
cnt++;
|
||||||
o = OrderedBytes.encodeNumeric(buff, negMed, ord); cnt++;
|
OrderedBytes.encodeNumeric(buff, negLarge, ord);
|
||||||
o = OrderedBytes.encodeNumeric(buff, negSmall, ord); cnt++;
|
cnt++;
|
||||||
o = OrderedBytes.encodeNumeric(buff, zero, ord); cnt++;
|
OrderedBytes.encodeNumeric(buff, negMed, ord);
|
||||||
o = OrderedBytes.encodeNumeric(buff, posSmall, ord); cnt++;
|
cnt++;
|
||||||
o = OrderedBytes.encodeNumeric(buff, posMed, ord); cnt++;
|
OrderedBytes.encodeNumeric(buff, negSmall, ord);
|
||||||
o = OrderedBytes.encodeNumeric(buff, posLarge, ord); cnt++;
|
cnt++;
|
||||||
o = OrderedBytes.encodeNumeric(buff, posInf, ord); cnt++;
|
OrderedBytes.encodeNumeric(buff, zero, ord);
|
||||||
o = OrderedBytes.encodeNumeric(buff, nan, ord); cnt++;
|
cnt++;
|
||||||
o = OrderedBytes.encodeInt8(buff, int8, ord); cnt++;
|
OrderedBytes.encodeNumeric(buff, posSmall, ord);
|
||||||
o = OrderedBytes.encodeInt16(buff, int16, ord); cnt++;
|
cnt++;
|
||||||
o = OrderedBytes.encodeInt32(buff, int32, ord); cnt++;
|
OrderedBytes.encodeNumeric(buff, posMed, ord);
|
||||||
o = OrderedBytes.encodeInt64(buff, int64, ord); cnt++;
|
cnt++;
|
||||||
o = OrderedBytes.encodeFloat32(buff, float32, ord); cnt++;
|
OrderedBytes.encodeNumeric(buff, posLarge, ord);
|
||||||
o = OrderedBytes.encodeFloat64(buff, float64, ord); cnt++;
|
cnt++;
|
||||||
o = OrderedBytes.encodeString(buff, text, ord); cnt++;
|
OrderedBytes.encodeNumeric(buff, posInf, ord);
|
||||||
o = OrderedBytes.encodeBlobVar(buff, blobVar, ord); cnt++;
|
cnt++;
|
||||||
|
OrderedBytes.encodeNumeric(buff, nan, ord);
|
||||||
|
cnt++;
|
||||||
|
OrderedBytes.encodeInt8(buff, int8, ord);
|
||||||
|
cnt++;
|
||||||
|
OrderedBytes.encodeInt16(buff, int16, ord);
|
||||||
|
cnt++;
|
||||||
|
OrderedBytes.encodeInt32(buff, int32, ord);
|
||||||
|
cnt++;
|
||||||
|
OrderedBytes.encodeInt64(buff, int64, ord);
|
||||||
|
cnt++;
|
||||||
|
OrderedBytes.encodeFloat32(buff, float32, ord);
|
||||||
|
cnt++;
|
||||||
|
OrderedBytes.encodeFloat64(buff, float64, ord);
|
||||||
|
cnt++;
|
||||||
|
OrderedBytes.encodeString(buff, text, ord);
|
||||||
|
cnt++;
|
||||||
|
OrderedBytes.encodeBlobVar(buff, blobVar, ord);
|
||||||
|
cnt++;
|
||||||
}
|
}
|
||||||
|
|
||||||
buff.setPosition(0);
|
buff.setPosition(0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user