當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Java.io.PipedOutputStream用法及代碼示例


Java.io.PipedInputStream

io.PipedOutputStream class in Java

管道IO 提供了 JVM 中同時運行的兩個線程之間的鏈接。因此,管道既可以用作源也可以用作目標。

  • PipedInputStream 也通過 PipedOutputStream 進行管道傳輸。因此,可以使用 PipedOutputStream 寫入數據,也可以使用 PipedInputStream 寫入數據。但是,同時使用兩個線程將會導致線程死鎖。
  • PipedOutputStream 是管道的發送端。數據寫入 PipedOutputStream。如果讀取數據的 PipedInputStream 不再存在,則稱管道已損壞。

聲明:

public class PipedOutputStream
extends OutputStream

構造函數:

  • PipedOutputStream():創建一個 PipedOutputStream,它未連接。
  • PipedOutputStream(PipedOutputStream inStream):創建一個 PipedOutputStream,它
    連接到PipedInputStream - ‘inStream’。

方法:

write(): java.io.PipedOutputStream.write(int byte)將指定字節寫入管道輸出流。

用法:

public void write(int byte)

Parameters :
byte : byte to be written

Return : void
Exception :
-> IOException : if in case IO error occurs.

write(byte[] buffer, int offset, int maxlen): java.io.PipedOutputStream.write(byte[] buffer, int offset, int maxlen)將 maxlen 個字節的數據從緩衝區寫入管道輸出流。如果沒有字節寫入 Stream,則該方法將阻塞。

用法:

public void write(byte[] buffer, int offset, int maxlen)

Parameters :
buffer : data of the buffer
offset : starting in the destination array - 'buffer'.
maxlen : maximum length of array to be read

Return : void
Exception :
-> IOException : if in case IO error occurs.

Java


// Java program illustrating the working of PipedInputStream
// write(byte[] buffer, int offset, int maxlen)
import java.io.*;
public class NewClass
{
    public static void main(String[] args) throws IOException
    {
        PipedInputStream geek_input = new PipedInputStream();
        PipedOutputStream geek_output = new PipedOutputStream();
        // Use of connect() : connecting geek_input with geek_output
        geek_input.connect(geek_output);
        byte[] buffer = {'J', 'A', 'V', 'A'};
        // Use of write(byte[] buffer, int offset, int maxlen)
        geek_output.write(buffer, 0, 4);
        int a = 5;
        System.out.print("Use of write(buffer, offset, maxlen) : ");
        while(a>0)
        {
            System.out.print(" " + (char) geek_input.read());
              a--;
        }
    }
}

輸出:

Use of write(buffer, offset, maxlen) :  J A V A
  • close(): java.io.PipedOutputStream.close()關閉管道輸出流並釋放分配的資源。
    用法:
public void close()
Parameters :
--------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.
  • 連接(PipedInputStream 目的地): java.io.PipedOutputStream.connect(PipedInputStream 目的地將管道輸出流連接到 ‘destination’ 管道輸入流,如果 ‘destination’ 是帶有其他流的管道,則會引發 IO 異常
    用法:
public void connect(PipedInputStream destination)
Parameters :
destination : the Piped Input Stream to be connected to
Return :
void
Exception :
-> IOException : if in case IO error occurs.
  • flush(): java.io.PipedOutputStream.flush()刷新輸出流。
    用法:
public void flush()
Parameters :
------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.

說明 PipedOutputStream 類方法工作的 Java 代碼:

Java


// Java program illustrating the working of PipedInputStream
// write(), write(byte[] buffer, int offset, int maxlen),
// close(), flush(), connect()
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 write(int byte) :
            geek_output.write(71);
            geek_output.write(69);
            geek_output.write(69);
            geek_output.write(75);
            geek_output.write(83);
            // Use of flush() method :
            geek_output.flush();
            System.out.println("Use of flush() method : ");
            int i = 5;
            while(i > 0)
            {
                System.out.print(" " + (char) geek_input.read());
                i--;
            }
            // USe of close() method :
            System.out.println("\nClosing the Output stream");
            geek_output.close();
        }
        catch (IOException except)
        {
            except.printStackTrace();
        }
    }
}

輸出:

Use of flush() method : 
G E E K S
Closing the Output stream




相關用法


注:本文由純淨天空篩選整理自佚名大神的英文原創作品 Java.io.PipedOutputStream Class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。