Java - ObjectInputStream readFully(byte[] buf) method



Description

The Java ObjectInputStream readFully(byte[] buf) method reads bytes into the given byte array (buf) until the array is completely filled. Unlike read(byte[] buf), which may return before filling the buffer, readFully(byte[] buf) blocks until all bytes are read.

Declaration

Following is the declaration for java.io.ObjectInputStream.readFully(byte[] buf) method −

public void readFully(byte[] buf)

Parameters

buf − The buffer into which the data is read.

Return Value

This method does not return a value.

Exception

  • EOFException − If end of file is reached.

  • IOException − If an I/O error has occurred.

Example - Usage of ObjectInputStream readFully(byte[] buf) method

The following example shows the usage of Java ObjectInputStream readFully(byte[] buf) 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) {
      String s = "Hello World";
      
      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(s);
         oout.flush();

         // create an ObjectInputStream for the file we created before
         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

         // read and print the whole content
         byte[] b = new byte[13];
         ois.readFully(b);
         String array = new String(b);
         System.out.println("" + array);
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

Hello World

Example - Reading a Fixed-Size Byte Array

The following example shows the usage of Java ObjectInputStream readFully(byte[] buf) method. This example writes a fixed-size byte array to a file and then reads it back using readFully(byte[] buf).

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.*;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Writing a byte array to a file
         FileOutputStream fos = new FileOutputStream("byte_data.dat");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         byte[] data = {65, 66, 67, 68, 69}; // 'A', 'B', 'C', 'D', 'E'
         oos.write(data);
         oos.close();

         // Reading byte array using ObjectInputStream
         FileInputStream fis = new FileInputStream("byte_data.dat");
         ObjectInputStream ois = new ObjectInputStream(fis);

         byte[] buffer = new byte[5]; // Buffer of fixed size
         ois.readFully(buffer); // Ensures the entire buffer is filled

         System.out.println("Read Data: " + new String(buffer)); // Convert bytes to string

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

Output

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

Read Data: ABCDE

Explanation

  • Writes the byte array {65, 66, 67, 68, 69} ("ABCDE") to a file.

  • Creates a buffer of size 5 and calls readFully(buffer).

  • Reads exactly 5 bytes into the buffer and converts them to a string.

  • Ensures all bytes are read before continuing.

Example - Handling an Insufficient Input Stream

The following example shows the usage of Java ObjectInputStream readFully(byte[] buf) method. This example demonstrates what happens when readFully(byte[] buf) is called on a stream with fewer bytes than expected.

ObjectInputStreamDemo.java

package com.tutorialspoint;

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Writing a smaller byte array than expected
         FileOutputStream fos = new FileOutputStream("small_data.dat");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         byte[] data = {72, 105}; // 'H', 'i' (only 2 bytes)
         oos.write(data);
         oos.close();

         // Reading with a larger buffer than available data
         FileInputStream fis = new FileInputStream("small_data.dat");
         ObjectInputStream ois = new ObjectInputStream(fis);

         byte[] buffer = new byte[5]; // Buffer expecting 5 bytes

         // This will throw EOFException because only 2 bytes are available
         ois.readFully(buffer);

         System.out.println("Read Data: " + new String(buffer));

         ois.close();
      } catch (EOFException e) {
         System.out.println("Exception: Not enough data to fill the buffer!");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Exception: Not enough data to fill the buffer!

Explanation

  • Writes only 2 bytes ("Hi") to a file.

  • Attempts to read 5 bytes using readFully(buffer), but only 2 are available.

  • Throws EOFException, as readFully() expects the buffer to be completely filled.

java_io_objectinputstream.htm
Advertisements