use of java.nio.BufferOverflowException in project ignite by apache.
the class AlignedBuffersDirectFileIO method readIntoAlignedBuffer.
/**
* Read bytes from file using Native IO and aligned buffers.
*
* @param destBuf Destination aligned byte buffer.
* @param filePos Starting position of file. Providing {@link #FILE_POS_USE_CURRENT} means it is required
* to read from current file position.
* @return number of bytes read from file, or <tt>-1</tt> if tried to read past EOF for file.
* @throws IOException if reading failed.
*/
private int readIntoAlignedBuffer(ByteBuffer destBuf, long filePos) throws IOException {
int pos = destBuf.position();
int limit = destBuf.limit();
int toRead = pos <= limit ? limit - pos : 0;
if (toRead == 0)
return 0;
if ((pos + toRead) > destBuf.capacity())
throw new BufferOverflowException();
int rd;
Pointer ptr = bufferPtrAtPosition(destBuf, pos);
if (filePos == FILE_POS_USE_CURRENT)
rd = IgniteNativeIoLib.read(fdCheckOpened(), ptr, nl(toRead)).intValue();
else
rd = IgniteNativeIoLib.pread(fdCheckOpened(), ptr, nl(toRead), nl(filePos)).intValue();
if (rd == 0)
// Tried to read past EOF for file
return -1;
if (rd < 0)
throw new IOException(String.format("Error during reading file [%s] from position [%s] : %s", file, filePos == FILE_POS_USE_CURRENT ? "current" : Long.toString(filePos), getLastError()));
destBuf.position(pos + rd);
return rd;
}
use of java.nio.BufferOverflowException in project jetty.project by eclipse.
the class BufferUtilTest method testAppend.
@Test
public void testAppend() throws Exception {
ByteBuffer to = BufferUtil.allocate(8);
ByteBuffer from = BufferUtil.toBuffer("12345");
BufferUtil.append(to, from.array(), 0, 3);
assertEquals("123", BufferUtil.toString(to));
BufferUtil.append(to, from.array(), 3, 2);
assertEquals("12345", BufferUtil.toString(to));
try {
BufferUtil.append(to, from.array(), 0, 5);
Assert.fail();
} catch (BufferOverflowException e) {
}
}
use of java.nio.BufferOverflowException in project hbase by apache.
the class TestMultiByteBuff method testWritesAndReads.
@Test
public void testWritesAndReads() {
// Absolute reads
ByteBuffer bb1 = ByteBuffer.allocate(15);
ByteBuffer bb2 = ByteBuffer.allocate(15);
int i1 = 4;
bb1.putInt(i1);
long l1 = 45L, l2 = 100L, l3 = 12345L;
bb1.putLong(l1);
short s1 = 2;
bb1.putShort(s1);
byte[] b = Bytes.toBytes(l2);
bb1.put(b, 0, 1);
bb2.put(b, 1, 7);
bb2.putLong(l3);
MultiByteBuff mbb = new MultiByteBuff(bb1, bb2);
assertEquals(l1, mbb.getLong(4));
assertEquals(l2, mbb.getLong(14));
assertEquals(l3, mbb.getLong(22));
assertEquals(i1, mbb.getInt(0));
assertEquals(s1, mbb.getShort(12));
// Relative reads
assertEquals(i1, mbb.getInt());
assertEquals(l1, mbb.getLong());
assertEquals(s1, mbb.getShort());
assertEquals(l2, mbb.getLong());
assertEquals(l3, mbb.getLong());
// Absolute writes
bb1 = ByteBuffer.allocate(15);
bb2 = ByteBuffer.allocate(15);
mbb = new MultiByteBuff(bb1, bb2);
byte b1 = 5, b2 = 31;
mbb.put(b1);
mbb.putLong(l1);
mbb.putInt(i1);
mbb.putLong(l2);
mbb.put(b2);
mbb.position(mbb.position() + 2);
try {
mbb.putLong(l3);
fail("'Should have thrown BufferOverflowException");
} catch (BufferOverflowException e) {
}
mbb.position(mbb.position() - 2);
mbb.putLong(l3);
mbb.rewind();
assertEquals(b1, mbb.get());
assertEquals(l1, mbb.getLong());
assertEquals(i1, mbb.getInt());
assertEquals(l2, mbb.getLong());
assertEquals(b2, mbb.get());
assertEquals(l3, mbb.getLong());
mbb.put(21, b1);
mbb.position(21);
assertEquals(b1, mbb.get());
mbb.put(b);
assertEquals(l2, mbb.getLong(22));
}
use of java.nio.BufferOverflowException 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.BufferOverflowException in project j2objc by google.
the class BufferTest method testPutByteBuffer.
// http://code.google.com/p/android/issues/detail?id=16184
public void testPutByteBuffer() throws Exception {
ByteBuffer dst = ByteBuffer.allocate(10).asReadOnlyBuffer();
// Can't put into a read-only buffer.
try {
dst.put(ByteBuffer.allocate(5));
fail();
} catch (ReadOnlyBufferException expected) {
}
// Can't put a buffer into itself.
dst = ByteBuffer.allocate(10);
try {
dst.put(dst);
fail();
} catch (IllegalArgumentException expected) {
}
// Can't put the null ByteBuffer.
try {
dst.put((ByteBuffer) null);
fail();
} catch (NullPointerException expected) {
}
// Can't put a larger source into a smaller destination.
try {
dst.put(ByteBuffer.allocate(dst.capacity() + 1));
fail();
} catch (BufferOverflowException expected) {
}
assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocate(8), false);
assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocateDirect(8), false);
assertPutByteBuffer(ByteBuffer.allocate(10), allocateMapped(8), false);
assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocate(8), true);
assertPutByteBuffer(ByteBuffer.allocate(10), ByteBuffer.allocateDirect(8), true);
assertPutByteBuffer(ByteBuffer.allocate(10), allocateMapped(8), true);
assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocate(8), false);
assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocateDirect(8), false);
assertPutByteBuffer(ByteBuffer.allocateDirect(10), allocateMapped(8), false);
assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocate(8), true);
assertPutByteBuffer(ByteBuffer.allocateDirect(10), ByteBuffer.allocateDirect(8), true);
assertPutByteBuffer(ByteBuffer.allocateDirect(10), allocateMapped(8), true);
}
Aggregations