logo

Třída Java.io.PipedInputStream v Javě

třída io.PipedInputStream' title= Trubky in IO poskytuje spojení mezi dvěma vlákny běžícími v JVM současně. Potrubí se tedy používá jako zdroj nebo cíl.
  • PipedInputStream je také přenášen pomocí PipedOutputStream. Data tedy mohou být zapsána pomocí PipedOutputStream a mohou být zapsána pomocí PipedInputStream. Ale použití obou vláken současně vytvoří pro vlákna uváznutí.
  • O potrubí se říká, že je přerušeno, pokud vlákno, které poskytovalo datové bajty do připojeného výstupního proudu potrubím, již není aktivní.
Prohlášení:
public class PipedInputStream extends InputStream
Konstruktor:
    PipedInputStream() :vytvoří PipedInputStream, který není připojen. PipedInputStream(int pSize) :vytvoří PipedInputStream, který není spojen se zadanou velikostí potrubí. PipedInputStream(PipedOutputStream outStream) :vytvoří PipedInputStream, který je připojen k PipedOutputStream - 'outStream'. PipedInputStream(PipedOutputStream outStream int pSize) :vytvoří Piped Input Stream, který je připojen k Piped Output Stream se zadanou velikostí potrubí.
Metody:
    int read(): Reads the next byte of data from this piped input stream.The value byte is returned as an int in the range 0 to 255. This method blocks until input data is available the end of the stream is detected or an exception is thrown. Java
    // 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();  }  } } 
    výstup:
    using read() : G using read() : E using read() : K
    read(byte[] buffer int offset int maxlen) : java.io.PipedInputStream.read(byte[] buffer int offset int maxlen) čte až maxlen bajtů dat z Piped Input Stream do pole vyrovnávacích pamětí. Metoda se zablokuje, pokud je dosaženo konce streamu nebo je vyvolána výjimka. Syntaxe:
    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. 
    příjem (int byte): java.io.PipedInputStream.receive(int byte) přijímá bajt dat. Pokud není k dispozici žádný vstup, metoda se zablokuje. Syntaxe:
    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.
    zavřít(): java.io.PipedInputStream.close() zavře Piped Input Stream a uvolní přidělené zdroje. Syntaxe:
    public void close()   Parameters :    --------------   Return :    void   Exception :   ->   IOException :   if in case IO error occurs.
    připojit (zdroj PipedOutputStream) : java.io.PipedInputStream.connect (zdroj PipedOutputStream) připojí zřetězený vstupní tok ke „zdrojovému“ zřetězenému výstupnímu toku a v případě, že „zdroj“ jsou roury s nějakým jiným tokem, je vyvolána IO výjimka Syntaxe:
    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.
    k dispozici(): java.io.PipedInputStream.available() vrací ne. bajtů, které lze číst ze vstupního toku, aniž by byly ve skutečnosti blokovány. Syntaxe:
    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 program vysvětlující fungování metod třídy 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();  }  } } 
    výstup:
    Use of available() : 5 Using read(buffer offset maxlen) : GEEKS Closing the stream
    Next Article: Třída Java.io.PipedOutputStream v Javě Vytvořit kvíz