Java - LineNumberInputStream available() method



Description

The Java LineNumberInputStream available() method returns the estimated number of bytes that can be read without blocking. Commonly used before calling read() to check if data is available.

Declaration

Following is the declaration for java.io.LineNumberInputStream.available() method −

public int available()

Parameters

NA

Return Value

The method returns the number of bytes that can be read from this input stream without blocking.

Exception

  • IOException − If an I/O error occurs.

Example - Usage of LineNumberInputStream available() method

The following example shows the usage of Java LineNumberInputStream available() method.

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) throws IOException {
      LineNumberInputStream lnis = null;
      FileInputStream fis = null;
      int i,j;
      char c;
      
      try {
         // create new input streams
         fis = new FileInputStream("test.txt");
         lnis = new LineNumberInputStream(fis);
         
         while((i = lnis.read())!=-1) {
         
            // converts int to char
            c = (char)i;
            
            // prints char
            System.out.println("Character read: "+c);
            
            j = lnis.available();
            
            System.out.println("Available bytes: "+j);
         }
         
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      } finally {
         // closes the stream and releases any system resources
         if(fis!=null)
            fis.close();
         if(lnis!=null)
            lnis.close();     
      }
   }
}

Output(Assuming test.txt contains "ABCDE")

Let us compile and run the above program, this will produce the following result−

Character read: A
Available bytes: 2
Character read: B
Available bytes: 1
Character read: C
Available bytes: 1
Character read: D
Available bytes: 0
Character read: E
Available bytes: 0

Example - Checking Available Bytes Before Reading

The following example shows the usage of Java LineNumberInputStream available() method.

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) {
      try (LineNumberInputStream lineNumberInputStream = new LineNumberInputStream(new FileInputStream("example.txt"))) {
         System.out.println("Bytes available: " + lineNumberInputStream.available());

         // Read the first byte
         int data = lineNumberInputStream.read();
         System.out.println("First byte read: " + (char) data);

         // Check available bytes again
         System.out.println("Bytes available after reading one byte: " + lineNumberInputStream.available());

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Hello")

Let us compile and run the above program, this will produce the following result−

Bytes available: 5
First byte read: H
Bytes available after reading one byte: 4

Explanation

  • Uses LineNumberInputStream to read from "example.txt".

  • Calls available() before reading, showing the total available bytes.

  • Reads one byte, then calls available() again to show the remaining unread bytes.

Example - Checking Available Bytes in a Loop

The following example shows the usage of Java LineNumberInputStream available() method.

LineNumberInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.LineNumberInputStream;

public class LineNumberInputStreamDemo {
   public static void main(String[] args) {
      try (LineNumberInputStream lineNumberInputStream = new LineNumberInputStream(new FileInputStream("example.txt"))) {
         int availableBytes;
         while ((availableBytes = lineNumberInputStream.available()) > 0) {
            System.out.println("Bytes available: " + availableBytes);
            System.out.println("Reading: " + (char) lineNumberInputStream.read());
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(if example.txt contains "Java")

Let us compile and run the above program, this will produce the following result−

Bytes available: 4
Reading: J
Bytes available: 3
Reading: a
Bytes available: 2
Reading: v
Bytes available: 1
Reading: a

Explanation

  • Uses LineNumberInputStream to read "example.txt".

  • Calls available() in a loop before reading each character.

  • Prints the number of available bytes before each read.

java_io_linenumberinputstream.htm
Advertisements