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

Java - PipedOutputStream write(int b) method



Description

The Java PipedOutputStream write(int b) method writes the specified byte to the piped output stream. Implements the write method of OutputStream.

Declaration

Following is the declaration for java.io.PipedOutputStream.write(int b) method.

public void write(int b)

Parameters

b − The byte to be written.

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of PipedOutputStream write(int b) method

The following example shows the usage of PipedOutputStream write(int b) 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) {
      byte[] b = {'h', 'e', 'l', 'l', 'o'};

      // 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);
         out.write(72);

         // flush the stream
         out.flush();

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

Output

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

FGH

Example - Writing individual characters using write(int b)

The following example shows the usage of PipedOutputStream write(int b) 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); // Connect streams

         // Write characters one by one using write(int b)
         output.write('A');
         output.write('B');
         output.write('C');

         output.close();

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

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

Output

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

ABC

Explanation

  • write(int b) writes a single byte (the lower 8 bits of the integer).

  • Here, 'A', 'B', and 'C' are written one at a time.

  • The connected PipedInputStream receives and reads each byte.

Example - Writing ASCII codes using write(int b) in a loop

The following example shows the usage of PipedOutputStream write(int b) 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) throws IOException {
      PipedInputStream input = new PipedInputStream();
      PipedOutputStream output = new PipedOutputStream();
      output.connect(input); // Connect manually

      // Writer thread: sends numbers 65 to 69 (ASCII A to E)
      Thread writer = new Thread(() -> {
         try {
            for (int i = 65; i <= 69; i++) {
               output.write(i);
            }
            output.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      });

      // Reader thread
      Thread reader = new Thread(() -> {
         try {
            int ch;
            while ((ch = input.read()) != -1) {
               System.out.print((char) ch); // Output: ABCDE
            }
            input.close();
         } catch (IOException e) {
            e.printStackTrace();
         }
      });

      writer.start();
      reader.start();
   }
}

Output

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

ABCDE

Explanation

  • The writer sends ASCII codes 65 to 69 (i.e., A to E) using write(int b).

  • The reader prints each character as it receives it from the pipe.

  • Demonstrates how to use write(int b) in a threaded, producer-consumer pattern.

java_io_pipedoutputstream.htm
Advertisements