Search in sources :

Example 81 with BufferUnderflowException

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");
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 82 with BufferUnderflowException

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;
}
Also used : ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 83 with BufferUnderflowException

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");
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 84 with BufferUnderflowException

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());
    }
}
Also used : BufferOverflowException(java.nio.BufferOverflowException) ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException) BufferOverflowException(java.nio.BufferOverflowException) IOException(java.io.IOException) EOFException(java.io.EOFException) BufferUnderflowException(java.nio.BufferUnderflowException) Test(org.junit.Test)

Example 85 with BufferUnderflowException

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;
}
Also used : ByteBuffer(java.nio.ByteBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Aggregations

BufferUnderflowException (java.nio.BufferUnderflowException)123 ByteBuffer (java.nio.ByteBuffer)70 IOException (java.io.IOException)25 ArrayList (java.util.ArrayList)22 DirectByteBuffer (java.nio.DirectByteBuffer)15 Test (org.junit.Test)14 CertificateException (java.security.cert.CertificateException)12 X509Certificate (java.security.cert.X509Certificate)11 BigInteger (java.math.BigInteger)10 ByteSource (org.apache.geode.internal.tcp.ByteBufferInputStream.ByteSource)9 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)9 SerializationTest (org.apache.geode.test.junit.categories.SerializationTest)9 UnitTest (org.apache.geode.test.junit.categories.UnitTest)9 CharBuffer (java.nio.CharBuffer)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 FloatBuffer (java.nio.FloatBuffer)6 CertificateFactory (java.security.cert.CertificateFactory)6 HashMap (java.util.HashMap)6 ArrayMap (android.util.ArrayMap)5 HSIconFileElement (com.android.anqp.HSIconFileElement)5