
- 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 - 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.