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

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

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

třída io.PipedOutputStream v Javě


Trubky v IO poskytují 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í.
  • PipedOutputStream odesílá konec kanálu. Data se zapisují do PipedOutputStream. Říká se, že roura je přerušená, pokud PipedInputStream, který četl data, již není.

Prohlášení:   

 public class PipedOutputStream   
extends OutputStream

Konstruktor:   

  • PipedOutputStream() : vytvoří PipedOutputStream, který není připojen.
  • PipedOutputStream(PipedOutputStream inStream) : vytvoří PipedOutputStream, že to 
    je připojen k PipedInputStream - 'inStream'.

Metody: 

write() : java.io.PipedOutputStream.write(int byte) zapíše určený bajt do Piped Output Stream. 

Syntaxe: 

    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) zapíše maxlen bajtů dat z vyrovnávací paměti do Piped Output Stream. Metoda se zablokuje, pokud do proudu nejsou zapsány žádné bajty. 

Syntaxe: 

    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  --  ;      }      }   }   

výstup: 

 Use of write(buffer offset maxlen) : J A V A   
  • close() : java.io.PipedOutputStream.close() zavře Piped Output Stream a uvolní přidělené zdroje. 
    Syntaxe: 
 public void close()   
Parameters :
--------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.
  • connect(cíl PipedInputStream) : java.io.PipedOutputStream.connect(cíl PipedInputStream připojuje zřetězený výstupní tok k 'cílovému' zřetězenému vstupnímu toku a v případě, že 'cíl' jsou roury s nějakým jiným tokem, je vyvolána IO výjimka 
    Syntaxe: 
 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() vyprázdní výstupní proud. 
    Syntaxe: 
 public void flush()   
Parameters :
------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.

Java kód ilustrující fungování metod třídy PipedOutputStream: 

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  ();      }      }   }   

výstup: 

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


 

Vytvořit kvíz