Java - PipedOutputStream close() method



Description

The Java PipedOutputStream close() method closes this piped output stream and releases any system resources associated with this stream. This stream may no longer be used for writing bytes.

Declaration

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

public void close()

Parameters

NA

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of PipedOutputStream close() method

The following example shows the usage of PipedOutputStream close() 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);

         // close the stream
         System.out.println("Closing Stream...");
         out.close();
         System.out.println("Stream Closed.");

         // 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 −

Closing Stream...
Stream Closed.
F
G

Example - Closing the stream after writing to a connected PipedInputStream

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

PipedOutputStreamDemo.java

package com.tutorialspoint;

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

public class PipedOutputStreamDemo {
   public static void main(String[] args) {
      try {
         PipedInputStream input = new PipedInputStream();
         PipedOutputStream output = new PipedOutputStream(input); // Connect the streams

         // Write data to the pipe
         output.write("Hello Pipe!".getBytes());

         // Close the output stream to signal end of data
         output.close();

         // Read the data from input
         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 Pipe!      

Explanation

  • The close() method closes the PipedOutputStream.

  • This signals the end of data to the connected PipedInputStream, which then returns -1 to its reader.

  • Essential in producer-consumer scenarios to indicate end of transmission.

Example - Ensuring resource cleanup in a producer thread

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

PipedOutputStreamDemo.java

package com.tutorialspoint;

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

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

      // Producer thread
      Thread producer = new Thread(() -> {
         try {
            output.write("Data from producer".getBytes());
            output.close(); // Important: close to avoid blocking reader
         } catch (IOException e) {
            e.printStackTrace();
         }
      });

      // Consumer thread
      Thread consumer = new Thread(() -> {
         try {
            int ch;
            while ((ch = input.read()) != -1) {
	            System.out.print((char) ch);
            }
            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 from producer

Explanation

  • output.close() ensures that the PipedInputStream doesn't block waiting for more data.

  • If you forget to close PipedOutputStream, the input stream may wait forever, expecting more data.

  • Always close the output stream once done writing.

java_io_pipedoutputstream.htm
Advertisements