Migrate from GitHub to SourceForge with this tool. Check out all of SourceForge's recent improvements.
Close

blocking Streams

BlockingInputStream and BlockingOutputStream allows to wait for closing of the Streams not only from while loop where your read or write bits, but also from anoter thread. For example you observe a Stream and want to wait in an extra thread for the finishing of this Stream, in htat case you can decorate your Stream with BlockingInputStream or BlockingOutputStream and waits until the Stream has finished.
Above is presented how a BlockingInputStream can be used:

Thread-1
InputStream is = 
//decorate inputStream with BlockingInputStream
final BlockingInputStream_1x2 bis = BlockingInputStream_1x2.of(is);

try {
    for(int i=0; i<arrIn.length; i++){
        arrOut[i] = (byte) bis.read();
        Thread.sleep(10);//slow down the read process
        System.out.println(i + " bytes read");
    }
    bis.close();
} catch (Exception e) {
    exceptionThrown.set(true);
}
Thread-2
//waits until BlockinginputStream has finished
bis.block();




In the same manner you can block on BlockingOutputStream, above you can see a code-snippet how the block-operation can be invoked.

Thread-1
OutputStream os = new ByteArrayOutputStream();
final BlockingOutputStream_1x2 bos = BlockingOutputStream_1x2.of(os);

Thread-2
//waits until BlockingOutputStream has finshed
bos.block();

See for more examples in Example Section