Java - PipedOutputStream write(byte[] b, int off, int len) method



Description

The Java PipedOutputStream write(byte[] b, int off, int len) 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.

Declaration

Following is the declaration for java.io.PipedOutputStream.write(byte[] b, int off, int len) method.

public void write(byte[] b, int off, int len)

Parameters

  • b − The data.

  • off − The start offset in the data.

  • len − The number of bytes to write.

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of PipedOutputStream write(byte[] b, int off, int len) method

The following example shows the usage of PipedOutputStream write(byte[] b, int off, int len) method.

PipedOutputStreamDemo.java

package com.tutorialspoint;

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

public class PipedOutputStreamDemo extends PipedInputStream {
   public static void main(String[] args) {
      byte[] b = {'h', 'e', 'l', 'l', 'o'};

      // create a new Piped input and Output Stream
      PipedOutputStream out = new PipedOutputStream();
      PipedOutputStreamDemo in = new PipedOutputStreamDemo();

      try {
         // connect input and output
         out.connect(in);

         // write something 
         out.write(b, 0, 3);

         // flush the stream
         out.flush();

         // print what we wrote
         for (int i = 0; i < 3; i++) {
            System.out.print("" + (char) in.read());
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

hel

Example - Writing part of a byte array to a PipedInputStream

The following example shows the usage of PipedOutputStream write(byte[] b, int off, int len) method.

PipedOutputStreamDemo.java

package com.tutorialspoint;

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

public class PipedOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      PipedInputStream input = new PipedInputStream();
      PipedOutputStream output = new PipedOutputStream(input); // connect streams

      byte[] message = "HelloWorld".getBytes();

      // Write only "World" (starting from index 5, length 5)
      output.write(message, 5, 5);
      output.close();

      int data;
      while ((data = input.read()) != -1) {
         System.out.print((char) data); // Output: World
      }

      input.close();
   }
}

Output

Let us compile and run the above program, this will produce the following result−

World

Explanation

  • The full byte array is "HelloWorld".

  • write(message, 5, 5) starts at index 5 ('W') and writes 5 bytes ("World").

  • Useful when you want to write a subset of a byte array to a stream.

Example - Producer thread writing partial chunks

The following example shows the usage of PipedOutputStream write(byte[] b, int off, int len) method.

PipedOutputStreamDemo.java

package com.tutorialspoint;

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

public class PipedOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      PipedInputStream input = new PipedInputStream();
      PipedOutputStream output = new PipedOutputStream(input);

      Thread producer = new Thread(() -> {
         try {
            byte[] data = "StreamDataExample".getBytes();

            // Write "Data" from the array (offset 6, length 4)
            output.write(data, 6, 4);
            output.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      });

      Thread consumer = new Thread(() -> {
         try {
            int ch;
            while ((ch = input.read()) != -1) {
               System.out.print((char) ch); // Output: Data
            }
            input.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      });
      producer.start();
      consumer.start();
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Data

Explanation

  • The producer writes only a portion of the string "StreamDataExample" − specifically, the "Data" segment.

  • Using write(byte[], off, len) is efficient when only part of a larger buffer is needed.

java_io_pipedoutputstream.htm
Advertisements