Java - PipedInputStream receive(int b) method



Description

The Java PipedInputStream receive(int b) method receives a byte of data. This method will block if no input is available.

Declaration

Following is the declaration for java.io.PipedInputStream.receive(int b) method.

public void receive(int b)

Parameters

  • b − The byte being received.

Return Value

This method does not return a value.

Exception

  • IOException − If the pipe is broken, unconnected, closed, or if an I/O error occurs.

Example - Usage of PipedInputStream receive(int b) method

The following example shows the usage of PipedInputStream receive(int b) method.

PipedInputStreamDemo.java

package com.tutorialspoint;

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

public class PipedInputStreamDemo extends PipedInputStream {
   public static void main(String[] args) {

      // create a new Piped input and Output Stream
      PipedOutputStream out = new PipedOutputStream();
      PipedInputStreamDemo in = new PipedInputStreamDemo();

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

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

         // receive a byte 
         System.out.println("Receiving Byte...");
         in.receive(71);
         System.out.println("Byte Received.");
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

Receiving Byte...
Byte Received.

Example - Custom subclass to expose receive(int b)

The following example shows the usage of PipedInputStream receive(int b) method.

PipedInputStreamDemo.java

package com.tutorialspoint;

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

public class PipedInputStreamDemo extends PipedInputStream {
   public PipedInputStreamDemo() throws IOException {
      super(); // Do not call super(PipedOutputStream) here
   }

   // Expose protected receive method
   public void myReceive(int b) throws IOException {
      this.receive(b);
   }

   public static void main(String[] args) {
      try {
         PipedInputStreamDemo input = new PipedInputStreamDemo();
         PipedOutputStream output = new PipedOutputStream(input); // This connects the pipe

         // Manually receive a byte (now safe to do)
         input.myReceive('A');

         int value = input.read();
         System.out.println("Read value: " + (char) value); // Should print 'A'

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

Output

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

Read value: A

Explanation

  • We extend PipedInputStream to access the receive(int b) method.

  • receive() manually inserts a byte into the internal buffer, simulating a write() from a PipedOutputStream.

  • Normally, receive() is called by PipedOutputStream.write() internally.

Example - Simulate two bytes received via receive() in a custom stream

The following example shows the usage of PipedInputStream receive(int b) method.

PipedInputStreamDemo.java

package com.tutorialspoint;

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

public class PipedInputStreamDemo extends PipedInputStream {
   public PipedInputStreamDemo() throws IOException {
      super(); // default constructor
   }

   public void injectByte(int b) throws IOException {
      this.receive(b); // protected method exposed
   }

   public static void main(String[] args) {
      try {
         PipedInputStreamDemo input = new PipedInputStreamDemo();
         PipedOutputStream output = new PipedOutputStream(input); // Connects the pipe

         // Simulate writing two bytes via receive()
         input.injectByte('X');
         input.injectByte('Y');

         // Read the data from the buffer
         System.out.print("Received: ");
         System.out.print((char) input.read());
         System.out.println((char) input.read());

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

Output

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

Received: XY

Explanation

  • Again, we use a subclass to inject bytes using receive(int b).

  • This simulates how a PipedOutputStream might feed characters "XY" to a connected input stream.

  • This example is purely for illustrative purposes; in real applications, use PipedOutputStream.write() instead.

java_io_pipedinputstream.htm
Advertisements