use of java.nio.BufferUnderflowException in project rest.li by linkedin.
the class BufferChain method get.
/**
* Return a {@link ByteBuffer} filled with the specified number of bytes
* from {@link BufferChain}.
* <p>
* The range of the array to be filled is specified by an offset and length.
* The current position is advanced by the number of bytes filled even if
* {@link BufferUnderflowException} is thrown.
* <p>
* @param length provides the number of bytes to be filled.
* @return {@code this}.
* @throws BufferUnderflowException if the buffer chain is exhausted before filling
* the specified number of bytes.
*/
public ByteBuffer get(int length) {
ByteBuffer buffer;
if (advanceBufferIfCurrentBufferHasNoRemaining() == false) {
throw new BufferUnderflowException();
}
int remaining = _currentBuffer.remaining();
if (remaining < length) {
buffer = ByteBuffer.allocate(length);
byte[] dst = buffer.array();
int offset = buffer.arrayOffset();
int more = length;
while (more > 0 && advanceBufferIfCurrentBufferHasNoRemaining()) {
_currentBuffer = _bufferList.get(_currentIndex);
remaining = _currentBuffer.remaining();
if (remaining > more) {
remaining = more;
}
_currentBuffer.get(dst, offset, remaining);
offset += remaining;
more -= remaining;
}
if (more > 0) {
throw new BufferUnderflowException();
}
} else {
buffer = _currentBuffer.slice();
buffer.limit(length);
_currentBuffer.position(_currentBuffer.position() + length);
}
buffer.flip();
return buffer;
}
use of java.nio.BufferUnderflowException in project neo4j by neo4j.
the class EphemeralFileSystemAbstractionTest method verifyFileIsEitherEmptyOrContainsLongIntegerValueOne.
private void verifyFileIsEitherEmptyOrContainsLongIntegerValueOne(StoreChannel channel) {
try {
long claimedSize = channel.size();
ByteBuffer buffer = allocateDirect(8);
channel.read(buffer, 0);
buffer.flip();
if (claimedSize == 8) {
assertEquals(1, buffer.getLong());
} else {
try {
buffer.getLong();
fail("Should have thrown an exception");
} catch (BufferUnderflowException e) {
// expected
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of java.nio.BufferUnderflowException in project graphdb by neo4j-attic.
the class ZooClient method getJmxConnectionData.
public void getJmxConnectionData(ConnectionInformation connection) {
String path = rootPath + "/" + HA_SERVERS_CHILD + "/" + machineId + "-jmx";
byte[] data;
try {
data = zooKeeper.getData(path, false, null);
} catch (KeeperException e) {
return;
} catch (InterruptedException e) {
Thread.interrupted();
return;
}
if (data == null || data.length == 0)
return;
ByteBuffer buffer = ByteBuffer.wrap(data);
char[] url, instanceId;
try {
// read URL
url = new char[buffer.getShort()];
buffer.asCharBuffer().get(url);
buffer.position(buffer.position() + url.length * 2);
// read instanceId
instanceId = new char[buffer.getShort()];
buffer.asCharBuffer().get(instanceId);
} catch (BufferUnderflowException e) {
return;
}
connection.setJMXConnectionData(new String(url), new String(instanceId));
}
use of java.nio.BufferUnderflowException in project robovm by robovm.
the class OldAndroidNIOTest method byteBufferTest.
private void byteBufferTest(ByteBuffer b) {
checkBuffer(b);
// Duplicate buffers revert to big-endian.
b.order(ByteOrder.LITTLE_ENDIAN);
ByteBuffer dupe = b.duplicate();
assertEquals(ByteOrder.BIG_ENDIAN, dupe.order());
b.order(ByteOrder.BIG_ENDIAN);
// Bounds checks
try {
b.put(-1, (byte) 0);
fail("expected exception not thrown");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
b.put(b.limit(), (byte) 0);
fail("expected exception not thrown");
} catch (IndexOutOfBoundsException e) {
// expected
}
// IndexOutOfBoundsException: offset < 0
try {
byte[] data = new byte[8];
b.position(0);
b.put(data, -1, 2);
fail("expected exception not thrown");
} catch (IndexOutOfBoundsException e) {
// expected
}
// IndexOutOfBoundsException: length > array.length - offset
try {
byte[] data = new byte[8];
b.position(0);
b.put(data, 1, 8);
fail("expected exception not thrown");
} catch (IndexOutOfBoundsException e) {
// expected
}
// BufferOverflowException: length > remaining()
try {
byte[] data = new byte[8];
b.position(b.limit() - 2);
b.put(data, 0, 3);
fail("expected exception not thrown");
} catch (BufferOverflowException e) {
// expected
}
// Fill buffer with bytes A0 A1 A2 A3 ...
b.position(0);
for (int i = 0; i < b.capacity(); i++) {
b.put((byte) (0xA0 + i));
}
try {
b.put((byte) 0xFF);
fail("expected exception not thrown");
} catch (BufferOverflowException e) {
// expected
}
b.position(0);
assertEquals((byte) 0xA7, b.get(7));
try {
b.get(12);
fail("expected exception not thrown");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
b.get(-10);
fail("expected exception not thrown");
} catch (IndexOutOfBoundsException e) {
// expected
}
b.position(0);
b.order(ByteOrder.LITTLE_ENDIAN);
assertEquals((byte) 0xA0, b.get());
assertEquals((byte) 0xA1, b.get());
assertEquals((byte) 0xA2, b.get());
assertEquals((byte) 0xA3, b.get());
assertEquals((byte) 0xA4, b.get());
assertEquals((byte) 0xA5, b.get());
assertEquals((byte) 0xA6, b.get());
assertEquals((byte) 0xA7, b.get());
assertEquals((byte) 0xA8, b.get());
assertEquals((byte) 0xA9, b.get());
assertEquals((byte) 0xAA, b.get());
assertEquals((byte) 0xAB, b.get());
try {
b.get();
fail("expected exception not thrown");
} catch (BufferUnderflowException e) {
// expected
}
b.position(0);
b.order(ByteOrder.BIG_ENDIAN);
assertEquals((byte) 0xA0, b.get());
assertEquals((byte) 0xA1, b.get());
assertEquals((byte) 0xA2, b.get());
assertEquals((byte) 0xA3, b.get());
assertEquals((byte) 0xA4, b.get());
assertEquals((byte) 0xA5, b.get());
assertEquals((byte) 0xA6, b.get());
assertEquals((byte) 0xA7, b.get());
assertEquals((byte) 0xA8, b.get());
assertEquals((byte) 0xA9, b.get());
assertEquals((byte) 0xAA, b.get());
assertEquals((byte) 0xAB, b.get());
try {
b.get();
fail("expected exception not thrown");
} catch (BufferUnderflowException e) {
// expected
}
b.position(6);
b.limit(10);
assertEquals((byte) 0xA6, b.get());
// Check sliced buffer
b.position(6);
ByteBuffer bb = b.slice();
checkBuffer(bb);
assertEquals(0, bb.position());
assertEquals(4, bb.limit());
assertEquals(4, bb.capacity());
assertEquals((byte) 0xA6, bb.get());
assertEquals((byte) 0xA7, bb.get());
assertEquals((byte) 0xA8, bb.get());
assertEquals((byte) 0xA9, bb.get());
try {
bb.get();
fail("expected exception not thrown");
} catch (BufferUnderflowException e) {
// expected
}
// Reset position and limit
b.position(0);
b.limit(b.capacity());
// Check 'getShort'
b.order(ByteOrder.LITTLE_ENDIAN);
b.position(0);
assertEquals((short) 0xA1A0, b.getShort());
assertEquals((short) 0xA3A2, b.getShort());
assertEquals((short) 0xA5A4, b.getShort());
assertEquals((short) 0xA7A6, b.getShort());
assertEquals((short) 0xA9A8, b.getShort());
assertEquals((short) 0xABAA, b.getShort());
try {
bb.getShort();
fail("expected exception not thrown");
} catch (BufferUnderflowException e) {
// expected
}
b.order(ByteOrder.BIG_ENDIAN);
b.position(0);
assertEquals((short) 0xA0A1, b.getShort());
assertEquals((short) 0xA2A3, b.getShort());
assertEquals((short) 0xA4A5, b.getShort());
assertEquals((short) 0xA6A7, b.getShort());
assertEquals((short) 0xA8A9, b.getShort());
assertEquals((short) 0xAAAB, b.getShort());
try {
bb.getShort();
fail("expected exception not thrown");
} catch (BufferUnderflowException e) {
// expected
}
// Check 'getInt'
b.order(ByteOrder.LITTLE_ENDIAN);
b.position(0);
assertEquals(0xA3A2A1A0, b.getInt());
assertEquals(0xA7A6A5A4, b.getInt());
assertEquals(0xABAAA9A8, b.getInt());
try {
bb.getInt();
fail("expected exception not thrown");
} catch (BufferUnderflowException e) {
// expected
}
b.order(ByteOrder.BIG_ENDIAN);
b.position(0);
assertEquals(0xA0A1A2A3, b.getInt());
assertEquals(0xA4A5A6A7, b.getInt());
assertEquals(0xA8A9AAAB, b.getInt());
try {
bb.getInt();
fail("expected exception not thrown");
} catch (BufferUnderflowException e) {
// expected
}
// Check 'getFloat'
b.order(ByteOrder.LITTLE_ENDIAN);
b.position(0);
assertEquals(0xA3A2A1A0, Float.floatToRawIntBits(b.getFloat()));
assertEquals(0xA7A6A5A4, Float.floatToRawIntBits(b.getFloat()));
assertEquals(0xABAAA9A8, Float.floatToRawIntBits(b.getFloat()));
try {
b.getFloat();
fail("expected exception not thrown");
} catch (BufferUnderflowException e) {
// expected
}
b.order(ByteOrder.BIG_ENDIAN);
b.position(0);
assertEquals(0xA0A1A2A3, Float.floatToRawIntBits(b.getFloat()));
assertEquals(0xA4A5A6A7, Float.floatToRawIntBits(b.getFloat()));
assertEquals(0xA8A9AAAB, Float.floatToRawIntBits(b.getFloat()));
try {
b.getFloat();
fail("expected exception not thrown");
} catch (BufferUnderflowException e) {
// expected
}
// Check 'getDouble(int position)'
b.order(ByteOrder.LITTLE_ENDIAN);
assertEquals(0xA7A6A5A4A3A2A1A0L, Double.doubleToRawLongBits(b.getDouble(0)));
assertEquals(0xA8A7A6A5A4A3A2A1L, Double.doubleToRawLongBits(b.getDouble(1)));
try {
b.getDouble(-1);
fail("expected exception not thrown");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
b.getDouble(5);
fail("expected exception not thrown");
} catch (IndexOutOfBoundsException e) {
// expected
}
b.order(ByteOrder.BIG_ENDIAN);
assertEquals(0xA0A1A2A3A4A5A6A7L, Double.doubleToRawLongBits(b.getDouble(0)));
assertEquals(0xA1A2A3A4A5A6A7A8L, Double.doubleToRawLongBits(b.getDouble(1)));
try {
b.getDouble(-1);
fail("expected exception not thrown");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
b.getDouble(5);
fail("expected exception not thrown");
} catch (IndexOutOfBoundsException e) {
// expected
}
// Slice and check 'getInt'
b.position(1);
b.limit(5);
b.order(ByteOrder.LITTLE_ENDIAN);
bb = b.slice();
assertEquals(4, bb.capacity());
assertEquals(ByteOrder.BIG_ENDIAN, bb.order());
assertEquals(0xA1A2A3A4, bb.getInt(0));
bb.order(ByteOrder.LITTLE_ENDIAN);
assertEquals(0xA4A3A2A1, bb.getInt(0));
bb.order(ByteOrder.LITTLE_ENDIAN);
ShortBuffer sb = bb.asShortBuffer();
checkBuffer(sb);
assertEquals(2, sb.capacity());
assertEquals((short) 0xA2A1, sb.get());
assertEquals((short) 0xA4A3, sb.get());
bb.order(ByteOrder.BIG_ENDIAN);
sb = bb.asShortBuffer();
checkBuffer(sb);
assertEquals(2, sb.capacity());
assertEquals((short) 0xA1A2, sb.get());
assertEquals((short) 0xA3A4, sb.get());
bb.order(ByteOrder.LITTLE_ENDIAN);
IntBuffer ib = bb.asIntBuffer();
checkBuffer(ib);
assertEquals(1, ib.capacity());
assertEquals(0xA4A3A2A1, ib.get());
bb.order(ByteOrder.BIG_ENDIAN);
ib = bb.asIntBuffer();
checkBuffer(ib);
assertEquals(1, ib.capacity());
assertEquals(0xA1A2A3A4, ib.get());
bb.order(ByteOrder.LITTLE_ENDIAN);
FloatBuffer fb = bb.asFloatBuffer();
checkBuffer(fb);
assertEquals(1, fb.capacity());
assertEquals(0xA4A3A2A1, Float.floatToRawIntBits(fb.get()));
bb.order(ByteOrder.BIG_ENDIAN);
fb = bb.asFloatBuffer();
checkBuffer(fb);
assertEquals(1, fb.capacity());
assertEquals(0xA1A2A3A4, Float.floatToRawIntBits(fb.get()));
}
use of java.nio.BufferUnderflowException in project robovm by robovm.
the class BufferUnderflowExceptionTest method test_Constructor.
/**
*@tests {@link java.nio.BufferUnderflowException#BufferUnderflowException()}
*/
public void test_Constructor() {
BufferUnderflowException exception = new BufferUnderflowException();
assertNull(exception.getMessage());
assertNull(exception.getLocalizedMessage());
assertNull(exception.getCause());
}
Aggregations