PipedOutputStream write(byte[], int, int) in Java

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

Syntax:

void java.io.PipedOutputStream.write(byte[] b, int off, int len) throws IOException

This method takes three arguments. This method writes len bytes from the specified byte array starting at offset off to this piped output stream.

This method blocks until all the bytes are written to the output stream.

Parameters: Three parameters are required for this method.

b: the data.

off: the start offset in the data.

len: the number of bytes to write.

Returns: NA

Throws:

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

2. NullPointerException - If b is null.

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

Approach 1: When no exception

Java

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

public class PipedOutputStreamwrite2 {
    public static void main(String[] args) throws IOException {
        PipedOutputStream pipedOutputStream =
new PipedOutputStream();

        PipedInputStream pipedInputStream =
new PipedInputStream();
        pipedOutputStream.connect(pipedInputStream);
        byte b[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
        int off = 0, len = 3;
        pipedOutputStream.write(b, off, len);

        System.out.println("Successfully writes");
        pipedOutputStream.close();

    }
}

Output:

Successfully writes


Approach 2: IOException 

Java

package com.PipedOutputStream;

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

public class PipedOutputStreamwrite2 {
    public static void main(String[] args) throws IOException {
        PipedOutputStream pipedOutputStream =
new PipedOutputStream();

        byte b[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
        int off = 0, len = 3;
        pipedOutputStream.write(b, off, len);

        System.out.println("Successfully writes");
        pipedOutputStream.close();

    }
}

Output:

Exception in thread "main" java.io.IOException: Pipe not connected at java.base/java.io.PipedOutputStream.write(PipedOutputStream.java:139) at com.PipedOutputStream.PipedOutputStreamwrite2.main(PipedOutputStreamwrite2.java:12)


Approach 3: NullPointerException 

Java

package com.PipedOutputStream;

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

public class PipedOutputStreamwrite2 {
    public static void main(String[] args) throws IOException {
        PipedOutputStream pipedOutputStream =
new PipedOutputStream();

        PipedInputStream pipedInputStream =
new PipedInputStream();
        pipedOutputStream.connect(pipedInputStream);
        byte b[] = null;
        int off = 0, len = 3;
        pipedOutputStream.write(b, off, len);

        System.out.println("Successfully writes");
        pipedOutputStream.close();

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.io.PipedOutputStream.write(PipedOutputStream.java:141) at com.PipedOutputStream.PipedOutputStreamwrite2.main(PipedOutputStreamwrite2.java:15)


Approach 4: IndexOutOfBoundsException

Java

package com.PipedOutputStream;

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

public class PipedOutputStreamwrite2 {
    public static void main(String[] args) throws IOException {
        PipedOutputStream pipedOutputStream =
new PipedOutputStream();

        PipedInputStream pipedInputStream =
new PipedInputStream();
        pipedOutputStream.connect(pipedInputStream);
        byte b[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
        int off = 0, len = 13;
        pipedOutputStream.write(b, off, len);

        System.out.println("Successfully writes");
        pipedOutputStream.close();

    }
}

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.base/java.io.PipedOutputStream.write(PipedOutputStream.java:144) at com.PipedOutputStream.PipedOutputStreamwrite2.main(PipedOutputStreamwrite2.java:15)


Some other methods of PipedOutputStream

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

connect(PipedInputStream)This method connects this piped output stream to a receiver.

flush()This method flushes this output stream and forces any buffered output by the test to be written out.

PipedOutputStream()This method creates a piped output stream that is not yet connected to a piped input stream.

PipedOutputStream(PipedInputStream)This method creates a piped output stream connected to the specified piped input stream.

write(int)This method writes the specified byte to the piped output stream.

write(byte[], int, int)This method writes len bytes from the specified byte array starting at offset off to this piped output stream.

No comments:

Post a Comment