use of org.jgroups.nio.Buffers in project JGroups by belaban.
the class BuffersTest method testEof.
public void testEof() throws Exception {
// -1 == EOF
byte[] data = { 'B', 'e', 'l', 'a' };
MockSocketChannel ch = new MockSocketChannel().bytesToRead((ByteBuffer) ByteBuffer.allocate(Global.INT_SIZE + data.length).putInt(data.length).put(data).flip());
Buffers bufs = new Buffers(ByteBuffer.allocate(Global.INT_SIZE), null);
ByteBuffer buf = bufs.readLengthAndData(ch);
assert buf != null;
assert buf.limit() == data.length;
ch.doClose();
try {
buf = bufs.readLengthAndData(ch);
assert false : "read() should have thrown an EOFException";
} catch (EOFException eof) {
System.out.printf("received exception as expected: %s\n", eof);
}
}
use of org.jgroups.nio.Buffers in project JGroups by belaban.
the class BuffersTest method testIteration.
public void testIteration() {
Buffers buf = new Buffers(6).add(ByteBuffer.wrap("hello world".getBytes()), ByteBuffer.allocate(1024), ByteBuffer.allocate(500), ByteBuffer.allocate(1024));
int i = 1, count = 0;
for (ByteBuffer b : buf) {
System.out.printf("buffer #%d: %d bytes\n", i++, b.remaining());
count++;
}
assert count == 4;
System.out.println("");
buf.remove(0).remove(2);
i = 1;
count = 0;
for (ByteBuffer b : buf) {
System.out.printf("buffer #%d: %d bytes\n", i++, b.remaining());
count++;
}
assert count == 2;
buf = new Buffers(10);
for (i = 1; i <= 10; i++) buf.add(ByteBuffer.allocate(i));
buf.remove(3).remove(4).remove(5).remove(9);
System.out.println();
count = 0;
i = 1;
for (ByteBuffer b : buf) {
System.out.printf("buffer #%d: %d bytes\n", i++, b.remaining());
count++;
}
assert count == 6;
}
use of org.jgroups.nio.Buffers in project JGroups by belaban.
the class BuffersTest method copyHelper.
protected void copyHelper(int capacity, int num_buffers, int buffer_size, int bytes_to_write) throws Exception {
assert capacity > 0 && num_buffers <= capacity;
// the original data, will be used to compare after the copy()
byte[][] arrays = new byte[num_buffers][];
for (int i = 0; i < arrays.length; i++) arrays[i] = Util.generateArray(buffer_size);
ByteBuffer[] buffers = new ByteBuffer[num_buffers];
for (int i = 0; i < arrays.length; i++) {
// make a copy as we'll modify it later, so the original is not modified: we need it to compare later
byte[] tmp = Arrays.copyOf(arrays[i], arrays[i].length);
buffers[i] = ByteBuffer.wrap(tmp);
}
ByteBuffer recorder = ByteBuffer.allocate(Math.max(bytes_to_write, num_buffers * buffer_size));
MockSocketChannel ch = new MockSocketChannel().bytesToWrite(bytes_to_write).recorder(recorder);
Buffers bufs = new Buffers(capacity).add(buffers);
System.out.println("\nbufs = " + bufs);
assert bufs.size() == buffers.length;
boolean successful_write = bytes_to_write >= num_buffers * buffer_size;
boolean rc = bufs.write(ch);
assert rc == successful_write;
if (!successful_write) {
bufs.copy();
int num_bufs_not_written = (int) Math.ceil((num_buffers * buffer_size - bytes_to_write) / (double) buffer_size);
assertNotEqual(bufs, buffers, num_bufs_not_written);
// modify the buffers and compare the output to the original buffers (should match)
for (ByteBuffer buf : buffers) modifyBuffer(buf);
// the next write() will succeed
ch.bytesToWrite(num_buffers * buffer_size);
rc = bufs.write(ch);
assert rc;
// now compare the original buffers with the recorded bytes
recorder.flip();
for (byte[] original : arrays) {
byte[] actual = new byte[original.length];
recorder.get(actual);
assert Arrays.equals(original, actual);
}
}
}
use of org.jgroups.nio.Buffers in project JGroups by belaban.
the class BuffersTest method testCreation.
public void testCreation() {
ByteBuffer b = buffer();
Buffers bufs = new Buffers(ByteBuffer.allocate(Global.INT_SIZE), b);
System.out.println("bufs = " + bufs);
assert bufs.remaining() == Global.INT_SIZE + b.capacity();
Buffers buf = new Buffers(8);
check(buf, 0, 0, 0, 0);
buf = new Buffers(ByteBuffer.allocate(Global.INT_SIZE), buf1);
check(buf, 0, 2, 2, buf1.limit() + Global.INT_SIZE);
}
use of org.jgroups.nio.Buffers in project JGroups by belaban.
the class BuffersTest method testAdd.
public void testAdd() throws Exception {
Buffers buf = new Buffers(12);
buf.add(buf1);
check(buf, 0, 1, 1, remaining(buf1));
buf.add(buf2);
check(buf, 0, 2, 2, remaining(buf1, buf2));
buf.add(buf3);
check(buf, 0, 3, 3, remaining(buf1, buf2, buf3));
}
Aggregations