Java - ObjectInputStream readFully(byte[] buf, int off, int len) method



Description

The Java ObjectInputStream readFully(byte[] buf, int off, int len) method reads exactly len bytes into the byte array b, starting at index off. Unlike read(byte[] b, int off, int len), which may return before filling the requested bytes, readFully() blocks until all bytes are read or throws an EOFException if not enough data is available.

Declaration

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

public void readFully(byte[] buf, int off, int len)

Parameters

  • buf − The buffer into which the data is read.

  • off − The start offset of the data.

  • len − The maximum number of bytes to 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, int off, int len) method

The following example shows the usage of Java ObjectInputStream readFully(byte[] buf, int off, int len) 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 from index 0 to 7
         byte[] b = new byte[13];
         ois.readFully(b, 0, 7);
         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

Example - Reading Part of a Byte Array

The following example shows the usage of Java ObjectInputStream readFully(byte[] buf, int off, int len) method. This example writes a byte array to a file and reads only a subset of it using readFully(byte[] b, int off, int len).

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;

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 part of the byte array using ObjectInputStream
         FileInputStream fis = new FileInputStream("byte_data.dat");
         ObjectInputStream ois = new ObjectInputStream(fis);

         byte[] buffer = new byte[10]; // Larger buffer
         ois.readFully(buffer, 2, 3); // Read 3 bytes into buffer starting at index 2

         System.out.print("Read Data: ");
         for (int i = 0; i < buffer.length; i++) {
            System.out.print((char) buffer[i] + " ");
         }

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

Output

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

Read Data: A B C

Explanation

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

  • Creates a buffer of size 10, but only reads 3 bytes starting at offset 2.

  • Fills positions 2, 3, and 4 in the buffer with 'A', 'B', 'C' while leaving other bytes as 0 (null characters).

Example - Handling Insufficient Data in the Stream

The following example shows the usage of Java ObjectInputStream readFully(byte[] buf, int off, int len) method. This example demonstrates what happens when readFully(b, off, len) requests more bytes than available.

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 only 3 bytes instead of expected 5
         FileOutputStream fos = new FileOutputStream("small_data.dat");
         ObjectOutputStream oos = new ObjectOutputStream(fos);
         byte[] data = {88, 89, 90}; // 'X', 'Y', 'Z'
         oos.write(data);
         oos.close();

         // Reading 5 bytes when only 3 are available
         FileInputStream fis = new FileInputStream("small_data.dat");
         ObjectInputStream ois = new ObjectInputStream(fis);

         byte[] buffer = new byte[10];

         // This will throw EOFException because only 3 bytes exist, but we are requesting 5
         ois.readFully(buffer, 2, 5);

         System.out.print("Read Data: ");
         for (int i = 2; i < 7; i++) {
            System.out.print((char) buffer[i] + " ");
         }

         ois.close();
      } catch (EOFException e) {
         System.out.println("Exception: Not enough data to fill the requested bytes!");
      } 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 requested bytes!

Explanation

  • Writes only 3 bytes ('X', 'Y', 'Z') to the file.

  • Attempts to read 5 bytes, which is more than available.

  • Throws EOFException because readFully() requires exactly len bytes and cannot return early.

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