PipedInputStream PipedInputStream(PipedOutputStream, int) in Java

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

Syntax:

java.io.PipedInputStream.PipedInputStream(PipedOutputStream src, int pipeSize) throws IOException

This method takes two arguments. 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.

Data bytes written to src will then be available as input from this stream.

Parameters: Two parameters are required for this method.

src: the stream to connect to.

pipeSize: the size of the pipe's buffer.

Throws:

1. IOException - if an I/O error occurs.

2. IllegalArgumentException - if pipeSize <= 0.

Approach 1: When no exception

Java

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

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

        PipedOutputStream src = new PipedOutputStream();
        int pipeSize = 10;
        PipedInputStream pipedInputStream =
new PipedInputStream(src, pipeSize);

        System.out.println(pipedInputStream);

        pipedInputStream.close();
    }
}

Output:

java.io.PipedInputStream@4361bd48


Approach 2: IllegalArgumentException

Java

package com.PipedInputStream;

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

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

        PipedOutputStream src = new PipedOutputStream();
        int pipeSize = 0;
        PipedInputStream pipedInputStream =
new PipedInputStream(src, pipeSize);

        System.out.println(pipedInputStream);

        pipedInputStream.close();
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Pipe Size <= 0 at java.base/java.io.PipedInputStream.initPipe(PipedInputStream.java:159) at java.base/java.io.PipedInputStream.<init>(PipedInputStream.java:125) at com.PipedInputStream.PipedInputStreamPipedInputStream4.main(PipedInputStreamPipedInputStream4.java:12)


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