Java.io.PipedOutputStream Class ב-Java
מחלקה Java.io.PipedInputStream ב-Java
צינורות ב-IO מספקים קישור בין שני שרשורים הפועלים ב-JVM בו-זמנית. אז צינורות משמשים הן כמקור או כיעד.
- PipedInputStream מועבר גם עם PipedOutputStream. כך שניתן לכתוב נתונים באמצעות PipedOutputStream וניתן לכתוב אותם באמצעות PipedInputStream. אבל שימוש בשני השרשורים בו זמנית ייצור מבוי סתום עבור השרשורים.
- PipedOutputStream שולח את קצה הצינור. הנתונים נכתבים ל- PipedOutputStream. אומרים שהצינור נשבר אם ה-PipedInputStream שקרא את הנתונים אינו עוד.
הַצהָרָה:
public class PipedOutputStream
extends OutputStream
בַּנַאִי:
- PipedOutputStream() : יוצר PipedOutputStream שהוא לא מחובר.
- PipedOutputStream(PipedOutputStream inStream): יוצר PipedOutputStream שזה
מחובר ל-PipedInputStream - 'inStream'.
שיטות:
write() : java.io.PipedOutputStream.write(int byte) כותב בייט שצוין לזרם הפלט Piped.
תחביר:
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) כותב maxlen bytes של הנתונים מהמאגר ל-Piped Output Stream. השיטה חוסמת אם לא נכתבים בתים לזרם.
תחביר:
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 -- ; } } }תְפוּקָה:
Use of write(buffer offset maxlen) : J A V A
- close() : java.io.PipedOutputStream.close() סוגר את זרם הפלט Piped ומשחרר את המשאבים שהוקצו.
תחביר:
public void close()
Parameters :
--------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.
- connect(PipedInputStream יעד) : java.io.PipedOutputStream.connect(יעד PipedInputStream מחבר את ה-Piped Output Stream ל-'destination' Piped Input Stream ובמקרה ש'יעד' הוא צינורות עם חריג אחר של זרם IO נזרק
תחביר:
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() שוטף את זרם הפלט.
תחביר:
public void flush()
Parameters :
------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.
קוד Java הממחיש את פעולתן של שיטות מחלקה 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 (); } } }תְפוּקָה:
Use of flush() method :
G E E K S
Closing the Output stream
צור חידון