Example usage for java.io PipedInputStream read

List of usage examples for java.io PipedInputStream read

Introduction

In this page you can find the example usage for java.io PipedInputStream read.

Prototype

public synchronized int read(byte b[], int off, int len) throws IOException 

Source Link

Document

Reads up to len bytes of data from this piped input stream into an array of bytes.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    PipedOutputStream out = new PipedOutputStream();
    PipedInputStream in = new PipedInputStream();

    // connect input and output
    in.connect(out);//  w  ww  .  j av a  2s . com

    // write something
    out.write(70);
    out.write(71);

    // read what we wrote into an array of bytes
    byte[] b = new byte[2];
    in.read(b, 0, 2);

    System.out.println(new String(b));
    in.close();
}

From source file:com.emc.ecs.sync.CasMigrationTest.java

private String pipeAndGetMd5(byte[] source) throws Exception {
    PipedInputStream pin = new PipedInputStream(BUFFER_SIZE);
    PipedOutputStream pout = new PipedOutputStream(pin);

    Producer producer = new Producer(source, pout);

    // produce in parallel
    Thread producerThread = new Thread(producer);
    producerThread.start();//from   w  w  w.j av  a 2 s  .c  o  m

    // consume inside this thread
    byte[] dest = new byte[source.length];
    try {
        int read = 0;
        while (read < dest.length && read != -1) {
            read += pin.read(dest, read, dest.length - read);
        }
    } finally {
        try {
            pin.close();
        } catch (Throwable t) {
            // ignore
        }
    }

    // synchronize
    producerThread.join();

    return Hex.encodeHexString(MessageDigest.getInstance("MD5").digest(dest));
}