Search in sources :

Example 11 with Buffers

use of org.jgroups.nio.Buffers in project JGroups by belaban.

the class BuffersTest method testMakeSpaceSimpleMove.

public void testMakeSpaceSimpleMove() throws Exception {
    MockSocketChannel ch = new MockSocketChannel().bytesToWrite(1000);
    Buffers buf = new Buffers(6);
    buf.add(buf1);
    buf.write(ch);
    check(buf, 1, 1, 0, 0);
    buf.add(buf2);
    check(buf, 1, 2, 1, remaining(buf2));
    boolean rc = makeSpace(buf);
    assert rc;
    check(buf, 0, 1, 1, remaining(buf2));
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers)

Example 12 with Buffers

use of org.jgroups.nio.Buffers in project JGroups by belaban.

the class BuffersTest method testRead.

public void testRead() throws Exception {
    byte[] data = "hello world".getBytes();
    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 b = bufs.readLengthAndData(ch);
    System.out.println("b = " + b);
    assert b != null;
    assert Arrays.equals(data, b.array());
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers) ByteBuffer(java.nio.ByteBuffer)

Example 13 with Buffers

use of org.jgroups.nio.Buffers in project JGroups by belaban.

the class BuffersTest method testMakeSpaceOverlappingMove.

public void testMakeSpaceOverlappingMove() throws Exception {
    MockSocketChannel ch = new MockSocketChannel().bytesToWrite(1000);
    Buffers buf = new Buffers(6);
    buf.add(buf1);
    check(buf, 0, 1, 1, remaining(buf1));
    buf.write(ch);
    check(buf, 1, 1, 0, 0);
    buf.add(buf2, buf3);
    boolean rc = makeSpace(buf);
    assert rc;
    check(buf, 0, 2, 2, remaining(buf2, buf3));
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers)

Example 14 with Buffers

use of org.jgroups.nio.Buffers in project JGroups by belaban.

the class BuffersTest method testWriteWithBuffering.

public void testWriteWithBuffering() throws Exception {
    Buffers bufs = new Buffers(4);
    // all writes will fail
    MockSocketChannel ch = new MockSocketChannel();
    for (int i = 0; i < 4; i++) {
        ByteBuffer b = buf1.duplicate();
        boolean rc = bufs.write(ch, b);
        assert !rc;
    }
    check(bufs, 0, 4, 4, remaining(buf1) * 4);
    ch.bytesToWrite(20);
    boolean rc = bufs.write(ch);
    assert !rc;
    rc = bufs.write(ch, buf1.duplicate());
    assert !rc;
    check(bufs, 0, 4, 4, remaining(buf1) * 5 - 20);
    // write all buffers now
    ch.bytesToWrite(1000);
    rc = bufs.write(ch);
    assert rc;
    check(bufs, 0, 0, 0, 0);
    rc = bufs.write(ch, buf1.duplicate());
    assert rc;
    check(bufs, 1, 1, 0, 0);
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers) ByteBuffer(java.nio.ByteBuffer)

Example 15 with Buffers

use of org.jgroups.nio.Buffers in project JGroups by belaban.

the class BuffersTest method testReadLength.

public void testReadLength() throws Exception {
    byte[] tmp = "hello world".getBytes();
    ByteBuffer data = ByteBuffer.allocate(Global.INT_SIZE + tmp.length).putInt(tmp.length).put(tmp);
    // read the entire length
    data.flip().limit(4);
    MockSocketChannel ch = new MockSocketChannel().bytesToRead(data);
    Buffers bufs = new Buffers(ByteBuffer.allocate(Global.INT_SIZE), null);
    ByteBuffer buf = bufs.readLengthAndData(ch);
    assert buf == null;
    // allow for some more data to be read...
    data.limit(8);
    buf = bufs.readLengthAndData(ch);
    assert buf == null;
    // read all data
    data.limit(data.capacity());
    buf = bufs.readLengthAndData(ch);
    assert buf != null;
}
Also used : MockSocketChannel(org.jgroups.nio.MockSocketChannel) Buffers(org.jgroups.nio.Buffers) ByteBuffer(java.nio.ByteBuffer)

Aggregations

Buffers (org.jgroups.nio.Buffers)23 MockSocketChannel (org.jgroups.nio.MockSocketChannel)19 ByteBuffer (java.nio.ByteBuffer)14 IpAddress (org.jgroups.stack.IpAddress)2 ByteArrayDataOutputStream (org.jgroups.util.ByteArrayDataOutputStream)2 EOFException (java.io.EOFException)1