Klasa Java.io.PipedOutputStream w Javie

Klasa Java.io.PipedOutputStream w Javie

Klasa Java.io.PipedInputStream w Javie

Klasa io.PipedOutputStream w Javie


Kobza w IO zapewnia łącze między dwoma wątkami działającymi jednocześnie w JVM. Zatem rury są używane zarówno jako źródło, jak i miejsce docelowe.  

  • PipedInputStream jest również przesyłany za pomocą PipedOutputStream. Zatem dane można zapisywać przy użyciu PipedOutputStream i można je zapisywać przy użyciu PipedInputStream. Jednak jednoczesne użycie obu wątków spowoduje zakleszczenie wątków.
  • PipedOutputStream wysyła koniec potoku. Dane są zapisywane w PipedOutputStream. Mówi się, że potok jest uszkodzony, jeśli nie ma już strumienia PipedInputStream, który odczytywał dane.

Deklaracja:   

 public class PipedOutputStream   
extends OutputStream

Konstruktor:   

  • PipedOutputStream() : tworzy PipedOutputStream, który nie jest podłączony.
  • PipedOutputStream(PipedOutputStream inStream): tworzy PipedOutputStream, który to 
    jest podłączony do PipedInputStream - „inStream”.

Metody: 

write(): java.io.PipedOutputStream.write(int bajt) zapisuje określony bajt do potokowego strumienia wyjściowego. 

Składnia: 

    public void write(int byte)     

Parameters :
byte : byte to be written

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

write(bajt[] bufor int przesunięcie int maxlen): java.io.PipedOutputStream.write(bajt[] bufor int przesunięcie int maxlen) zapisuje maxlen bajtów danych z bufora do potokowego strumienia wyjściowego. Metoda blokuje się, jeśli do Stream nie są zapisywane żadne bajty. 

Składnia: 

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

Wyjście: 

 Use of write(buffer offset maxlen) : J A V A   
  • zamknij(): java.io.PipedOutputStream.close() zamyka potokowy strumień wyjściowy i zwalnia przydzielone zasoby. 
    Składnia: 
 public void close()   
Parameters :
--------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.
  • connect(miejsce docelowe PipedInputStream): java.io.PipedOutputStream.connect(miejsce docelowe PipedInputStream łączy potokowy strumień wyjściowy z „docelowym” potokowym strumieniem wejściowym, a w przypadku, gdy „docelowym” są potoki, zgłaszany jest wyjątek IO innego strumienia 
    Składnia: 
 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() opróżnia strumień wyjściowy. 
    Składnia: 
 public void flush()   
Parameters :
------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.

Kod Java ilustrujący działanie metod klasy 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  ();      }      }   }   

Wyjście: 

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


 

Utwórz quiz