Java.io.PipedInputStream class in Java Last Updated : 13 Oct, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 at the same time will create a deadlock for the threads. A pipe is said to be broken if a thread that was providing data bytes to the connected piped output stream is no longer alive. Declaration: public class PipedInputStream extends InputStream Constructor : PipedInputStream() : creates a PipedInputStream, that it is not connected. PipedInputStream(int pSize) : creates a PipedInputStream, that it is not connected with specified pipe size. PipedInputStream(PipedOutputStream outStream) : creates a PipedInputStream, that it is connected to PipedOutputStream - 'outStream'. PipedInputStream(PipedOutputStream outStream, int pSize) : creates a Piped Input Stream that is connected to Piped Output Stream with the specified pipe size. Methods: int read(): 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. Java // Java program illustrating the working of read() method 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 read() method : geek_output.write(71); System.out.println("using read() : " + (char)geek_input.read()); geek_output.write(69); System.out.println("using read() : " + (char)geek_input.read()); geek_output.write(75); System.out.println("using read() : " + (char)geek_input.read()); } catch (IOException except) { except.printStackTrace(); } } } Output : using read() : G using read() : E using read() : K read(byte[] buffer, int offset, int maxlen) : java.io.PipedInputStream.read(byte[] buffer, int offset, int maxlen) reads upto maxlen bytes of the data from Piped Input Stream to the array of buffers. The method blocks if end of Stream is reached or exception is thrown. Syntax : public int read(byte[] buffer, int offset, int maxlen) Parameters : buffer : the destination buffer into which the data is to be read offset : starting in the destination array - 'buffer'. maxlen : maximum length of array to be read Return : next 'maxlen' bytes of the data as an integer value return -1 is end of stream is reached Exception : -> IOException : if in case IO error occurs. -> NullPointerException : if buffer is null. -> IndexOutOfBoundsException : if offset is -ve or maxlen is -ve or maxlen > buffer.length - offset. receive(int byte) : java.io.PipedInputStream.receive(int byte) receives byte of the data. If no input is available, then the method blocks. Syntax : protected void receive(int byte) Parameters : byte : the bytes of the data received Return : void Exception : -> IOException : if in case IO error occurs or pipe is broken. close() : java.io.PipedInputStream.close() closes the Piped Input Stream and releases the allocated resources. Syntax : public void close() Parameters : -------------- Return : void Exception : -> IOException : if in case IO error occurs. connect(PipedOutputStream source) : java.io.PipedInputStream.connect(PipedOutputStream source) connects the Piped Input Stream to the 'source' Piped Output Stream and in case 'source' is pipes with some other stream, IO exception is thrown Syntax : public void connect(PipedOutputStream source) Parameters : source : the Piped Output Stream to be connected to Return : void Exception : -> IOException : if in case IO error occurs. available() : java.io.PipedInputStream.available() returns no. of bytes that can be read from Input Stream without actually being blocked. Syntax : public int available() Parameters : ------------- Return : no. of bytes that can be read from Input Stream without actually being blocked. 0, if the stream is already closed but by invoking close() method Exception : -> IOException : if in case IO error occurs. Java program explaining the working of PipedInputStream class methods : Java // Java program illustrating the working of PipedInputStream // connect(), read(byte[] buffer, int offset, int maxlen), // close(), available() 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); geek_output.write(71); geek_output.write(69); geek_output.write(69); geek_output.write(75); geek_output.write(83); // Use of available() : System.out.println("Use of available() : " + geek_input.available()); // Use of read(byte[] buffer, int offset, int maxlen) : byte[] buffer = new byte[5]; // destination 'buffer' geek_input.read(buffer, 0, 5); String str = new String(buffer); System.out.println("Using read(buffer, offset, maxlen) : " + str); // USe of close() method : System.out.println("Closing the stream"); geek_input.close(); } catch (IOException except) { except.printStackTrace(); } } } Output: Use of available() : 5 Using read(buffer, offset, maxlen) : GEEKS Closing the stream Next Article: Java.io.PipedOutputStream class in Java Comment More infoAdvertise with us Next Article Java.io.PipedInputStream class in Java M Mohit Gupta Improve Article Tags : Misc Java Java-I/O Practice Tags : JavaMisc Similar Reads Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e 7 min read Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me 15+ min read TCP/IP Model The TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While 7 min read Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac 15+ min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br 14 min read Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt 10 min read Like