Home » Java programming language

Java ObjectInputStream registerValidation() Method with Example

ObjectInputStream Class registerValidation() method: Here, we are going to learn about the registerValidation() method of ObjectInputStream Class with its syntax and example.
Submitted by Preeti Jain, on April 05, 2020

ObjectInputStream Class registerValidation() method

  • registerValidation() method is available in java.io package.
  • registerValidation() method is used to register an object (ob) to be validated before the graph is returned.
  • registerValidation() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • registerValidation() method may throw an exception at the time of registering an object to validate.
    • NotActiveException: This exception may throw when the given parameters are not allowed to enable.
    • InvalidObjectException: This exception may throw when the given parameter is not valid.

Syntax:

    public void registerValidation(ObjectInputValiadation ob, int priority);

Parameter(s):

  • ObjectInputValiadation ob – represents the object to get the validation callback.
  • int priority – represents the priority by which based on ordering of callback.

Return value:

The return type of the method is void, it returns nothing.

Example:

// Java program to demonstrate the example 
// of void registerValidation(ObjectInputValiadation ob, 
// int priority) method of ObjectInputStream


import java.io.*;

public class RegisterValidation {
 public static void main(String[] args) {
  try {
   // Instantiates ObjectOutputStream , ObjectInputStream 
   // FileInputStream and FileOutputStream
   FileOutputStream file_out_stm = new FileOutputStream("D:\\includehelp.txt");
   Drain obj_out_stm = new Drain(file_out_stm);
   FileInputStream file_in_stm = new FileInputStream("D:\\includehelp.txt");
   ObjectInputStream obj_in_stm = new ObjectInputStream(file_in_stm);

   obj_out_stm.writeObject(new ReadObject());

   // By using readObject() method is to read
   ReadObject a = (ReadObject) obj_in_stm.readObject();

   // Testing Validations
   if (ReadObject.str.equals("Java Programmer")) {
    System.out.println("Validations Successful!!!");
   } else {
    System.out.println("Invalid Validations!!! ");
   }
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }

 static class ReadObject implements Serializable, ObjectInputValidation {
  static String str = "Java World!!!";
  private String readObject(ObjectInputStream ois)
  throws IOException, ClassNotFoundException {
   // By using readFields() method is to get fields 
   ObjectInputStream.GetField get_f = ois.readFields();

   ois.registerValidation(this, 0);

   // Return str
   return (String) get_f.get("str", null);
  }

  @Override
  public void validateObject() throws InvalidObjectException {
   throw new UnsupportedOperationException("Unsupported Validations!!!");
  }
 }
}

Output

Invalid Validations!!!






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.