Java.io - SPLessons

Java.io PipedOutputStream

Home > Lesson > Chapter 37
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io PipedOutputStream

Java.io PipedOutputStream

shape Introduction

The Java.io PipedOutputStream is the class used for writing the content of the pipe as a byte stream. The Java.io PipedOutputStream class is subclass to output stream class. The pipes are acts as communication channel in between the threads under similar JVM. The piped output stream is connected with the same piped input stream but both are processed by two different threads. In case of piped output stream call the close method explicitly.

shape Conceptual figure

Class Declaration

shape Declaration

Java.io PipedOutputStream class is declared below: Public class PipedOutputStream extends OutputStream

Class Constructors

shape Table

Constructor Description
PipedOutputStream() The function of this constructor is to create the piped output stream instance and not connected with piped input stream.
PipedOutputStream(PipedInputStream snk) The function of this constructor is to create the piped output stream instance which is connected to the represented piped input stream.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released.
void connect(pipedInputStream snk) The function of this method is to connect the piped output stream with piped input stream.
void write(byte[] b, int off, int n) By using this method it writes the data up to ‘n’ bytes from the output stream to an array of bytes.
void write(int b) The function of this method is to write the represented bytes into output stream.

Inherited Methods

shape Description

From the following classes, methods are inherited to the piped output stream class.
  • Java.io.Object
  • Java.io.OutputStream

shape Examples

Usage of void close() method. [c]import java.io.*; public class PipedOutputStream extends PipedInputStream { public static void main(String[] args) { // creating a new Piped input and Output Stream PipedOutputStream out = new PipedOutputStream(); PipedInputStream in = new PipedInputStream(); try { // connect input and output streams out.connect(in); // T0 write something out.write(83); out.write(80); // To close the stream out.close(); System.out.println("Stream Closed."); // print for (int i = 0; i < 2; i++) { System.out.println("" + (char) in.read()); } } catch (IOException ex) { ex.printStackTrace(); } } }[/c] Output The result will be as follows. [c]Stream Closed. S P[/c] Usage of void write(byte[] b, int off, int n) meyhod. [c]import java.io.*; public class PipedOutputStreamDemo extends PipedInputStream { public static void main(String[] args) { byte[] b = {'S', 'P', 'L', 'E', 'S', 'S', 'O', 'N', 'S'}; // creating a new Piped input and Output Stream PipedOutputStream out = new PipedOutputStream(); PipedInputStream in = new PipedInputStream(); try { // To connect input and output streams out.connect(in); // write something out.write(b, 0, 8); // flush the stream out.flush(); // print the string written above for (int i = 2; i < 8; i++) { System.out.print("" + (char) in.read()); } } catch (IOException ex) { ex.printStackTrace(); } } }[/c] Output The result will be as follows. [c]SPLESS[/c]

Summary

shape Key Points

  • The Java.io PipedOutputStream is the class used for writing the content of the pipe as a byte stream.
  • The pipes are acts as communication channel in between the threads under similar JVM.
  • The Java.io PipedOutputStream is connected with the same piped input stream but both are processed by two different threads.