ObjectInputStream registerValidation() Method in Java

In this tutorial, you will learn how to use the registerValidation() method in Java.

First, you should know about the ObjectInputStream class which contains registerValidation() method before we go forward.

ObjectInputStream in Java is used to recover those objects previously serialized using an ObjectOutputStream. Another use is to pass objects between hosts using a socket stream for remote communication. Using with FileOutputStream and FileInputStream respectively allows ObjectOutputStream and ObjectInputStream to store graphs of objects.

java.io package contains ObjectInputStream class which further contains the registerValidation() method. Firstly import this package. This method registers an object to be checked before the graph is returned back. This is a nonstatic method. Therefore class object is used to access this method. It functions like resolveObject().

NOTE: This method creates files on the hard disk. As a result, This method doesn’t work on an online IDE.

registerValidation() Syntax

Below is the syntax for this method:

public void registerValidation(ObjectInputValiadation object, int order);

In the above syntax, you can see two parameters. They are:

  • ObjectInputValiadation object: Here you send an object for which you need validation.
  • int order: Object priority in the list.

Return type for registerValidation()

It returns nothing. Therefore its return type is void

Exceptions

  • NotActiveException
  • InvalidObjectException

Example

See the example code below:

import java.io.*;   
 class Demo extends ObjectInputStream   
{   
    public Demo(InputStream in) throws IOException   
    {   
        super(in);   
    }   
}   
  public class Example  
{   
    public static void main(String[] args)   
    {   
        String str1 = "Welcome";   
        try  
        {   
            FileOutputStream out = new FileOutputStream("Codespeedy.txt");   
            ObjectOutputStream in = new ObjectOutputStream(out);   
            ObjectInputStream objIn  = new ObjectInputStream(new FileInputStream("codespeedy.txt"));   
            in.writeObject(new Check());   
            in.writeUTF(str1);   
            in.flush();   
            Check obj = (Check) objIn.readObject();   
            System.out.println("read value : " + obj.str2);   
    
            System.out.println("Checking");   
            if (Check.str2.equals("Codespeedy"))  
            {   
                System.out.println("Codespeedy");   
            }   
            else  
            {   
                System.out.println("Something wrong ");   
            }   
        }   
    
        catch (Exception excpt)   
        {   
            System.out.println("Error");   
            excpt.printStackTrace();   
        }   
    }   
    
    static class Check implements Serializable, ObjectInputValidation   
    {   
        static String str2 = "Codespeedy";   
        private String readObject(ObjectInputStream in)   
        throws IOException, ClassNotFoundException   
        {   
    
            ObjectInputStream.GetField getfield = in.readFields();   
    
            in.registerValidation(this, 0);   
              return (String) getfield.get("str2", null);   
        }   
    
        @Override  
        public void validateObject() throws InvalidObjectException   
        {   
            throw new UnsupportedOperationException("Not supported yet.");   
        }   
    }   
}

And after we run the above Java code, we can see the results just like given below:

read value : Codespeedy
Checking
Codespeedy

 

Also read:

Leave a Reply