ObjectInputStream类registerValidation()方法 (ObjectInputStream Class registerValidation() method)
registerValidation() method is available in java.io package.
registerValidation()方法在java.io包中可用。
registerValidation() method is used to register an object (ob) to be validated before the graph is returned.
registerValidation()方法用于在返回图形之前注册要验证的对象(ob)。
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()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
registerValidation() method may throw an exception at the time of registering an object to validate.
registerValidation()方法在注册要验证的对象时可能会引发异常。
- NotActiveException: This exception may throw when the given parameters are not allowed to enable.NotActiveException :如果不允许启用给定参数,则可能引发此异常。
- InvalidObjectException: This exception may throw when the given parameter is not valid.InvalidObjectException :当给定参数无效时,可能引发此异常。
Syntax:
句法:
public void registerValidation(ObjectInputValiadation ob, int priority);
Parameter(s):
参数:
ObjectInputValiadation ob – represents the object to get the validation callback.
ObjectInputValiadation ob –表示要获取验证回调的对象。
int priority – represents the priority by which based on ordering of callback.
int priority –表示基于回调顺序的优先级。
Return value:
返回值:
The return type of the method is void, it returns nothing.
该方法的返回类型为void ,不返回任何内容。
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!!!
翻译自: https://www.includehelp.com/java/objectinputstream-registervalidation-method-with-example.aspx