Conducte în IO oferă o legătură între două fire care rulează în JVM în același timp. Deci, țevile sunt folosite atât ca sursă, cât și ca destinație.
- PipedInputStream este, de asemenea, canalizat cu PipedOutputStream. Deci datele pot fi scrise folosind PipedOutputStream și pot fi scrise folosind PipedInputStream. Dar utilizarea ambelor fire în același timp va crea un blocaj pentru fire.
- Se spune că o conductă este ruptă dacă un fir care furniza octeți de date fluxului de ieșire conectat nu mai este viu.
Declaraţie: public class PipedInputStream extends InputStream
Constructor: | PipedInputStream() : | creează un PipedInputStream care nu este conectat.
| PipedInputStream(int pSize): | creează un PipedInputStream care nu este conectat la dimensiunea țevii specificată.
| PipedInputStream(PipedOutputStream outStream): | creează un PipedInputStream care este conectat la PipedOutputStream - 'outStream'.
| PipedInputStream(PipedOutputStream outStream int pSize): | creează un flux de intrare Piped care este conectat la fluxul de ieșire cu conducte cu dimensiunea de conductă specificată. Metode: | int read(): | 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 (); } } } Ieșire: 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) citește până la maxlen octeți ai datelor de la Piped Input Stream în matricea de buffere. Metoda blochează dacă sfârșitul fluxului este atins sau se aruncă o excepție. Sintaxa: 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.
| primire(int byte): | java.io.PipedInputStream.receive(int byte) primește octetul de date. Dacă nu este disponibilă nicio intrare, metoda se blochează. Sintaxa: 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.
| close() : | java.io.PipedInputStream.close() închide fluxul de intrare Piped și eliberează resursele alocate. Sintaxa: public void close() Parameters : -------------- Return : void Exception : -> IOException : if in case IO error occurs.
| conecta (sursa PipedOutputStream): | java.io.PipedInputStream.connect(sursa PipedOutputStream) conectează fluxul de intrare cu conducte la fluxul de ieșire „sursă” și în cazul în care „sursa” este conducte cu o altă excepție IO de flux. Sintaxa: 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.
| disponibil() : | java.io.PipedInputStream.available() returneaza nr. de octeți care pot fi citiți din fluxul de intrare fără a fi blocați efectiv. Sintaxa: 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.
Program Java care explică funcționarea metodelor clasei 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 (); } } } Ieșire: Use of available() : 5 Using read(buffer offset maxlen) : GEEKS Closing the stream
Next Article: Clasa Java.io.PipedOutputStream în Java Creați un test