Java.io.PipedOutputStream Class in Java
Last Updated :
05 Mar, 2024
Java.io.PipedInputStream class in Java

Pipes in IO provide a link between two threads running in JVM at the same time. So, Pipes are used both as source or destination. Â
- PipedInputStream is also piped with PipedOutputStream. So, data can be written using PipedOutputStream and can be written using PipedInputStream.But, using both threads at the same time will create a deadlock for the threads.
- PipedOutputStream is sending end of the pipe. Data is written to the PipedOutputStream. The pipe is said to be broken if the PipedInputStream, that was reading the data is no more.
Declaration:Â Â
public class PipedOutputStream
extends OutputStream
Constructor:Â Â
- PipedOutputStream() : creates a PipedOutputStream, that it is not connected.
- PipedOutputStream(PipedOutputStream inStream) : creates a PipedOutputStream, that itÂ
is connected to PipedInputStream - 'inStream'.
Methods:Â
write() : java.io.PipedOutputStream.write(int byte) writes a specified byte to the Piped Output Stream.Â
Syntax :Â
public void write(int byte)
Parameters :
byte : byte to be written
Return : void
Exception :
-> IOException : if in case IO error occurs.
write(byte[] buffer, int offset, int maxlen) : java.io.PipedOutputStream.write(byte[] buffer, int offset, int maxlen) writes maxlen bytes of the data from buffer to the Piped Output Stream. The method blocks if no bytes are written to the Stream.Â
Syntax :Â
public void write(byte[] buffer, int offset, int maxlen)
Parameters :
buffer : data of the buffer
offset : starting in the destination array - 'buffer'.
maxlen : maximum length of array to be read
Return : void
Exception :
-> IOException : if in case IO error occurs.
Java
// Java program illustrating the working of PipedInputStream
// write(byte[] buffer, int offset, int maxlen)
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
PipedInputStream geek_input = new PipedInputStream();
PipedOutputStream geek_output = new PipedOutputStream();
// Use of connect() : connecting geek_input with geek_output
geek_input.connect(geek_output);
byte[] buffer = {'J', 'A', 'V', 'A'};
// Use of write(byte[] buffer, int offset, int maxlen)
geek_output.write(buffer, 0, 4);
int a = 5;
System.out.print("Use of write(buffer, offset, maxlen) : ");
while(a>0)
{
System.out.print(" " + (char) geek_input.read());
a--;
}
}
}
Output:Â
Use of write(buffer, offset, maxlen) : J A V A
- close() : java.io.PipedOutputStream.close() closes the Piped Output Stream and releases the allocated resources.Â
Syntax :Â
public void close()
Parameters :
--------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.
- connect(PipedInputStream destination) : java.io.PipedOutputStream.connect(PipedInputStream destination connects the Piped Output Stream to the 'destination' Piped Input Stream and in case 'destination' is pipes with some other stream, IO exception is thrownÂ
Syntax :Â
public void connect(PipedInputStream destination)
Parameters :
destination : the Piped Input Stream to be connected to
Return :
void
Exception :
-> IOException : if in case IO error occurs.
- flush() : java.io.PipedOutputStream.flush() flushes the Output Stream.Â
Syntax :Â
public void flush()
Parameters :
------------
Return :
void
Exception :
-> IOException : if in case IO error occurs.
Java code illustrating the working of PipedOutputStream class methods :Â
Java
// Java program illustrating the working of PipedInputStream
// write(), write(byte[] buffer, int offset, int maxlen),
// close(), flush(), connect()
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws IOException
{
PipedInputStream geek_input = new PipedInputStream();
PipedOutputStream geek_output = new PipedOutputStream();
try
{
// Use of connect() : connecting geek_input with geek_output
geek_input.connect(geek_output);
// Use of write(int byte) :
geek_output.write(71);
geek_output.write(69);
geek_output.write(69);
geek_output.write(75);
geek_output.write(83);
// Use of flush() method :
geek_output.flush();
System.out.println("Use of flush() method : ");
int i = 5;
while(i > 0)
{
System.out.print(" " + (char) geek_input.read());
i--;
}
// USe of close() method :
System.out.println("\nClosing the Output stream");
geek_output.close();
}
catch (IOException except)
{
except.printStackTrace();
}
}
}
Output:Â
Use of flush() method :
G E E K S
Closing the Output stream
Â
Similar Reads
Java.io.PipedInputStream class in Java
Pipes in IO provides a link between two threads running in JVM at the same time. So, Pipes are used both as source or destination. PipedInputStream is also piped with PipedOutputStream. So, data can be written using PipedOutputStream and can be written using PipedInputStream.But, using both threads
5 min read
Java.io.OutputStream class in Java
This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink. Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output. Constructo
2 min read
Java.io.LineNumberInputStream Class in Java
java.io.LineNumberInputStream class is simply an extension of input stream providing a extra facility to keep the record of current line number. Line is a sequence of bytes ending with : '\r' i.e. a carriage return character or a newline character : '\n', or a linefeed character following the carria
11 min read
Java.io.ObjectOutputStream Class in Java | Set 1
An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be read (reconstituted) using an ObjectInputStream. Persistent storage of objects can be accomplished by using a file for the stream. Only objects that support the java.io.Serializable in
9 min read
Java.io.PushbackInputStream class in Java
Pushback is used on an input stream to allow a byte to be read and then returned (i.e, "pushed back") to the stream. The PushbackInputStream class implements this idea. It provides a mechanism "peek" at what is coming from an input stream without disrupting it. It extends FilterInputStream.Fields: p
7 min read
Java.io.ObjectInputStream Class in Java | Set 1
ObjectInputStream Class deserializes the primitive data and objects previously written by ObjectOutputStream. Both ObjectOutputStream and ObjectInputStream are used as it provides storage for graphs of object.It ensures that the object it is working for, matches the classes of JVM i.e Java Virtual M
9 min read
Java.io.Printstream Class in Java | Set 1
A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Unlike other output streams, a PrintStream never throws an IOException; instead, exceptional situations merely set an internal flag that can be tested via the c
5 min read
Java.io.Printstream Class in Java | Set 2
Java.io.Printstream Class in Java | Set 1More Methods: PrintStream printf(Locale l, String format, Object... args) : A convenience method to write a formatted string to this output stream using the specified format string and arguments. Syntax :public PrintStream printf(Locale l, String format, Obje
6 min read
Java.io.ObjectInputStream Class in Java | Set 2
Java.io.ObjectInputStream Class in Java | Set 1 Note : Java codes mentioned in this article won't run on Online IDE as the file used in the code doesn't exists online. So, to verify the working of the codes, you can copy them to your System and can run it over there. More Methods of ObjectInputStrea
6 min read
Java.util.zip.ZipInputStream class in Java
This class implements an input stream filter for reading files in the ZIP file format. Includes support for both compressed and uncompressed entries. Constructors: ZipInputStream(InputStream in) : Creates a new ZIP input stream. ZipInputStream(InputStream in, Charset charset) : Creates a new ZIP inp
3 min read