Java - PipedReader read(char[] cbuf, int off, int len) method



Description

The Java PipedReader read(char[] cbuf, int off, int len) method reads up to len characters of data from this piped stream into an array of characters. Less than len characters will be read if the end of the data stream is reached or if len exceeds the pipe's buffer size. This method blocks until at least one character of input is available.

Declaration

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

public int read(char[] cbuf, int off, int len)

Parameters

  • cbuf − The buffer into which the data is read.

  • off − The start offset of the data.

  • len − The maximum number of characters read.

Return Value

This method returns the total number of characters read into the buffer, or -1 if there is no more data because the end of the stream has been reached.

Exception

IOException − If an I/O error occurs.

Example - Usage of PipedReader read(char[] cbuf, int off, int len) method

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

PipedReaderDemo.java

package com.tutorialspoint;

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

public class PipedReaderDemo {
   public static void main(String[] args) {
   
      // create a new Piped writer and reader
      PipedWriter writer = new PipedWriter();
      PipedReader reader = new PipedReader();

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

         // write something
         writer.write(70);
         writer.write(71);

         // read into a char array
         char[] b = new char[2];
         reader.read(b, 0, 2);

         // print the char array
         for (int i = 0; i < 2; i++) {
            System.out.println("" + b[i]);
         }
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

F
G

Example - Reading a portion of data into a character array

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

PipedReaderDemo.java

package com.tutorialspoint;

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

public class PipedReaderDemo {
   public static void main(String[] args) {
      try {
         PipedReader reader = new PipedReader();
         PipedWriter writer = new PipedWriter(reader); // Connect via constructor

         writer.write("HelloWorld");
         writer.close();

         char[] buffer = new char[10];
         int charsRead = reader.read(buffer, 2, 5); // Read 5 characters starting at index 2

         System.out.println("Characters read: " + charsRead);
         System.out.println("Buffer content: ");
         for (char c : buffer) {
            System.out.print(c == 0 ? '-' : c); // Use '-' to show unfilled buffer slots
         }

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

Output

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

Characters read: 5
Buffer content: 
--Hello---

Explanation

  • read(char[] cbuf, int off, int len) reads up to len characters into cbuf, starting at offset off.

  • In this example, 5 characters are read into buffer starting from index 2, so only part of the buffer is filled.

  • The remaining positions are untouched (\u0000, shown here as -).

Example - Inter-thread communication with buffer reading

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

PipedReaderDemo.java

package com.tutorialspoint;

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

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

      Thread producer = new Thread(() -> {
         try {
            writer.write("Buffered PipedReader Example");
            writer.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      });

      Thread consumer = new Thread(() -> {
         try {
            char[] buffer = new char[8];
            int n;
            while ((n = reader.read(buffer, 0, buffer.length)) != -1) {
               System.out.print(new String(buffer, 0, n));
            }
            reader.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−

Buffered PipedReader Example

Explanation

  • The producer thread writes a full string.

  • The consumer thread reads the data into an 8-character buffer and prints it in chunks.

  • Demonstrates reading multiple characters efficiently using read(char[], off, len) for performance.

java_io_pipedreader.htm
Advertisements