Java - ObjectInputStream available() method



Description

The Java ObjectInputStream available() method returns the number of bytes that can be read without blocking. It helps determine how much data is left in the stream but does not guarantee the total remaining size of the object.

Declaration

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

 public int available() 

Parameters

NA

Return Value

This method returns the number of available bytes.

Exception

IOException − If there are I/O errors while reading from the underlying InputStream.

Example - Usage of ObjectInputStream available() method

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

ObjectInputStreamDemo.java

 package com.tutorialspoint; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectInputStreamDemo { public static void main(String[] args) { try { // create a new file with an ObjectOutputStream FileOutputStream out = new FileOutputStream("test.txt"); ObjectOutputStream oout = new ObjectOutputStream(out); // write something in the file oout.writeUTF("Hello World"); oout.flush(); // create an ObjectInputStream for the file we created before ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt")); // check how many bytes are available System.out.println("" + ois.available()); } catch (Exception ex) { ex.printStackTrace(); } } } 

Output

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

 13 

Example - Checking Available Bytes Before and After Reading an Object

The following example shows the usage of Java ObjectInputStream available() method. This example writes an object to a file, reads it using ObjectInputStream, and checks available bytes before and after reading.

ObjectInputStreamDemo.java

 package com.tutorialspoint; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class Person implements Serializable { private static final long serialVersionUID = 1L; String name; int age; public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person{name='" + name + "', age=" + age + "}"; } } public class ObjectInputStreamDemo { public static void main(String[] args) { try { // Serialize object to file ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("person.dat")); oos.writeObject(new Person("Alice", 30)); oos.close(); // Read object from file ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.dat")); System.out.println("Bytes available before reading: " + ois.available()); Person person = (Person) ois.readObject(); System.out.println("Read Object: " + person); System.out.println("Bytes available after reading: " + ois.available()); ois.close(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } 

Output

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

 Bytes available before reading: 0 Read Object: Person{name='Alice', age=30} Bytes available after reading: 0 

Explanation

  • Serializes an object (Person) to a file (person.dat).

  • Before reading, available() returns 0 because ObjectInputStream does not support determining object size.

  • Reads the object using readObject().

  • After reading, available() still returns 0 because it only works reliably for FileInputStream, not deserialized objects.

Example - Checking Available Bytes in a ByteArray Stream

The following example shows the usage of Java ObjectInputStream available() method. This example writes an object to a ByteArrayOutputStream, then reads it using ObjectInputStream and checks available bytes.

ObjectInputStreamDemo.java

 package com.tutorialspoint; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ObjectInputStreamDemo { public static void main(String[] args) { try { // Serialize object to a byte array ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject("Hello, World!"); oos.close(); // Convert byte array to input stream ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); System.out.println("Bytes available before reading: " + ois.available()); String message = (String) ois.readObject(); System.out.println("Read Object: " + message); System.out.println("Bytes available after reading: " + ois.available()); ois.close(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } 

Output

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

 Bytes available before reading: 0 Read Object: Hello, World! Bytes available after reading: 0 

Explanation

  • Writes a String object to a ByteArrayOutputStream.

  • Before reading, available() shows the number of bytes in the buffer.

  • Reads the string using readObject().

  • After reading, available() returns 0, as all bytes have been consumed.

java_io_objectinputstream.htm
Advertisements