Java IO Tutorial - Java PipedOutputStream .write (byte[] b, int off, int len)








Syntax

PipedOutputStream.write(byte[] b, int off, int len) has the following syntax.

public void write(byte[] b,  int off,  int len)  throws IOException

Example

In the following code shows how to use PipedOutputStream.write(byte[] b, int off, int len) method.

//from  w  w w .  j a va  2  s .c o m

import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class Main extends PipedInputStream {

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

    byte[] b = { 'h', 'e', 'l', 'l', 'o' };

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

    out.connect(in);

    // write something
    out.write(b, 0, 3);

    // flush the stream
    out.flush();

    // print what we wrote
    for (int i = 0; i < 3; i++) {
      System.out.print((char) in.read());
    }
    out.close();
  }
}

The code above generates the following result.