Java.io - SPLessons

Java.io PipedInputStream

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

Java.io PipedInputStream

Java.io PipedInputStream

shape Introduction

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

shape Conceptual figure

Class Declaration

shape Declaration

Java.io PipedInputStream class is declared below: Public class PipedInputStream extends InputStream

shape Fields

Java.io PipedInputStream class fields are:
  • Protected byte [] bu : The field represents to the internal buffer where actually the data  stored.
  • Protected int in : The field represents to the index position in the buffer that to store next coming byte of data from piped output stream.
  • Protected int out : The field represents to the index position in the buffer to read next coming byte by this piped input stream.
  • Protected static int PIPE_SIZE : The field represents the default size of the input stream buffer.

Class Constructors

shape Table

Constructor Description
PipedInputStream() The function of this constructor is to create the piped input stream instance and not connected with piped output stream.
PipedInputStream(PipedOutputStream src) The function of this constructor is to create the piped input stream instance which is connected to the represented piped output stream.
PipedInputStream(int pipeSize) The function of this constructor is to create the piped input stream instance with specified size of buffer or pipe.
PipedInputStream(PipedOutputStream src, int pipeSize) The function of this constructor is to create the piped input stream instance with specified size of buffer or pipe with the connection to piped output 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(pipedOutputStream src) The function of this method is to connect the piped input stream with represented piped output stream.
int read(byte[] b, int off, int n) By using this method it reads the data up to ‘n’ bytes from the piped input stream into array of  the bytes.
int read() The function of this method is to read one byte of data from this stream.

Inherited Methods

shape Description

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

shape Examples

Usage of void close() method. [c]import java.io.*; public class 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 in.connect(out); // write something out.write(83); out.write(80); // read what we wrote above 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(); } } }[/c] Output Following is the result will be displayed. [c] S P Closing Stream........... Stream Closed.[/c] Usage of void connect(pipedOutputStream src) method. [c]import java.io.*; public class 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 in.connect(out); // write something out.write(83); out.write(80); // read what we wrote above for (int i = 0; i< 2; i++) { System.out.println("" + (char) in.read()); } } catch (IOException ex) { ex.printStackTrace(); } } }[/c] Output Following is the result will be displayed. [c]S P[/c]

Summary

shape Key Points

  • The Java.io PipedInputStream is the class used for reading 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 PipedInputStream is connected with the same piped output stream but both are processed by two different threads.