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








Syntax

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

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

Example

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

//from   w ww. jav  a 2s  .  c  o m
import java.io.ByteArrayInputStream;
import java.io.IOException;

public class Main {
  public static void main(String[] args) throws IOException {

    byte[] buf = { 65, 66, 67, 68, 69 };

    ByteArrayInputStream bais = new ByteArrayInputStream(buf);

    byte[] b = new byte[4];
    int num = bais.read(b, 2, 2);

    System.out.println("Bytes read: " + num);

    for (byte s : b) {
      char c = (char) s;
      System.out.println(s);
      if (s == 0){
        System.out.println(": Null");
      }else{
        System.out.println(": " + c);
      }
    }

  }
}

The code above generates the following result.