Classe Java.io.PipedInputStream em Java

Classe Java.io.PipedInputStream em Java
Classe io.PipedInputStream Tubos in IO fornece um link entre dois threads em execução na JVM ao mesmo tempo. Portanto, Pipes são usados ​​​​como origem ou destino.
  • PipedInputStream também é canalizado com PipedOutputStream. Portanto, os dados podem ser gravados usando PipedOutputStream e podem ser gravados usando PipedInputStream. Mas usar os dois threads ao mesmo tempo criará um impasse para os threads.
  • Diz-se que um canal está quebrado se um encadeamento que fornecia bytes de dados para o fluxo de saída canalizado conectado não estiver mais ativo.
Declaração:
public class PipedInputStream extends InputStream 
Construtor:
    PipedInputStream(): cria um PipedInputStream que não está conectado. PipedInputStream(int pSize): cria um PipedInputStream que não está conectado ao tamanho de tubo especificado. PipedInputStream(PipedOutputStream outStream): cria um PipedInputStream que está conectado ao PipedOutputStream - 'outStream'. PipedInputStream(PipedOutputStream outStream int pSize): cria um fluxo de entrada canalizado que está conectado ao fluxo de saída canalizado com o tamanho de tubo especificado.
Métodos:
    leitura interna(): 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  ();      }      }   }   
    Saída :
    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) lê até maxlen bytes dos dados do Piped Input Stream para a matriz de buffers. O método é bloqueado se o fim do Stream for atingido ou uma exceção for lançada. Sintaxe:
    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.  
    receber (int byte): java.io.PipedInputStream.receive(int byte) recebe byte dos dados. Se nenhuma entrada estiver disponível, o método será bloqueado. Sintaxe:
    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. 
    fechar() : java.io.PipedInputStream.close() fecha o Piped Input Stream e libera os recursos alocados. Sintaxe:
    public void close()   Parameters :    --------------   Return :    void   Exception :   ->   IOException :   if in case IO error occurs. 
    conectar (fonte PipedOutputStream): java.io.PipedInputStream.connect (fonte PipedOutputStream) conecta o fluxo de entrada canalizado ao fluxo de saída canalizado 'fonte' e no caso de 'fonte' ser canalizado com algum outro fluxo, a exceção IO é lançada Sintaxe:
    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. 
    disponível() : java.io.PipedInputStream.available() retorna não. de bytes que podem ser lidos do Input Stream sem realmente serem bloqueados. Sintaxe:
    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 explicando o funcionamento dos métodos da 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  ();      }      }   }   
    Saída:
    Use of available() : 5 Using read(buffer offset maxlen) : GEEKS Closing the stream 
    Next Article: Classe Java.io.PipedOutputStream em Java Criar questionário