Java IO Tutorial - Java PipedInputStream.read(byte[] b, int off, int len)








Syntax

PipedInputStream.read(byte[] b, int off, int len) has the following syntax.

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

Example

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

//from w  w w. j  ava 2  s .  com

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

public class Main {

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

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

    // connect input and output
    in.connect(out);

    // 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();
  }
}

The code above generates the following result.