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.

java_io_pipedinputstream.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy