Package org.jgroups.util

Examples of org.jgroups.util.BlockingInputStream


    }


   
    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");
View Full Code Here


        }
    }


    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");
View Full Code Here

            assert buffer[i] == tmp[i];
        System.out.println("OK");
    }

    public void testLargeTransfer2() 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 bytes=in.read(tmp); // reads 1 million bytes in one go

        System.out.println("read " + bytes + " bytes");
        assert bytes == buffer.length : "read " + bytes + " bytes but expected " + buffer.length;
        System.out.print("Verifying that the buffers are the same: ");
        for(int i=0; i < tmp.length; i++)
View Full Code Here

            assert buffer[i] == tmp[i];
        System.out.println("OK");
    }

    public void testWriterMultipleChunks() throws Exception {
        final BlockingInputStream in=new BlockingInputStream(100);
        final byte[] buffer=generateBuffer(500);
        Writer writer=new Writer(in, buffer, 5, true);
        writer.start();

        byte[] tmp=new byte[20];
        int num=0;
        while(true) {
            int read=in.read(tmp);
            if(read == -1)
                break;
            num+=read;
        }
        System.out.println("read " + num + " bytes");
View Full Code Here

        System.out.println("read " + num + " bytes");
        assert num == 5 * buffer.length;
    }

    public void testMultipleWriters() throws Exception {
        final BlockingInputStream in=new BlockingInputStream(100);
        final byte[] buffer=generateBuffer(500);

        final Writer[] writers=new Writer[5];
        for(int i=0; i < writers.length; i++) {
            writers[i]=new Writer(in, buffer, 1, false);
            writers[i].setName("writer-" + (i+1));
            writers[i].start();
        }

        new Thread() {
            public void run() {
                while(true) {
                    boolean all_done=true;
                    for(Writer writer: writers) {
                        if(writer.isAlive()) {
                            all_done=false;
                            break;
                        }
                    }
                    if(all_done) {
                        Util.close(in);
                        return;
                    }
                    else
                        Util.sleep(100);
                }
            }
        }.start();

        byte[] tmp=new byte[400];
        int num=0;
        while(true) {
            int read=in.read(tmp, 0, tmp.length);
            if(read == -1)
                break;
            num+=read;
        }
        System.out.println("read " + num + " bytes");
View Full Code Here

            assert writer.isAlive() == false;
    }


    public void testWriteExceedingCapacity() throws IOException {
        final BlockingInputStream in=new BlockingInputStream(10);
        new Thread() {
            public void run() {
                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);
        }
    }
View Full Code Here

        }
    }


    public void testWritingBeyondLength() throws IOException {
        final BlockingInputStream in=new BlockingInputStream(800);

        new Thread() {
            public void run() {
                byte[] buf=new byte[800+600];
                try {
                    in.write(buf);
                }
                catch(IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        byte[] buf=new byte[1000];
        int read=in.read(buf);
        assert read == buf.length;
    }
View Full Code Here

        assert read == buf.length;
    }


    public void testSimpleWrite() throws Exception {
        final BlockingInputStream input=new BlockingInputStream(8192);
        byte[] in={'B', 'e', 'l', 'a'};
        input.write(in);

        byte[] buf=new byte[5];
        for(int i=0; i < in.length; i++) {
            int read=input.read(buf, i, 1);
            assert read == 1;
        }
        for(int i=0; i < in.length; i++)
            assert in[i] == buf[i];
    }
View Full Code Here

            assert in[i] == buf[i];
    }


    public void testObjectStreaming() throws Exception {
        final BlockingInputStream input=new BlockingInputStream(8192);

        Map<String,List<Long>> map=new HashMap<String,List<Long>>(4);
        for(String key: Arrays.asList("A", "B", "C", "D")) {
            List<Long> list=new ArrayList<Long>(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() {
            public void run() {
                try {
                    input.write(buffer);
                }
                catch(IOException e) {
                    e.printStackTrace();
                }
            }
View Full Code Here

TOP

Related Classes of org.jgroups.util.BlockingInputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.