Search in sources :

Example 11 with BlockingInputStream

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

the class BlockingInputStreamTest method testBlockingWriteAndClose.

public void testBlockingWriteAndClose() throws IOException {
    final BlockingInputStream in = new BlockingInputStream(3);
    final CountDownLatch latch = new CountDownLatch(1);
    byte[] buf = { 'B', 'e', 'l', 'a' };
    // closes input stream after 1 sec
    new Closer(latch, in, 1000L).start();
    latch.countDown();
    in.write(buf, 0, buf.length);
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) BlockingInputStream(org.jgroups.util.BlockingInputStream)

Example 12 with BlockingInputStream

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

the class BlockingInputStreamTest method testWriteCloseRead3.

public void testWriteCloseRead3() throws IOException {
    final BlockingInputStream in = new BlockingInputStream(300);
    StringBuilder sb = new StringBuilder();
    for (int i = 1; i <= 10; i++) sb.append("Hello world " + i);
    byte[] buffer = sb.toString().getBytes();
    // don't use a separate thread
    new Writer(in, buffer).execute();
    byte[] buf = new byte[200];
    int num = in.read(buf);
    assert num == buffer.length;
}
Also used : BlockingInputStream(org.jgroups.util.BlockingInputStream)

Example 13 with BlockingInputStream

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

the class STATE method createStreamToProvider.

protected Tuple<InputStream, Object> createStreamToProvider(final Address provider, final StateHeader hdr) {
    Util.close(input_stream);
    input_stream = new BlockingInputStream(buffer_size);
    return new Tuple<>(input_stream, null);
}
Also used : BlockingInputStream(org.jgroups.util.BlockingInputStream) Tuple(org.jgroups.util.Tuple)

Example 14 with BlockingInputStream

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

the class BlockingInputStreamTest method testSimpleTransfer.

public void testSimpleTransfer() throws IOException {
    final BlockingInputStream in = new BlockingInputStream(100);
    byte[] buffer = new byte[500];
    for (int i = 0; i < buffer.length; i++) buffer[i] = (byte) (i % 2 == 0 ? 0 : 1);
    new Writer(in, buffer).start();
    byte[] tmp = new byte[500];
    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 == 500 : "offset is " + offset + " but expected 500";
    for (int i = 0; i < tmp.length; i++) {
        if (i % 2 == 0)
            assert tmp[i] == 0;
        else
            assert tmp[i] == 1;
    }
}
Also used : BlockingInputStream(org.jgroups.util.BlockingInputStream)

Example 15 with BlockingInputStream

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

the class BlockingInputStreamTest method testObjectStreaming.

public void testObjectStreaming() throws Exception {
    final BlockingInputStream input = new BlockingInputStream(8192);
    Map<String, List<Long>> map = new HashMap<>(4);
    for (String key : Arrays.asList("A", "B", "C", "D")) {
        List<Long> list = new ArrayList<>(1000);
        map.put(key, list);
        for (int i = 1; i <= 1000; i++) list.add((long) i);
    }
    ByteArrayOutputStream output = new ByteArrayOutputStream(8192);
    OutputStream out = new BufferedOutputStream(output);
    Util.objectToStream(map, new DataOutputStream(out));
    out.flush();
    final byte[] buffer = output.toByteArray();
    Thread writer = new Thread(() -> {
        try {
            input.write(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
    writer.start();
    Map<String, List<Long>> tmp = Util.objectFromStream(new DataInputStream(input));
    assert tmp.size() == 4;
    for (String key : Arrays.asList("A", "B", "C", "D")) {
        List<Long> list = map.get(key);
        assert list.size() == 1000;
        assert list.iterator().next() == 1;
    }
}
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