当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Java.io.PipedInputStream用法及代码示例


io.PipedInputStream class

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

  • PipedInputStream 也通过 PipedOutputStream 进行管道传输。因此,可以使用 PipedOutputStream 写入数据,也可以使用 PipedInputStream 写入数据。但是,同时使用两个线程将会导致线程死锁。
  • 如果向连接的管道输出流提供数据字节的线程不再活动,则称管道已损坏。

声明:

public class PipedInputStream
  extends InputStream

构造函数:

  • PipedInputStream():创建一个 PipedInputStream,它未连接。
  • 管道输入流(int pSize):创建一个 PipedInputStream,它不与指定的管道大小连接。
  • 管道输入流(管道输出流outStream):创建一个 PipedInputStream,它连接到 PipedOutputStream - ‘outStream’。
  • PipedInputStream(PipedOutputStream outStream, int pSize):创建一个连接到具有指定管道大小的管道输出流的管道输入流。

方法:

  • int read():从此管道输入流中读取数据的下一个字节。值字节以 0 到 255 范围内的 int 形式返回。此方法将阻塞,直到输入数据可用、检测到流的末尾或出现异常。抛出。
    
    // 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
  • 读取(字节[]缓冲区,int偏移量,int maxlen):java.io.PipedInputStream.read(byte[] 缓冲区, int 偏移量, int maxlen)将最多 maxlen 个字节的数据从管道输入流读取到缓冲区数组。如果到达 Stream 末尾或引发异常,该方法将阻塞。
    Syntax :
    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.io.PipedInputStream.receive(int byte)接收数据字节。如果没有可用的输入,则该方法会阻塞。
    Syntax :
    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.
  • close():java.io.PipedInputStream.close()关闭管道输入流并释放分配的资源。
    Syntax :
    public void close()
    Parameters : 
    --------------
    Return :                                               
    void
    Exception :
    -> IOException : if in case IO error occurs.
  • 连接(PipedOutputStream 源):java.io.PipedInputStream.connect(PipedOutputStream源)将管道输入流连接到 ‘source’ 管道输出流,如果 ‘source’ 是带有其他流的管道,则会引发 IO 异常
    Syntax :
    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.
  • available():java.io.PipedInputStream.available()返回编号可以从输入流读取而不会实际被阻塞的字节数。
    Syntax :
    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.
  • Java 程序解释PipedInputStream 类方法的用法原理:

    
    // 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.io.PipedOutputStream class in Java



相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 Java.io.PipedInputStream class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。