Java.io.PipedInputStream klasa u Javi
Cijevi u IO pružaju vezu između dvije niti koje se izvode u JVM-u u isto vrijeme. Dakle, cijevi se koriste i kao izvor i odredište.
- PipedInputStream je također povezan s PipedOutputStream. Dakle, podaci se mogu pisati pomoću PipedOutputStream i mogu se pisati pomoću PipedInputStream. Ali korištenje obje niti u isto vrijeme stvorit će zastoj za niti.
- PipedOutputStream šalje kraj cijevi. Podaci se zapisuju u PipedOutputStream. Kaže se da je cijev prekinuta ako PipedInputStream koji je čitao podatke više ne postoji.
deklaracija:
public class PipedOutputStream
extends OutputStream
Konstruktor:
- PipedOutputStream() : stvara PipedOutputStream da nije povezan.
- PipedOutputStream(PipedOutputStream inStream) : stvara PipedOutputStream koji ga
je povezan s PipedInputStream - 'inStream'.
Metode:
write() : java.io.PipedOutputStream.write(int byte) zapisuje određeni bajt u izlazni tok.
Sintaksa:
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) zapisuje maxlen bajtova podataka iz međuspremnika u izlazni tok. Metoda se blokira ako nijedan bajt nije upisan u Stream.
Sintaksa:
public void write(byte[] buffer int offset int maxlen)Java
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 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--; } } }
Izlaz:
Use of write(buffer offset maxlen) : J A V A
- close() : java.io.PipedOutputStream.close() zatvara Piped Output Stream i oslobađa dodijeljene resurse.
Sintaksa:
public void close()
Parameters :
--------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.
- povezivanje (PipedInputStream odredište) : java.io.PipedOutputStream.connect(PipedInputStream odredište povezuje Piped Output Stream s 'odredišnim' Piped Input Streamom i u slučaju da je 'destinacija' cijevi s nekim drugim tokom IO iznimka
Sintaksa:
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() ispire izlazni tok.
Sintaksa:
public void flush()
Parameters :
------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.
Java kod koji ilustrira rad metoda klase 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(); } } }
Izlaz:
Use of flush() method :
G E E K S
Closing the Output stream