Java - PipedOutputStream flush() method



Description

The Java PipedOutputStream flush() method flushes this output stream and forces any buffered output bytes to be written out. This will notify any readers that bytes are waiting in the pipe.

Declaration

Following is the declaration for java.io.PipedOutputStream.flush() method.

public void flush()

Parameters

NA

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of PipedOutputStream flush() method

The following example shows the usage of PipedOutputStream flush() 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) {
   
      // 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(70);
         out.write(71);

         // flush the stream
         out.flush();

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

Output

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

F
G

Example - Using flush() with PipedOutputStream directly

The following example shows the usage of PipedOutputStream flush() 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) {
      try {
         PipedInputStream input = new PipedInputStream();
         PipedOutputStream output = new PipedOutputStream(input);

         // Write data and flush
         output.write("Hello".getBytes());
         output.flush(); // Not strictly necessary, but good form
         output.close();

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

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

Output

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

Hello

Explanation

  • Here, flush() is called after writing to ensure data is pushed through.

  • In PipedOutputStream, it's often a no-op but can help signal intent and maintain consistency when used with other OutputStream types.

Example - Using flush() with PrintWriter over PipedOutputStream

The following example shows the usage of PipedOutputStream flush() method.

PipedOutputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintWriter;

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

      // Wrap PipedOutputStream with PrintWriter
      PrintWriter writer = new PrintWriter(output);

      // Consumer thread
      Thread readerThread = new Thread(() -> {
         try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            String line;
            while ((line = reader.readLine()) != null) {
               System.out.println("Received: " + line);
            }
            reader.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      });

      readerThread.start();

      // Write and flush data to the pipe
      writer.println("Message from producer");
      writer.flush(); // Ensures data is immediately available to the reader

      writer.close();
   }
}

Output

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

Received: Message from producer

Explanation

  • This example shows a producer-consumer thread model.

  • flush() is essential here because PrintWriter buffers output − without flush(), the reader might block waiting for data.

  • Ensures timely delivery of messages through the pipe.

java_io_pipedoutputstream.htm
Advertisements