add Buffer capacity check for type UInt64 (#454)

This commit is contained in:
OlivierSalasc
2018-09-27 14:49:25 +02:00
committed by Jeroen van Erp
parent 4de9f8ab9f
commit 17c368f9c2
2 changed files with 25 additions and 0 deletions

View File

@@ -372,6 +372,7 @@ public class Buffer<T extends Buffer<T>> {
@SuppressWarnings("unchecked")
private T putUInt64Unchecked(long uint64) {
ensureCapacity(8);
data[wpos++] = (byte) (uint64 >> 56);
data[wpos++] = (byte) (uint64 >> 48);
data[wpos++] = (byte) (uint64 >> 40);

View File

@@ -146,4 +146,28 @@ public class BufferTest {
assertArrayEquals("Value: " + value, bytesLong, bytesBigInt);
}
}
@Test
public void shouldExpandCapacityOfUInt32(){
PlainBuffer buf = new PlainBuffer();
for(int i=0;i<Buffer.DEFAULT_SIZE+1;i+=4) {
buf.putUInt32(1l);
}
/* Buffer should have been expanded at this point*/
assertEquals(Buffer.DEFAULT_SIZE*2,buf.data.length);
}
@Test
public void shouldExpandCapacityOfUInt64(){
BigInteger bigUint64 = BigInteger.valueOf(Long.MAX_VALUE);
PlainBuffer buf = new PlainBuffer();
assertEquals(Buffer.DEFAULT_SIZE,buf.data.length);
for(int i=0;i<Buffer.DEFAULT_SIZE+1;i+=8) {
buf.putUInt64(bigUint64.longValue());
}
/* Buffer should have been expanded at this point*/
assertEquals(Buffer.DEFAULT_SIZE*2,buf.data.length);
}
}