
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - PipedInputStream read() method
Description
The Java PipedInputStream read() method reads the next byte of data from this piped input stream. The value byte is returned as an int in the range 0 to 255. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
Declaration
Following is the declaration for java.io.PipedInputStream.read() method.
public int read()
Parameters
NA
Return Value
This method returns the next byte of data, or -1 if the end of the stream is reached.
Exception
IOException − If an I/O error occurs.
Example - Usage of PipedInputStream read() method
The following example shows the usage of PipedInputStream read() method.
PipedInputStreamDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; public class PipedInputStreamDemo { public static void main(String[] args) { // create a new Piped input and Output Stream PipedOutputStream out = new PipedOutputStream(); PipedInputStream in = new PipedInputStream(); try { // connect input and output in.connect(out); // write something out.write(70); out.write(71); // read what we wrote for (int i = 0; i < 2; i++) { System.out.println("" + (char) in.read()); } } catch (IOException ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
F G
Example - Using read() to read one byte at a time from a piped stream
The following example shows the usage of PipedInputStream read() method.
PipedInputStreamDemo.java
package com.tutorialspoint; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.IOException; public class PipedInputStreamDemo { public static void main(String[] args) { try { PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pis = new PipedInputStream(); pis.connect(pos); // Write a string into the pipe pos.write("ABC".getBytes()); // Read one byte at a time int data; while ((data = pis.read()) != -1) { System.out.print((char) data); // Converts byte to char } pos.close(); pis.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
ABC
Explanation
read() reads one byte at a time from the piped input stream.
It returns the byte value (0−255) or -1 if the end of the stream is reached.
In this example, it reads "A", "B", and "C" byte by byte from the pipe.
Example - Reading bytes sent by another thread using read()
The following example shows the usage of PipedInputStream read() method.
PipedInputStreamDemo.java
package com.tutorialspoint; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.IOException; public class PipedInputStreamDemo { public static void main(String[] args) throws IOException { PipedInputStream pis = new PipedInputStream(); PipedOutputStream pos = new PipedOutputStream(pis); // connect via constructor Thread producer = new Thread(() -> { try { pos.write("Hello from thread!".getBytes()); pos.close(); } catch (IOException e) { e.printStackTrace(); } }); Thread consumer = new Thread(() -> { try { int ch; while ((ch = pis.read()) != -1) { System.out.print((char) ch); } pis.close(); } catch (IOException e) { e.printStackTrace(); } }); producer.start(); consumer.start(); } }
Output
Let us compile and run the above program, this will produce the following result−
Hello from thread!
Explanation
This example uses threads to simulate real-time communication.
The producer thread writes data to the pipe.
The consumer thread reads it one byte at a time using read().
read() blocks until data is available or the stream is closed.