int read(byte[] b, int off, int len)
描述 (Description)
java.io.PipedInputStream.read(byte[] b int off, int len)方法从此管道输入流中读取最多len个字节的数据到一个字节数组。 如果到达数据流的末尾或者len超过管道的缓冲区大小,则将读取小于len个字节。 如果len为零,则不读取任何字节,返回0; 否则,该方法将阻塞,直到输入的至少1个字节可用,已检测到流的末尾,或者抛出异常。
声明 (Declaration)
以下是java.io.PipedInputStream.read()方法的声明。
public int read(byte[] b, int off, int len)
参数 (Parameters)
b - 读取数据的缓冲区。
off - 目标数组中的起始偏移量b。
len - 读取的最大字节数。
返回值 (Return Value)
此方法返回读入缓冲区的总字节数,如果没有更多数据,则返回-1,因为已到达流的末尾。
异常 (Exception)
NullPointerException - 如果b为null。
IndexOutOfBoundsException - 如果off为负数,则len为负数,或len为b.length-off。
IOException - 如果管道损坏,未连接,关闭或发生I/O错误。
例子 (Example)
以下示例显示了java.io.PipedInputStream.read()方法的用法。
package com.iowiki;
import java.io.*;
public class PipedInputStreamDemo {
public static void main(String[] args) {
// create a new Piped input and Output Stream
PipedOutputStream out = new PipedOutputStream();
PipedInputStream in = new PipedInputStream();
try {
// 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);
// print the array as a string
String s = new String(b);
System.out.println("" + s);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
让我们编译并运行上面的程序,这将产生以下结果 -
FG