Java - PipedWriter write(char[] cbuf, int off, int len) method



Description

The Java PipedWriter write(char[] cbuf, int off, int len) method writes len characters from the specified character array starting at offset off to this piped output stream. This method blocks until all the characters are written to the output stream. If a thread was reading data characters from the connected piped input stream, but the thread is no longer alive, then an IOException is thrown.

Declaration

Following is the declaration for java.io.PipedWriter.write(char[] cbuf, int off, int len) method.

public void write(char[] cbuf, int off, int len)

Parameters

  • cbuf − The data.

  • off − The start offset in the data.

  • len − The number of characters to write.

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of PipedWriter write(char[] cbuf, int off, int len) method

The following example shows the usage of PipedWriter write(char[] cbuf, int off, int len) method.

PipedWriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;

public class PipedWriterDemo {
   public static void main(String[] args) {
      char[] c = {'h', 'e', 'l', 'l', 'o'};
      
      // create a new Piped writer and reader
      PipedWriter writer = new PipedWriter();
      PipedReader reader = new PipedReader();

      try {
         // connect the reader and the writer
         writer.connect(reader);

         // write something
         writer.write(c, 0, 3);

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

Output

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

h
e
l

Example - Write part of a character array (basic use)

The following example shows the usage of PipedWriter write(char[] cbuf, int off, int len) method.

PipedWriterDemo.java

package com.tutorialspoint;

import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.IOException;

public class PipedWriterDemo {
   public static void main(String[] args) {
      try {
         PipedReader reader = new PipedReader();
         PipedWriter writer = new PipedWriter();
         writer.connect(reader);

         char[] message = "Hello, Java World!".toCharArray();

         // Write only "Java" from the message
         writer.write(message, 7, 4); // offset 7, length 4
         writer.close();

         int ch;
         while ((ch = reader.read()) != -1) {
            System.out.print((char) ch); // Output: Java
         }

         reader.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Java

Explanation

  • Only a specific part ("Java") of the char array is written using offset = 7 and length = 4.

  • This allows selective writing from larger data blocks.

Example - Multithreaded write using a slice of a char array

The following example shows the usage of PipedWriter write(char[] cbuf, int off, int len) method.

PipedWriterDemo.java

package com.tutorialspoint;

import java.io.PipedReader;
import java.io.PipedWriter;
import java.io.IOException;

public class PipedWriterDemo {
   public static void main(String[] args) {
      try {
         PipedReader reader = new PipedReader();
         PipedWriter writer = new PipedWriter();
         writer.connect(reader);

         char[] data = "Multithreaded Communication".toCharArray();

         // Writer thread writes "Communication"
         Thread writerThread = new Thread(() -> {
            try {
               writer.write(data, 13, 14); // offset 13, length 14
               writer.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         });

         // Reader thread prints received data
         Thread readerThread = new Thread(() -> {
            try {
               int ch;
               while ((ch = reader.read()) != -1) {
                  System.out.print((char) ch); // Output: Communication
               }
               reader.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         });

         writerThread.start();
         readerThread.start();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Communication

Explanation

  • "Communication" starts at index 13 and is 13 characters long in the original string.

  • The writer thread writes this portion; the reader thread reads and prints it.

  • This showcases how partial arrays are used effectively with threading.

java_io_pipedwriter.htm
Advertisements