צינורות ב-IO מספק קישור בין שני שרשורים הפועלים ב-JVM בו-זמנית. אז צינורות משמשים הן כמקור או כיעד.
PipedInputStream מועבר גם עם PipedOutputStream. כך שניתן לכתוב נתונים באמצעות PipedOutputStream וניתן לכתוב אותם באמצעות PipedInputStream. אבל שימוש בשני השרשורים בו זמנית ייצור מבוי סתום עבור השרשורים.
אומרים שצינור נשבר אם חוט שסיפק בתים של נתונים לזרם הפלט המחובר אינו חי עוד.
הַצהָרָה:
public class PipedInputStream extends InputStream
קונסטרוקטור:
PipedInputStream() :
יוצר PipedInputStream שהוא לא מחובר.
PipedInputStream(int pSize):
יוצר PipedInputStream שאינו מחובר לגודל הצינור שצוין.
PipedInputStream(PipedOutputStream OutStream):
יוצר PipedInputStream שהוא מחובר ל- PipedOutputStream - 'outStream'.
PipedInputStream(PipedOutputStream outStream int pSize):
יוצר זרם קלט צינור המחובר לזרם פלט צינור עם גודל הצינור שצוין. שיטות:
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() methodimportjava.io.*;publicclassNewClass{publicstaticvoidmain(String[]args)throwsIOException{PipedInputStreamgeek_input=newPipedInputStream();PipedOutputStreamgeek_output=newPipedOutputStream();try{// Use of connect() : connecting geek_input with geek_outputgeek_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(IOExceptionexcept){except.printStackTrace();}}}
פלט:
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) קורא עד מקסימום בתים של הנתונים מ-Piped Input Stream למערך המאגרים. השיטה חוסמת אם מגיעים לסוף הזרם או נזרק חריג. תחביר:
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.
receive(int byte) :
java.io.PipedInputStream.receive(int byte) מקבל בייט של הנתונים. אם אין קלט זמין, השיטה חוסמת. תחביר:
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() סוגר את זרם הקלט בצינור ומשחרר את המשאבים שהוקצו. תחביר:
public void close() Parameters : -------------- Return : void Exception : -> IOException : if in case IO error occurs.
connect(מקור PipedOutputStream):
java.io.PipedInputStream.connect(מקור PipedOutputStream) מחבר את זרם הקלט בצינור לזרם פלט צינור 'מקור' ובמקרה ש'מקור' הוא צינורות עם חריג אחר של זרם IO נזרק תחביר:
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.
זמין() :
java.io.PipedInputStream.available() מחזיר לא. של בתים שניתן לקרוא מ-Input Stream מבלי להיחסם בפועל. תחביר:
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.
תוכנית Java המסבירה את פעולתן של שיטות מחלקה PipedInputStream: Java
// Java program illustrating the working of PipedInputStream// connect() read(byte[] buffer int offset int maxlen)// close() available()importjava.io.*;publicclassNewClass{publicstaticvoidmain(String[]args)throwsIOException{PipedInputStreamgeek_input=newPipedInputStream();PipedOutputStreamgeek_output=newPipedOutputStream();try{// Use of connect() : connecting geek_input with geek_outputgeek_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=newbyte[5];// destination 'buffer'geek_input.read(buffer05);Stringstr=newString(buffer);System.out.println('Using read(buffer offset maxlen) : '+str);// USe of close() method :System.out.println('Closing the stream');geek_input.close();}catch(IOExceptionexcept){except.printStackTrace();}}}
תְפוּקָה:
Use of available() : 5 Using read(buffer offset maxlen) : GEEKS Closing the stream
Next Article: מחלקה Java.io.PipedOutputStream ב-Java צור חידון