Rör i IO ger en länk mellan två trådar som körs i JVM samtidigt. Så Pipes används både som källa eller destination.
PipedInputStream är också kopplad till PipedOutputStream. Så data kan skrivas med PipedOutputStream och kan skrivas med PipedInputStream. Men att använda båda trådarna samtidigt kommer att skapa ett dödläge för trådarna.
Ett rör sägs vara brutet om en tråd som tillhandahöll databytes till den anslutna piped utströmmen inte längre är vid liv.
Förklaring:
public class PipedInputStream extends InputStream
Konstruktör:
PipedInputStream() :
skapar en PipedInputStream som den inte är ansluten.
PipedInputStream(int pSize):
skapar en PipedInputStream som inte är ansluten till angiven rörstorlek.
PipedInputStream(PipedOutputStream outStream) :
skapar en PipedInputStream som den är ansluten till PipedOutputStream - 'outStream'.
PipedInputStream(PipedOutputStream outStream int pSize) :
skapar en Piped Input Stream som är ansluten till Piped Output Stream med angiven rörstorlek. Metoder:
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();}}}
Utgång:
using read() : G using read() : E using read() : K
read(byte[] buffert int offset int maxlen):
java.io.PipedInputStream.read(byte[] buffert int offset int maxlen) läser upp till maxlen byte av data från Piped Input Stream till arrayen av buffertar. Metoden blockerar om slutet av Stream nås eller undantag kastas. Syntax :
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.
motta(int byte):
java.io.PipedInputStream.receive(int byte) tar emot byte av data. Om ingen ingång är tillgänglig blockeras metoden. Syntax :
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() stänger Piped Input Stream och släpper de tilldelade resurserna. Syntax :
public void close() Parameters : -------------- Return : void Exception : -> IOException : if in case IO error occurs.
connect(PipedOutputStream-källa):
java.io.PipedInputStream.connect(PipedOutputStream-källa) ansluter Piped Input Stream till "källa" Piped Output Stream och om "källa" är rör med något annat ström IO-undantag kastas Syntax :
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.
tillgänglig() :
java.io.PipedInputStream.available() returnerar nr. av byte som kan läsas från Input Stream utan att faktiskt blockeras. Syntax :
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-program som förklarar hur PipedInputStream-klassmetoderna fungerar: 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();}}}
Produktion:
Use of available() : 5 Using read(buffer offset maxlen) : GEEKS Closing the stream
Next Article: Java.io.PipedOutputStream-klass i Java Skapa frågesport