PipedInputStream connect(PipedOutputStream) in Java

connect(PipedOutputStream): This method is available in the java.io.PipedInputStream class of Java.

Syntax:

void java.io.PipedInputStream.connect(PipedOutputStream src) throws IOException

This method takes one argument. This method causes this piped input stream to be connected to the piped output stream src. If this object is already connected to another piped output stream, an IOExceptionis thrown.

If src is an unconnected piped output stream and snk is an unconnected piped input stream, they may be connected by either the call: snk.connect(src) or the call: src.connect(snk).

The two calls have the same effect.

Parameters: One parameter is required for this method.

src: The piped output stream to connect to.

Returns: NA

Throws:

IOException - if an I/O error occurs.

Approach

Java

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipedInputStreamaconnect {
    public static void main(String[] args) throws IOException {

        PipedInputStream pipedInputStream =
new PipedInputStream();

        PipedOutputStream src = new PipedOutputStream();
        pipedInputStream.connect(src);
        System.out.println("Connecting the piped input stream");
        pipedInputStream.close();

    }
}

Output:

Connecting the piped input stream


Some other methods of PipedInputStream

close()This method closes this piped input stream and releases any system resources associated with the stream.

connect(PipedOutputStream)This method causes this piped input stream to be connected to the piped output stream src.

available()This method returns the number of bytes that can be read from this input stream without blocking.

PipedInputStream()This method creates a PipedInputStream so that it is not yet connected.

PipedInputStream(int)This method creates a PipedInputStream so that it is not yet connected and uses the specified pipe size for the pipe's buffer.

PipedInputStream(PipedOutputStream)This method creates a PipedInputStream so that it is connected to the piped output stream src.

PipedInputStream(PipedOutputStream, int)This method creates a PipedInputStream so that it is connected to the piped output stream src and uses the specified pipe size for the pipe's buffer.

read()This method reads the next byte of data from this piped input stream.

read(byte[], int, int)This method reads up to len bytes of data from this piped input stream into an array of bytes.

No comments:

Post a Comment