
- 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 available() method
Description
The Java PipedInputStream available() method returns the number of bytes that can be read from this input stream without blocking.
Declaration
Following is the declaration for java.io.PipedInputStream.available() method.
public int available()
Parameters
NA
Return Value
This method returns the number of bytes that can be read from this input stream without blocking, or 0 if this input stream has been closed by invoking its close() method, or if the pipe is unconnected, or broken.
Exception
IOException − If an I/O error occurs.
Example - Usage of PipedInputStream available() method
The following example shows the usage of PipedInputStream available() 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); // print how many bytes are available System.out.println("" + in.available()); // 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 −
2 F G
Example - Checking available bytes after writing data to a pipe
The following example shows the usage of PipedInputStream available() 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(pos); pos.write("Hello".getBytes()); // Check how many bytes are available to read int availableBytes = pis.available(); System.out.println("Available bytes: " + availableBytes); // Should print 5 // Read the data byte[] buffer = new byte[availableBytes]; pis.read(buffer); System.out.println("Data read: " + new String(buffer)); pos.close(); pis.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Available bytes: 5 Data read: Hello
Explanation
available() returns the number of bytes that can be read without blocking.
After writing "Hello" (5 bytes), available() returns 5.
This is useful when you want to know how much data is ready for immediate reading.
Example - Using available() in a producer-consumer thread scenario
The following example shows the usage of PipedInputStream available() 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); Thread producer = new Thread(() -> { try { pos.write("Data from producer".getBytes()); pos.close(); } catch (IOException e) { e.printStackTrace(); } }); Thread consumer = new Thread(() -> { try { // Wait briefly to let producer write Thread.sleep(100); int available = pis.available(); System.out.println("Consumer sees " + available + " bytes available."); byte[] data = new byte[available]; pis.read(data); System.out.println("Consumer read: " + new String(data)); pis.close(); } catch (Exception e) { e.printStackTrace(); } }); producer.start(); consumer.start(); } }
Output
Let us compile and run the above program, this will produce the following result−
Consumer sees 18 bytes available. Consumer read: Data from producer
Explanation
In this multi-threaded example, one thread writes data, and the other checks available() and reads it.
available() helps the consumer know how much data is ready without blocking.
This method is handy for synchronizing stream reading in producer-consumer scenarios.