JAVA伊奥。Java中的PipedInputStream类

io.PipedInputStream class

null

in-IO提供了同时在JVM中运行的两个线程之间的链接。因此,管道既可以用作源,也可以用作目标。

  • PipedInputStream也与PipedOutStream连接。因此,数据可以使用PipedOutStream写入,也可以使用PipedInputStream写入。但是,同时使用两个线程将为线程创建死锁。
  • 如果向连接的管道输出流提供数据字节的线程不再活动,则称管道已断开。

宣言:

public class PipedInputStream
  extends InputStream

建造师:

  • PipedInputStream(): 创建未连接的PipedInputStream。
  • PipedInputStream(int pSize): 创建未连接到指定管道大小的PipedInputStream。
  • PipedInputStream(PipedOutStream外流): 创建一个PipedInputStream,并将其连接到PipedOutStream—“outStream”。
  • PipedInputStream(PipedOutStream外流,int-pSize): 创建连接到具有指定管道大小的管道输出流的管道输入流。

方法:

  • int read(): 从这个管道输入流中读取下一个字节的数据。字节值以0到255范围内的整数形式返回。此方法会一直阻塞,直到输入数据可用、检测到流结束或引发异常为止。

    // Java program illustrating the working of read() method
    import java.io.*;
    public class NewClass
    {
    public static void main(String[] args) throws IOException
    {
    PipedInputStream geek_input = new PipedInputStream();
    PipedOutputStream geek_output = new PipedOutputStream();
    try
    {
    // Use of connect() : connecting geek_input with geek_output
    geek_input.connect(geek_output);
    // Use of read() method :
    geek_output.write( 71 );
    System.out.println( "using read() : " + ( char )geek_input.read());
    geek_output.write( 69 );
    System.out.println( "using read() : " + ( char )geek_input.read());
    geek_output.write( 75 );
    System.out.println( "using read() : " + ( char )geek_input.read());
    }
    catch (IOException except)
    {
    except.printStackTrace();
    }
    }
    }

    
    

    输出:

    using read() : G
    using read() : E
    using read() : K
  • 读取(字节[]缓冲区,整数偏移量,整数最大值): JAVA伊奥。PipedInputStream。读取(字节[]缓冲区,整数偏移量,整数最大值) 从管道输入流向缓冲区阵列读取最多maxlen字节的数据。如果到达流的结尾或引发异常,则该方法将阻塞。 语法:
    public int read(byte[] buffer, int offset, int maxlen)
    Parameters : 
    buffer : the destination buffer into which the data is to be read
    offset : starting in the destination array - 'buffer'.
    maxlen : maximum length of array to be read
    Return :                                               
    next 'maxlen' bytes of the data as an integer value 
    return -1 is end of stream is reached
    Exception :
    -> IOException : if in case IO error occurs.
    -> NullPointerException : if buffer is null.
    -> IndexOutOfBoundsException : if offset is -ve or 
                                    maxlen is -ve or maxlen > buffer.length - offset.
    
  • 接收(整数字节): JAVA伊奥。PipedInputStream。接收(整数字节) 接收字节的数据。如果没有可用的输入,则该方法会阻塞。 语法:
    protected void receive(int byte)
    Parameters : 
    byte : the bytes of the data received
    Return :                                               
    void
    Exception :
    -> IOException : if in case IO error occurs or pipe is broken.
  • 关闭(): JAVA伊奥。PipedInputStream。关闭() 关闭管道输入流并释放分配的资源。 语法:
    public void close()
    Parameters : 
    --------------
    Return :                                               
    void
    Exception :
    -> IOException : if in case IO error occurs.
  • 连接(管道输出流源): JAVA伊奥。PipedInputStream。连接(管道输出流源) 将管道输入流连接到“源”管道输出流,如果“源”与其他流是管道,则会引发IO异常 语法:
    public void connect(PipedOutputStream source)
    Parameters : 
    source : the Piped Output Stream to be connected to
    Return :                                               
    void
    Exception :
    -> IOException : if in case IO error occurs.
  • 可用(): JAVA伊奥。PipedInputStream。可用() 返回可以从输入流中读取而不被实际阻止的字节数。 语法:
    public int available()
    Parameters : 
    -------------
    Return :                                               
    no. of bytes that can be read from Input Stream without actually being blocked.
    0, if the stream is already closed but by invoking close() method
    Exception :
    -> IOException : if in case IO error occurs.

    解释PipedInputStream类方法工作原理的Java程序:

    // Java program illustrating the working of PipedInputStream
    // connect(), read(byte[] buffer, int offset, int maxlen),
    // close(), available()
    import java.io.*;
    public class NewClass
    {
    public static void main(String[] args) throws IOException
    {
    PipedInputStream geek_input = new PipedInputStream();
    PipedOutputStream geek_output = new PipedOutputStream();
    try
    {
    // Use of connect() : connecting geek_input with geek_output
    geek_input.connect(geek_output);
    geek_output.write( 71 );
    geek_output.write( 69 );
    geek_output.write( 69 );
    geek_output.write( 75 );
    geek_output.write( 83 );
    // Use of available() :
    System.out.println( "Use of available() : " + geek_input.available());
    // Use of read(byte[] buffer, int offset, int maxlen) :
    byte [] buffer = new byte [ 5 ];
    // destination 'buffer'
    geek_input.read(buffer, 0 , 5 );
    String str = new String(buffer);
    System.out.println( "Using read(buffer, offset, maxlen) : " + str);
    // USe of close() method :
    System.out.println( "Closing the stream" );
    geek_input.close();
    }
    catch (IOException except)
    {
    except.printStackTrace();
    }
    }
    }

    
    

    输出:

    Use of available() : 5
    Using read(buffer, offset, maxlen) : GEEKS
    Closing the stream

    下一篇文章: JAVA伊奥。Java中的PipedOutputStream类

    本文由 莫希特·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

    如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享