Java.io.PipedOutputStream.write() Method



Description

The java.io.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() 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

The following example shows the usage of java.io.PipedOutputStream.write() method.

package com.tutorialspoint;

import java.io.*;

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();
      PipedInputStreamDemo in = new PipedInputStreamDemo();

      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();
      }
   }
}

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

hel
java_io_pipedoutputstream.htm
Advertisements