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 Java.io.PipedInputStream.available()用法及代碼示例
- Java Java.io.PipedInputStream.close()用法及代碼示例
- Java Java.io.PipedInputStream.connect()用法及代碼示例
- Java Java.io.PipedInputStream.read()用法及代碼示例
- Java Java.io.PipedOutputStream.close()用法及代碼示例
- Java Java.io.PipedOutputStream.connect()用法及代碼示例
- Java Java.io.PipedOutputStream.flush()用法及代碼示例
- Java Java.io.PipedOutputStream.write()用法及代碼示例
- Java Java.io.PipedReader.close()用法及代碼示例
- Java Java.io.PipedReader.connect()用法及代碼示例
- Java Java.io.PipedReader.read()用法及代碼示例
- Java Java.io.PipedReader.ready()用法及代碼示例
- Java Java.io.PipedWriter.close()用法及代碼示例
- Java Java.io.PipedWriter.connect()用法及代碼示例
- Java Java.io.PipedWriter.flush()用法及代碼示例
- Java Java.io.PipedWriter.write()用法及代碼示例
- Java Java.io.PipedReader用法及代碼示例
- Java Java.io.PipedWriter用法及代碼示例
- Java Java.io.PipedOutputStream用法及代碼示例
- Java Java.io.PrintStream.append()用法及代碼示例
- Java Java.io.PrintStream.checkError()用法及代碼示例
- Java Java.io.PrintStream.clearError()用法及代碼示例
- Java Java.io.PrintStream.close()用法及代碼示例
- Java Java.io.PrintStream.flush()用法及代碼示例
- Java Java.io.PrintStream.format()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.io.PipedInputStream class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。