Search in sources :

Example 6 with BlockingInputStream

use of org.jgroups.util.BlockingInputStream in project JGroups by belaban.

the class BlockingInputStreamTest method testWriteExceedingCapacity.

public void testWriteExceedingCapacity() throws IOException {
    final BlockingInputStream in = new BlockingInputStream(10);
    new Thread(() -> {
        byte[] tmp = new byte[20];
        int num = 0;
        try {
            while (true) {
                int read = in.read(tmp);
                if (read == -1)
                    break;
                num += read;
            }
            System.out.println("read " + num + " bytes");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }).start();
    byte[] buffer = new byte[15];
    try {
        in.write(buffer);
    } finally {
        Util.close(in);
    }
}
Also used : BlockingInputStream(org.jgroups.util.BlockingInputStream)

Example 7 with BlockingInputStream

use of org.jgroups.util.BlockingInputStream in project JGroups by belaban.

the class BlockingInputStreamTest method testWriteCloseRead.

public void testWriteCloseRead() throws IOException {
    final BlockingInputStream in = new BlockingInputStream(100);
    for (int i = 1; i <= 5; i++) {
        byte[] buf = ("Hello world " + i).getBytes();
        in.write(buf);
    }
    in.close();
    int size = in.available();
    byte[] buf = new byte[100];
    int num = in.read(buf);
    assert num == size;
}
Also used : BlockingInputStream(org.jgroups.util.BlockingInputStream)

Example 8 with BlockingInputStream

use of org.jgroups.util.BlockingInputStream in project JGroups by belaban.

the class BlockingInputStreamTest method testLargeTransfer.

public void testLargeTransfer() throws IOException {
    final BlockingInputStream in = new BlockingInputStream(8192);
    final byte[] buffer = generateBuffer(1000000);
    new Writer(in, buffer).start();
    byte[] tmp = new byte[buffer.length];
    int offset = 0;
    while (true) {
        int bytes = in.read(tmp, offset, tmp.length - offset);
        if (bytes == -1)
            break;
        offset += bytes;
    }
    System.out.println("read " + offset + " bytes");
    assert offset == buffer.length : "offset is " + offset + " but expected " + buffer.length;
    System.out.print("Verifying that the buffers are the same: ");
    for (int i = 0; i < tmp.length; i++) assert buffer[i] == tmp[i];
    System.out.println("OK");
}
Also used : BlockingInputStream(org.jgroups.util.BlockingInputStream)

Example 9 with BlockingInputStream

use of org.jgroups.util.BlockingInputStream in project JGroups by belaban.

the class BlockingInputStreamTest method testWriteCloseRead2.

public void testWriteCloseRead2() throws IOException {
    final BlockingInputStream in = new BlockingInputStream(100);
    StringBuilder sb = new StringBuilder();
    for (int i = 1; i <= 10; i++) sb.append("Hello world " + i);
    byte[] buffer = sb.toString().getBytes();
    new Writer(in, buffer).start();
    Util.sleep(500);
    byte[] buf = new byte[200];
    int num = in.read(buf);
    assert num == buffer.length;
}
Also used : BlockingInputStream(org.jgroups.util.BlockingInputStream)

Example 10 with BlockingInputStream

use of org.jgroups.util.BlockingInputStream in project JGroups by belaban.

the class BlockingInputStreamTest method testReadOnClosedInputStream.

public void testReadOnClosedInputStream() throws IOException {
    final BlockingInputStream in = new BlockingInputStream(100);
    in.close();
    byte[] buf = new byte[100];
    int num = in.read(buf, 0, buf.length);
    assert num == -1 : " expected -1 (EOF) but got " + num;
}
Also used : BlockingInputStream(org.jgroups.util.BlockingInputStream)

Aggregations

BlockingInputStream (org.jgroups.util.BlockingInputStream)18 CountDownLatch (java.util.concurrent.CountDownLatch)2 Tuple (org.jgroups.util.Tuple)1