Java - ObjectOutputStream.PutField put(String name, byte val) method



Description

The Java ObjectOutputStream.PutField put(String name, byte val) method puts the value of the named byte field into the persistent field.

  • This method is used in a class's custom writeObject(ObjectOutputStream oos) method.

  • It allows you to manually assign a byte value to a specific field.

  • After calling put(), you must call oos.writeFields() to actually write the fields to the stream.

Declaration

Following is the declaration for java.io.ObjectOutputStream.PutField.put(String name, byte val) method.

public abstract void put(String name, byte val)

Parameters

  • name − The name of the serializable field.

  • val − The value to assign to the field.

Return Value

This method does not return a value.

Exception

  • IllegalArgumentException − If name does not match the name of a serializable field for the class whose fields are being written, or if the type of the named field is not byte.

Example - Usage of ObjectOutputStream.PutField put(String name, byte val) method

The following example shows the usage of ObjectOutputStream.PutField put(String name, byte val) method.

ObjectOutputStreamDemo.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 ObjectOutputStreamDemo 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 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 - Serialize a Device object with a manually assigned status byte

The following example shows the usage of ObjectOutputStream.PutField put(String name, byte val) method. We're using put() to write a byte field named status.

ObjectOutputStreamDemo.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 ObjectOutputStreamDemo {
   public static void main(String[] args) throws IOException, ClassNotFoundException {
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("device1.ser"))) {
         Device d = new Device("Sensor", (byte) 1);
         oos.writeObject(d);

         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("device1.ser"));
         Device d1 = (Device)ois.readObject();
         System.out.println(" Device name = " + d1.name + " ; status = " + d1.status);
      }
   }

   static class Device implements Serializable {
      private static final long serialVersionUID = 1L;

      String name;
      byte status;  // 0 = off, 1 = on

      public Device(String name, byte status) {
         this.name = name;
         this.status = status;
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         fields.put("name", name);
         fields.put("status", status);  // put byte value
         oos.writeFields();
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         name = (String) fields.get("name", "Unknown");
         status = (byte) fields.get("status", (byte) 0);
      }
   }
}

Output

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

Device name = Sensor ; status = 1

Explanation

  • The put() call writes the byte field status.

  • On deserialization, it's read back with a default fallback.

Example - Conditionally set a byte priority field (custom logic)

The following example shows the usage of ObjectOutputStream.PutField put(String name, byte val) method. We're serializing a Task object and assign a custom byte priority during serialization (not from the original field).

ObjectOutputStreamDemo.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 ObjectOutputStreamDemo {
   public static void main(String[] args) throws IOException, ClassNotFoundException {
      try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("task2.ser"))) {
         Task task = new Task("Email cleanup", false); // not important
         oos.writeObject(task);

         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("task2.ser"));
         Task t1 = (Task)ois.readObject();
         System.out.println(" Task::  description = '" + t1.description + "' priority = " + t1.priority);
      }
   }

   static class Task implements Serializable {
      private static final long serialVersionUID = 1L;

      String description;
      transient boolean important;  // not serialized directly
      byte priority;                // this will be written manually

      public Task(String description, boolean important) {
         this.description = description;
         this.important = important;
      }

      private void writeObject(ObjectOutputStream oos) throws IOException {
         ObjectOutputStream.PutField fields = oos.putFields();
         fields.put("description", description);
         fields.put("priority", (byte) (important ? 10 : 1));  // derived byte
         oos.writeFields();
      }

      private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
         ObjectInputStream.GetField fields = ois.readFields();
         description = (String) fields.get("description", "");
         priority = (byte) fields.get("priority", (byte) 0);
      }
   }
}

Output

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

Task::  description = 'Email cleanup' priority = 1

Explanation

  • The priority field is calculated at serialization time.

  • This allows us to serialize derived byte values while skipping transient fields like important.

java_io_objectoutputstream.putfield.htm
Advertisements