logo

Clase Java.io.PipedOutputStream en Java

Clase Java.io.PipedInputStream en Java

Clase io.PipedOutputStream en Java' src='//techcodeview.com/img/misc/33/java-io-pipedoutputstream-class-in-java.webp' title=


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.
  • PipedOutputStream está enviando el final de la tubería. Los datos se escriben en PipedOutputStream. Se dice que la tubería está rota si el PipedInputStream que estaba leyendo los datos ya no existe.

Declaración:   



public class PipedOutputStream  
extends OutputStream

Constructor:   

  • Flujo de salida canalizada() : crea un PipedOutputStream que no está conectado.
  • Flujo de salida canalizado (flujo de salida canalizado en flujo): crea un PipedOutputStream que 
    está conectado a PipedInputStream - 'inStream'.

Métodos: 

escribir(): java.io.PipedOutputStream.write(int byte) escribe un byte específico en el flujo de salida canalizado. 

Sintaxis: 

    public void write(int byte)     

Parameters :
byte : byte to be written

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

escribir (byte [] buffer int offset int maxlen): java.io.PipedOutputStream.write (byte [] buffer int offset int maxlen) escribe maxlen bytes de los datos del búfer en el flujo de salida canalizado. El método se bloquea si no se escriben bytes en Stream. 

Sintaxis: 

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

Producción: 

Use of write(buffer offset maxlen) : J A V A  
  • cerrar(): java.io.PipedOutputStream.cerrar() cierra el flujo de salida canalizado y libera los recursos asignados. 
    Sintaxis: 
public void close()  
Parameters :
--------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.
  • conectar (destino de PipedInputStream): java.io.PipedOutputStream.connect (destino de PipedInputStream) conecta el flujo de salida canalizado al flujo de entrada canalizado 'destino' y, en caso de que el 'destino' sea canalizado con algún otro flujo, se genera una excepción de E/S 
    Sintaxis: 
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.
  • descarga(): java.io.PipedOutputStream.flush() vacía el flujo de salida. 
    Sintaxis: 
public void flush()  
Parameters :
------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.

Código Java que ilustra el funcionamiento de los métodos de clase 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();  }  } } 

Producción: 

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


 

Crear cuestionario