Classe Java.io.PipedInputStream en Java

Classe Java.io.PipedInputStream en Java
Classe io.PipedInputStream Tuyaux in IO fournit un lien entre deux threads exécutés dans JVM en même temps. Les tuyaux sont donc utilisés à la fois comme source ou comme destination.
  • PipedInputStream est également redirigé vers PipedOutputStream. Ainsi, les données peuvent être écrites à l'aide de PipedOutputStream et peuvent être écrites à l'aide de PipedInputStream. Mais l'utilisation des deux threads en même temps créera une impasse pour les threads.
  • Un canal est dit rompu si un thread qui fournissait des octets de données au flux de sortie canalisé connecté n'est plus actif.
Déclaration:
public class PipedInputStream extends InputStream 
Constructeur :
    PipedInputStream() : crée un PipedInputStream auquel il n'est pas connecté. PipedInputStream(int pSize) : crée un PipedInputStream qui n'est pas connecté à la taille de tuyau spécifiée. PipedInputStream(PipedOutputStream outStream) : crée un PipedInputStream auquel il est connecté à PipedOutputStream - 'outStream'. PipedInputStream(PipedOutputStream outStream int pSize) : crée un flux d'entrée redirigé connecté au flux de sortie redirigé avec la taille de canal spécifiée.
Méthodes :
    int lire() : 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  ();      }      }   }   
    Sortir :
    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) lit jusqu'à un maximum d'octets de données du flux d'entrée canalisé vers le tableau de tampons. La méthode se bloque si la fin du Stream est atteinte ou si une exception est levée. 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.  
    recevoir(int octet) : java.io.PipedInputStream.receive (int octet) reçoit l'octet des données. Si aucune entrée n’est disponible, la méthode se bloque. 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. 
    fermer() : java.io.PipedInputStream.close() ferme le flux d'entrée canalisé et libère les ressources allouées. Syntaxe :
    public void close()   Parameters :    --------------   Return :    void   Exception :   ->   IOException :   if in case IO error occurs. 
    connecter(source PipedOutputStream) : java.io.PipedInputStream.connect (source PipedOutputStream) connecte le flux d'entrée canalisé au flux de sortie canalisé « source » et dans le cas où « source » est un canal avec un autre flux, une exception d'E/S est levée 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. 
    disponible() : java.io.PipedInputStream.available() renvoie le numéro. d'octets qui peuvent être lus à partir du flux d'entrée sans être réellement bloqués. 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. 
    Programme Java expliquant le fonctionnement des méthodes de la classe 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  ();      }      }   }   
    Sortir:
    Use of available() : 5 Using read(buffer offset maxlen) : GEEKS Closing the stream 
    Next Article: Classe Java.io.PipedOutputStream en Java Créer un quiz