أنابيب يوفر inIO رابطًا بين خيطين يعملان في JVM في نفس الوقت. لذلك يتم استخدام الأنابيب كمصدر أو وجهة.
يتم أيضًا توصيل PipedInputStream باستخدام PipedOutputStream. لذلك يمكن كتابة البيانات باستخدام PipedOutputStream ويمكن كتابتها باستخدام PipedInputStream. ولكن استخدام كلا الخيطين في نفس الوقت سيؤدي إلى طريق مسدود لسلاسل الرسائل.
يُقال إن الأنبوب مكسور إذا لم يعد الخيط الذي كان يوفر بايتات البيانات إلى دفق الإخراج المتصل عبر الأنابيب موجودًا.
تصريح:
public class PipedInputStream extends InputStream
منشئ :
بايبيدينبوتستريم ():
ينشئ PipedInputStream أنه غير متصل.
PipedInputStream(int pSize):
ينشئ PipedInputStream غير متصل بحجم الأنبوب المحدد.
PipedInputStream (PipedOutputStream outStream):
ينشئ PipedInputStream متصلاً بـ PipedOutputStream - 'outStream'.
PipedInputStream(PipedOutputStream outStream int pSize):
يقوم بإنشاء دفق إدخال عبر الأنابيب متصل بتدفق إخراج عبر الأنابيب بحجم الأنبوب المحدد. طُرق:
قراءة كثافة العمليات ():
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
قراءة (بايت [] المخزن المؤقت int الإزاحة int maxlen):
java.io.PipedInputStream.read (بايت [] المخزن المؤقت int تعويض 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.
تلقي (بايت كثافة العمليات):
java.io.PipedInputStream.receive (بايت صحيح) يتلقى بايت من البيانات. إذا لم يكن هناك أي إدخال متاح، فسيتم حظر الطريقة. بناء الجملة :
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.
يغلق() :
java.io.PipedInputStream.إغلاق () يغلق تدفق الإدخال عبر الأنابيب ويحرر الموارد المخصصة. بناء الجملة :
public void close() Parameters : -------------- Return : void Exception : -> IOException : if in case IO error occurs.
الاتصال (مصدر PipedOutputStream):
java.io.PipedInputStream.connect (مصدر PipedOutputStream) يربط دفق الإدخال عبر الأنابيب بتدفق الإخراج عبر الأنابيب "المصدر" وفي حالة أن "المصدر" عبارة عن أنابيب مع بعض استثناءات الإدخال والإخراج الأخرى للدفق بناء الجملة :
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() لا يعود. عدد وحدات البايت التي يمكن قراءتها من تدفق الإدخال دون حظرها فعليًا. بناء الجملة :
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 إنشاء اختبار