use of java.nio.BufferUnderflowException in project cassandra by apache.
the class SetSerializer method validateForNativeProtocol.
public void validateForNativeProtocol(ByteBuffer bytes, ProtocolVersion version) {
try {
ByteBuffer input = bytes.duplicate();
int n = readCollectionSize(input, version);
for (int i = 0; i < n; i++) elements.validate(readValue(input, version));
if (input.hasRemaining())
throw new MarshalException("Unexpected extraneous bytes after set value");
} catch (BufferUnderflowException e) {
throw new MarshalException("Not enough bytes to read a set");
}
}
use of java.nio.BufferUnderflowException in project hbase by apache.
the class MultiByteBuff method getInt.
private int getInt(int index, int itemIndex) {
ByteBuffer item = items[itemIndex];
int offsetInItem = index - this.itemBeginPos[itemIndex];
int remainingLen = item.limit() - offsetInItem;
if (remainingLen >= Bytes.SIZEOF_INT) {
return ByteBufferUtils.toInt(item, offsetInItem);
}
if (items.length - 1 == itemIndex) {
// means cur item is the last one and we wont be able to read a int. Throw exception
throw new BufferUnderflowException();
}
ByteBuffer nextItem = items[itemIndex + 1];
// Get available bytes from this item and remaining from next
int l = 0;
for (int i = offsetInItem; i < item.capacity(); i++) {
l <<= 8;
l ^= ByteBufferUtils.toByte(item, i) & 0xFF;
}
for (int i = 0; i < Bytes.SIZEOF_INT - remainingLen; i++) {
l <<= 8;
l ^= ByteBufferUtils.toByte(nextItem, i) & 0xFF;
}
return l;
}
use of java.nio.BufferUnderflowException in project cassandra by apache.
the class ListSerializer method getElement.
/**
* Returns the element at the given index in a list.
* @param serializedList a serialized list
* @param index the index to get
* @return the serialized element at the given index, or null if the index exceeds the list size
*/
public ByteBuffer getElement(ByteBuffer serializedList, int index) {
try {
ByteBuffer input = serializedList.duplicate();
int n = readCollectionSize(input, ProtocolVersion.V3);
if (n <= index)
return null;
for (int i = 0; i < index; i++) {
int length = input.getInt();
input.position(input.position() + length);
}
return readValue(input, ProtocolVersion.V3);
} catch (BufferUnderflowException e) {
throw new MarshalException("Not enough bytes to read a list");
}
}
use of java.nio.BufferUnderflowException in project flink by apache.
the class MemorySegmentTestBase method testByteBufferOverflowUnderflow.
@Test
public void testByteBufferOverflowUnderflow() {
try {
final int bbCapacity = pageSize / 10;
ByteBuffer bb = ByteBuffer.allocate(bbCapacity);
MemorySegment seg = createSegment(pageSize);
try {
seg.get(pageSize / 5, bb, pageSize / 10 + 2);
fail("should fail with an exception");
} catch (BufferOverflowException ignored) {
}
// position / limit should not have been modified
assertEquals(0, bb.position());
assertEquals(bb.capacity(), bb.limit());
try {
seg.put(pageSize / 5, bb, pageSize / 10 + 2);
fail("should fail with an exception");
} catch (BufferUnderflowException ignored) {
}
// position / limit should not have been modified
assertEquals(0, bb.position());
assertEquals(bb.capacity(), bb.limit());
int pos = bb.capacity() / 3;
int limit = 2 * bb.capacity() / 3;
bb.limit(limit);
bb.position(pos);
try {
seg.get(20, bb, bb.capacity() / 3 + 3);
fail("should fail with an exception");
} catch (BufferOverflowException ignored) {
}
// position / limit should not have been modified
assertEquals(pos, bb.position());
assertEquals(limit, bb.limit());
try {
seg.put(20, bb, bb.capacity() / 3 + 3);
fail("should fail with an exception");
} catch (BufferUnderflowException ignored) {
}
// position / limit should not have been modified
assertEquals(pos, bb.position());
assertEquals(limit, bb.limit());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of java.nio.BufferUnderflowException in project rest.li by linkedin.
the class BufferChain method remain.
private final ByteBuffer remain(int length) {
ByteBuffer buffer;
if (advanceBufferIfCurrentBufferHasNoRemaining() == false) {
throw new BufferUnderflowException();
}
int remaining = _currentBuffer.remaining();
if (remaining < length) {
// out.println("remaining(" + length + ") " + remaining);
byte[] bytes = new byte[length];
_currentBuffer.get(bytes, 0, remaining);
if (advanceBufferIfCurrentBufferHasNoRemaining() == false) {
throw new BufferUnderflowException();
}
_currentBuffer.get(bytes, remaining, length - remaining);
buffer = ByteBuffer.wrap(bytes);
buffer.order(_order);
} else {
buffer = _currentBuffer;
}
return buffer;
}
Aggregations