Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Java - PipedInputStream close() method



Description

The Java PipedInputStream close() method closes this piped input stream and releases any system resources associated with the stream.

Declaration

Following is the declaration for java.io.PipedInputStream.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 PipedInputStream close() method

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

PipedInputStreamDemo.java

package com.tutorialspoint;

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

public class PipedInputStreamDemo {
   public static void main(String[] args) {
   
      // create a new Piped input and Output Stream
      PipedOutputStream out = new PipedOutputStream();
      PipedInputStream in = new PipedInputStream();

      try {
         // connect input and output
         in.connect(out);

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

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

         // close the stream
         System.out.println("Closing Stream...");
         in.close();
         System.out.println("Stream Closed.");
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

F
G
Closing Stream...
Stream Closed. 

Example - Closing PipedInputStream after reading data

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

PipedInputStreamDemo.java

package com.tutorialspoint;

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

public class PipedInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Create and connect piped streams
         PipedOutputStream pos = new PipedOutputStream();
         PipedInputStream pis = new PipedInputStream(pos);

         // Write some data
         pos.write("Hello, Pipe!".getBytes());

         // Read data from input stream
         byte[] buffer = new byte[pis.available()];
         pis.read(buffer);
         System.out.println("Read: " + new String(buffer));

         // Close the input stream
         pis.close();
         pos.close();

         System.out.println("PipedInputStream closed successfully.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Read: Hello, Pipe!
PipedInputStream closed successfully.

Explanation

  • close() is used to release system resources associated with the input stream.

  • After reading the data, the input stream is closed to prevent memory leaks or resource exhaustion.

  • Always closing both ends of a pipe (PipedInputStream and PipedOutputStream) is best practice.

Example - Handling exceptions when writing after PipedInputStream is closed

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

PipedInputStreamDemo.java

package com.tutorialspoint;

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

public class PipedInputStreamDemo {
   public static void main(String[] args) {
      try {
         PipedOutputStream pos = new PipedOutputStream();
         PipedInputStream pis = new PipedInputStream(pos);

         // Close input stream first
         pis.close();
         System.out.println("PipedInputStream closed.");

         // Try writing to the pipe after input stream is closed
         pos.write("Should fail".getBytes());  // This will throw IOException
      } catch (IOException e) {
         System.out.println("IOException caught: " + e.getMessage());
      }
   }
}

Output

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

PipedInputStream closed.
IOException caught: Pipe closed

Explanation

  • Once PipedInputStream is closed, any attempt to write to the connected PipedOutputStream will throw an IOException.

  • This helps ensure system integrity and avoids writing to a broken pipe.

  • Demonstrates why you should carefully coordinate stream closing in multi-threaded or piped stream communication.

java_io_pipedinputstream.htm
Advertisements