Most visited

Recently visited

Added in API level 1

PipedInputStream

public class PipedInputStream
extends InputStream

java.lang.Object
   ↳ java.io.InputStream
     ↳ java.io.PipedInputStream


管道输入流应连接到管道输出流; 管道输入流然后提供写入管道输出流的任何数据字节。 通常,数据是通过一个线程从PipedInputStream对象中读取的,并且数据通过其他线程写入相应的PipedOutputStream 。 不建议尝试从单个线程使用这两个对象,因为它可能使线程死锁。 管道输入流包含一个缓冲区,在限制范围内将读取操作与写入操作解耦。 如果向连接的管道输出流提供数据字节的线程不再有效,则管道被称为broken 。

也可以看看:

Summary

Constants

int PIPE_SIZE

管道的循环输入缓冲区的缺省大小。

Fields

protected byte[] buffer

传入数据放入其中的循环缓冲区。

protected int in

当从连接的管道输出流接收到数据的下一个字节时,循环缓冲区中的位置索引。

protected int out

循环缓冲区中位置的索引,此管道输入流将读取下一个字节的数据。

Public constructors

PipedInputStream(PipedOutputStream src)

创建一个 PipedInputStream以便它连接到管道输出流 src 。

PipedInputStream(PipedOutputStream src, int pipeSize)

创建一个 PipedInputStream以便它连接到管道输出流 src并使用管道缓冲区的指定管道大小。

PipedInputStream()

创建一个 PipedInputStream以便它尚未 connected 。

PipedInputStream(int pipeSize)

创建一个 PipedInputStream以便它尚未 connected并使用管道缓冲区的指定管道大小。

Public methods

int available()

返回可以从该输入流中读取而不被阻塞的字节数。

void close()

关闭此管道输入流并释放与该流关联的所有系统资源。

void connect(PipedOutputStream src)

使此管道输入流连接到管道输出流 src 。

int read()

从此管道输入流中读取下一个字节的数据。

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

从这个管道输入流中最多读取 len个字节的数据到一个字节数组中。

Protected methods

void receive(int b)

接收一个字节的数据。

Inherited methods

From class java.io.InputStream
From class java.lang.Object
From interface java.io.Closeable
From interface java.lang.AutoCloseable

Constants

PIPE_SIZE

Added in API level 1
int PIPE_SIZE

管道的循环输入缓冲区的缺省大小。

常量值:1024(0x00000400)

Fields

buffer

Added in API level 1
byte[] buffer

传入数据放入其中的循环缓冲区。

in

Added in API level 1
int in

当从连接的管道输出流接收到数据的下一个字节时,循环缓冲区中的位置索引。 in<0意味着缓冲区为空, in==out意味着缓冲区已满

out

Added in API level 1
int out

循环缓冲区中位置的索引,此管道输入流将读取下一个字节的数据。

Public constructors

PipedInputStream

Added in API level 1
PipedInputStream (PipedOutputStream src)

创建一个PipedInputStream以便它连接到管道输出流src 。 写入src数据字节将作为该流的输入。

Parameters
src PipedOutputStream: the stream to connect to.
Throws
IOException if an I/O error occurs.

PipedInputStream

Added in API level 9
PipedInputStream (PipedOutputStream src, 
                int pipeSize)

创建一个PipedInputStream以便它连接到管道输出流src并使用管道缓冲区的指定管道大小。 写入src数据字节将作为来自此流的输入。

Parameters
src PipedOutputStream: the stream to connect to.
pipeSize int: the size of the pipe's buffer.
Throws
IOException if an I/O error occurs.
IllegalArgumentException if pipeSize <= 0.

PipedInputStream

Added in API level 1
PipedInputStream ()

创建一个PipedInputStream以便它尚未connected 。 在使用之前,它必须是connected到PipedOutputStream 。

PipedInputStream

Added in API level 9
PipedInputStream (int pipeSize)

创建一个PipedInputStream以便它尚未connected,并使用管道缓冲区的指定管道大小。 在使用之前,它必须是connected到PipedOutputStream 。

Parameters
pipeSize int: the size of the pipe's buffer.
Throws
IllegalArgumentException if pipeSize <= 0.

Public methods

available

Added in API level 1
int available ()

返回可以从该输入流中读取而不被阻塞的字节数。

Returns
int the number of bytes that can be read from this input stream without blocking, or 0 if this input stream has been closed by invoking its close() method, or if the pipe is unconnected, or broken.
Throws
IOException if an I/O error occurs.

close

Added in API level 1
void close ()

关闭此管道输入流并释放与该流关联的所有系统资源。

Throws
IOException if an I/O error occurs.

connect

Added in API level 1
void connect (PipedOutputStream src)

使此管道输入流连接到管道输出流src 。 如果此对象已连接到某个其他管道输出流, IOException引发IOException 。

如果 src是未连接的管道输出流,而 snk是未连接的管道输入流,则它们可以通过以下任一方式进行连接:

snk.connect(src) 

或电话:

src.connect(snk) 

这两个调用具有相同的效果。

Parameters
src PipedOutputStream: The piped output stream to connect to.
Throws
IOException if an I/O error occurs.

read

Added in API level 1
int read ()

从此管道输入流中读取下一个字节的数据。 值字节被返回作为int范围0到255 。 此方法阻塞直到输入数据可用,流的末尾被检测到,或抛出异常。

Returns
int the next byte of data, or -1 if the end of the stream is reached.
Throws
IOException if the pipe is unconnected, broken, closed, or if an I/O error occurs.

read

Added in API level 1
int read (byte[] b, 
                int off, 
                int len)

从该管道输入流中最多读取len个字节的数据到一个字节数组中。 如果数据流的末尾已达到或者len超过管道的缓冲区大小,将读取少于len个字节。 如果len为零,则不读取字节并返回0; 否则,该方法阻塞,直到至少有1个字节的输入可用,流的末尾已被检测到,或者引发异常。

Parameters
b byte: the buffer into which the data is read.
off int: the start offset in the destination array b
len int: the maximum number of bytes read.
Returns
int the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws
NullPointerException If b is null.
IndexOutOfBoundsException If off is negative, len is negative, or len is greater than b.length - off
IOException if the pipe is broken, unconnected, closed, or if an I/O error occurs.

Protected methods

receive

Added in API level 1
void receive (int b)

接收一个字节的数据。 如果没有可用输入,此方法将被阻止。

Parameters
b int: the byte being received
Throws
IOException If the pipe is broken, unconnected, closed, or if an I/O error occurs.

Hooray!