Java.io.PipedOutputStream-klass i Java

Java.io.PipedOutputStream-klass i Java

Java.io.PipedInputStream-klass i Java

io.PipedOutputStream-klass i Java


Rör i IO ger en länk mellan två trådar som körs i JVM samtidigt. Så Pipes används både som källa eller destination.  

  • PipedInputStream är också kopplad till PipedOutputStream. Så data kan skrivas med PipedOutputStream och kan skrivas med PipedInputStream. Men att använda båda trådarna samtidigt kommer att skapa ett dödläge för trådarna.
  • PipedOutputStream skickar slutet av röret. Data skrivs till PipedOutputStream. Röret sägs vara trasigt om PipedInputStream som läste data inte finns längre.

Förklaring:   

 public class PipedOutputStream   
extends OutputStream

Konstruktör:   

  • PipedOutputStream() : skapar en PipedOutputStream som den inte är ansluten.
  • PipedOutputStream(PipedOutputStream inStream) : skapar en PipedOutputStream som den 
    är ansluten till PipedInputStream - 'inStream'.

Metoder: 

write() : java.io.PipedOutputStream.write(int byte) skriver en specificerad byte till Piped Output Stream. 

Syntax : 

    public void write(int byte)     

Parameters :
byte : byte to be written

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

write(byte[] buffert int offset int maxlen): java.io.PipedOutputStream.write(byte[] buffert int offset int maxlen) skriver maxlen-bytes av data från buffert till piped utgångsström. Metoden blockerar om inga byte skrivs till strömmen. 

Syntax : 

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

Produktion: 

 Use of write(buffer offset maxlen) : J A V A   
  • close() : java.io.PipedOutputStream.close() stänger Piped Output Stream och frigör de tilldelade resurserna. 
    Syntax : 
 public void close()   
Parameters :
--------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.
  • connect(PipedInputStream destination) : java.io.PipedOutputStream.connect(PipedInputStream destination) ansluter Piped Output Stream till "destination" Piped Input Stream och om "destination" är rör med något annat stream IO-undantag kastas 
    Syntax : 
 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() spolar utströmmen. 
    Syntax : 
 public void flush()   
Parameters :
------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.

Java-kod som illustrerar hur PipedOutputStream-klassmetoderna fungerar: 

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

Produktion: 

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


 

Skapa frågesport