Clase Java.io.PipedInputStream en Java

Clase Java.io.PipedInputStream en Java
clase io.PipedInputStream Tubería en IO proporciona un enlace entre dos subprocesos que se ejecutan en JVM al mismo tiempo. Entonces, las tuberías se utilizan como origen o destino.
  • PipedInputStream también se canaliza con PipedOutputStream. Por lo tanto, los datos se pueden escribir usando PipedOutputStream y se pueden escribir usando PipedInputStream. Pero el uso de ambos subprocesos al mismo tiempo creará un punto muerto para los subprocesos.
  • Se dice que una tubería está rota si un subproceso que proporcionaba bytes de datos al flujo de salida conectado ya no está activo.
Declaración:
public class PipedInputStream extends InputStream 
Constructor:
    PipedInputStream() : crea un PipedInputStream que no está conectado. PipedInputStream(int pTamaño): crea un PipedInputStream que no está conectado con el tamaño de tubería especificado. PipedInputStream(PipedOutputStream outStream): crea un PipedInputStream al que está conectado PipedOutputStream - 'outStream'. PipedInputStream(PipedOutputStream outStream int pSize): crea una corriente de entrada canalizada que está conectada a una corriente de salida canalizada con el tamaño de tubería especificado.
Métodos:
    int lectura(): 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  ();      }      }   }   
    Producción :
    using read() : G using read() : E using read() : K 
    leer(byte[] buffer int offset int maxlen): java.io.PipedInputStream.read(byte[] buffer int offset int maxlen) lee hasta un máximo de bytes de los datos del flujo de entrada canalizado a la matriz de búferes. El método se bloquea si se alcanza el final de Stream o se lanza una excepción. Sintaxis:
    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.  
    recibir (int byte): java.io.PipedInputStream.receive (int byte) recibe el byte de los datos. Si no hay ninguna entrada disponible, el método se bloquea. Sintaxis:
    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. 
    cerca() : java.io.PipedInputStream.close() cierra el flujo de entrada canalizado y libera los recursos asignados. Sintaxis:
    public void close()   Parameters :    --------------   Return :    void   Exception :   ->   IOException :   if in case IO error occurs. 
    conectar (fuente PipedOutputStream): java.io.PipedInputStream.connect (fuente de PipedOutputStream) conecta el flujo de entrada canalizado al flujo de salida canalizado 'fuente' y, en caso de que la 'fuente' sea canalización con algún otro flujo, se genera una excepción de E/S Sintaxis:
    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.disponible() devuelve no. de bytes que se pueden leer desde Input Stream sin ser realmente bloqueados. Sintaxis:
    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. 
    Programa Java que explica el funcionamiento de los métodos de la clase 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  ();      }      }   }   
    Producción:
    Use of available() : 5 Using read(buffer offset maxlen) : GEEKS Closing the stream 
    Next Article: Clase Java.io.PipedOutputStream en Java Crear cuestionario