Classe Java.io.PipedOutputStream in Java
Classe Java.io.PipedInputStream in Java
Tubi in IO forniscono un collegamento tra due thread in esecuzione in JVM contemporaneamente. Quindi i tubi vengono utilizzati sia come origine che come destinazione.
- PipedInputStream viene inoltre reindirizzato a PipedOutputStream. Pertanto i dati possono essere scritti utilizzando PipedOutputStream e possono essere scritti utilizzando PipedInputStream. Ma l'utilizzo di entrambi i thread contemporaneamente creerà un deadlock per i thread.
- PipedOutputStream sta inviando l'estremità della pipe. I dati vengono scritti in PipedOutputStream. Si dice che la pipe sia rotta se il PipedInputStream che stava leggendo i dati non c'è più.
Dichiarazione:
public class PipedOutputStream
extends OutputStream
Costruttore:
- PipedOutputStream() : crea un PipedOutputStream a cui non è connesso.
- PipedOutputStream(PipedOutputStream inStream): crea un PipedOutputStream che esso
è connesso a PipedInputStream - 'inStream'.
Metodi:
write(): java.io.PipedOutputStream.write(int byte) scrive un byte specificato nel flusso di output convogliato.
Sintassi:
public void write(int byte)
Parameters :
byte : byte to be written
Return : void
Exception :
-> IOException : if in case IO error occurs.write(byte[] buffer int offset int maxlen): java.io.PipedOutputStream.write(byte[] buffer int offset int maxlen) scrive maxlen byte di dati dal buffer al flusso di output convogliato. Il metodo si blocca se non vengono scritti byte nello Stream.
Sintassi:
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 -- ; } } }Produzione:
Use of write(buffer offset maxlen) : J A V A
- close(): java.io.PipedOutputStream.close() chiude il flusso di output convogliato e rilascia le risorse allocate.
Sintassi:
public void close()
Parameters :
--------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.
- connect(destinazione PipedInputStream): java.io.PipedOutputStream.connect(destinazione PipedInputStream collega il flusso di output convogliato al flusso di input convogliato di "destinazione" e nel caso in cui la "destinazione" sia convogliata con qualche altro flusso viene generata un'eccezione IO
Sintassi:
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() scarica il flusso di output.
Sintassi:
public void flush()
Parameters :
------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.
Codice Java che illustra il funzionamento dei metodi della classe 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 (); } } }Produzione:
Use of flush() method :
G E E K S
Closing the Output stream
Crea quiz