PipedInputStream read(byte[], int, int) in Java

read(byte[], int, int): This method is available in the java.io.PipedInputStream class of Java.

Syntax:

int java.io.PipedInputStream.read(byte[] b, int off, int len) throws IOException

This method takes three arguments. This method reads up to len bytes of data from this piped input stream into an array of bytes.

Note:

1. Less than len bytes will be read if the end of the data stream is reached or if len exceeds the pipe's buffer size.

2. If len is zero, then no bytes are read and 0 is returned; otherwise, the method blocks until at least 1 byte of input are available, the end of the stream has been detected, or an exception is thrown.

Parameters: Three parameters are required for this method.

b: the buffer into which the data is read.

off: the start offset in the destination array b.

len: the maximum number of bytes read.

Returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Throws:

1. NullPointerException - If b is null.

2. IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off.

3. IOException - if the pipe is broken, unconnected, closed, or if an I/O error occurs.

Approach 1: When no exception

Java

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

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

        PipedInputStream pipedInputStream = new PipedInputStream();

        PipedOutputStream pipedOutputStream = new PipedOutputStream();

        pipedInputStream.connect(pipedOutputStream);
        byte b[] = { 'a', 'b', 'd', 'd', 'e', 'f' };
        int off = 0, len = 3;
        System.out.println(pipedInputStream.read(b, off, len));

        pipedInputStream.close();
    }
}

Output:

-1


Approach 2: NullPointerException

Java

package com.PipedInputStream;

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

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

        PipedInputStream pipedInputStream =
new PipedInputStream();

        PipedOutputStream pipedOutputStream =
new PipedOutputStream();

        pipedInputStream.connect(pipedOutputStream);
        byte b[] = null;
        int off = 0, len = 3;
        System.out.println(pipedInputStream.read(b, off, len));

        pipedInputStream.close();
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.io.PipedInputStream.read(PipedInputStream.java:369) at com.PipedInputStream.PipedInputStreamread2.main(PipedInputStreamread2.java:17)


Approach 2: IndexOutOfBoundsException

Java

package com.PipedInputStream;

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

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

        PipedInputStream pipedInputStream =
new PipedInputStream();

        PipedOutputStream pipedOutputStream =
new PipedOutputStream();

        pipedInputStream.connect(pipedOutputStream);
        byte b[] = { 'a', 'b', 'd', 'd', 'e', 'f' };
        int off = 0, len = 13;
        System.out.println(pipedInputStream.read(b, off, len));

        pipedInputStream.close();
    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.PipedInputStream.read(PipedInputStream.java:371) at com.PipedInputStream.PipedInputStreamread2.main(PipedInputStreamread2.java:17)


Approach 3: IOException

Java

package com.PipedInputStream;

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

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

        PipedInputStream pipedInputStream =
new PipedInputStream();

        byte b[] = { 'a', 'b', 'd', 'd', 'e', 'f' };
        int off = 0, len = 3;
        System.out.println(pipedInputStream.read(b, off, len));

        pipedInputStream.close();
    }
}

Output:

Exception in thread "main" java.io.IOException: Pipe not connected at java.base/java.io.PipedInputStream.read(PipedInputStream.java:305) at java.base/java.io.PipedInputStream.read(PipedInputStream.java:377) at com.PipedInputStream.PipedInputStreamread2.main(PipedInputStreamread2.java:13)


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