Java - ObjectInputStream.GetField get(String name, byte val) method



Description

The Java ObjectInputStream.getField get(String name, byte val) method gets the value of the named byte field from the persistent field.

Declaration

Following is the declaration for java.io.ObjectInputStream.getField.get(String name, byte val) method.

public abstract boolean get(String name, byte val)

Parameters

  • name − The name of the field.

  • val − The default value to use if name does not have a value.

Return Value

This method returns the value of the named byte field.

Exception

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

  • IllegalArgumentException − If type of name is not serializable or if the field type is incorrect.

Example - Usage of ObjectInputStream.getField get(String name, byte val) method

The following example shows the usage of ObjectInputStream.getField get(String name, byte val) method.

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.ObjectStreamField;
import java.io.Serializable;

public class ObjectInputStreamDemo implements Serializable {
   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.writeObject(new Example());
         oout.flush();
         oout.close();

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

         // read an object from the stream and cast it to Example
         Example a = (Example) ois.readObject();

         // print var variable of a
         System.out.println("" + a.var);
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }

   static public class Example implements Serializable {
      static byte var = 5;
      
      // assign a new serialPersistentFields 
      private static final ObjectStreamField[] serialPersistentFields = {
         new ObjectStreamField("var", Byte.TYPE)
      };

      private void readObject(ObjectInputStream in)
         throws IOException, ClassNotFoundException {

         // get the field and assign it at string variable
         ObjectInputStream.GetField fields = in.readFields();

         // check if string is defaulted, meaning if it has no value
         byte b = 0;
         var = (byte) fields.get("var", b);
      }

      private void writeObject(ObjectOutputStream out) throws IOException {

         // write into the ObjectStreamField array the variable var
         ObjectOutputStream.PutField fields = out.putFields();
         fields.put("var", var);
         out.writeFields();
      }
   }
}

Output

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

5

Example - Read a Serialized byte Field Normally

The following example shows the usage of ObjectInputStream.getField get(String name, byte val) method. This example shows how to read a byte field that was serialized.

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;

public class ObjectInputStreamDemo {
   public static void main(String[] args) throws Exception {
      String filename = "byte1.ser";

      // Serialize
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         Data data = new Data((byte) 42);
         oos.writeObject(data);
      }

      // Deserialize
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         Data d = (Data) ois.readObject();
      }
   }

   static class Data implements Serializable {
      private static final long serialVersionUID = 1L;
      byte number;

      Data(byte number) {
         this.number = number;
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         byte num = fields.get("number", (byte) 0);
         System.out.println("Deserialized number: " + num);
      }
   }
}

Output

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

Deserialized number: 42

Explanation

  • The get("number", (byte) 0) retrieves the stored value 42.

  • The second argument ((byte) 0) is only used if the field is missing.

Example - Field Was Not Serialized — get() Returns Default Value

The following example shows the usage of ObjectInputStream.getField get(String name, byte val) method. In this example, we intentionally skip writing the byte field during serialization, so get() returns the default (0).

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;

public class ObjectInputStreamDemo {
   public static void main(String[] args) throws Exception {
      String filename = "byte2.ser";

      // Serialize without writing 'number' field
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
         oos.writeObject(new Data((byte) 42)); // The number field exists but isn't written
      }

      // Deserialize
      try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
         Data d = (Data) ois.readObject();
      }
   }

   static class Data implements Serializable {
      private static final long serialVersionUID = 1L;
      byte number;

      Data(byte number) {
         this.number = number;
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         // Do NOT write the 'number' field
         oos.writeFields();
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         byte num = fields.get("number", (byte) 0); // Should fallback to default 0
         System.out.println("Deserialized number (defaulted): " + num);
      }
   }
}

Output

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

Deserialized number (defaulted): 0

Explanation

  • Since number wasn't written, fields.get("number", (byte) 0) falls back to 0.

  • This simulates handling removed fields or missing data gracefully.

java_io_objectinputstream.getfield.htm
Advertisements