当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Java ObjectInputStream registerValidation()用法及代码示例

ObjectInputStream 类方法的 registerValidation() 方法在返回图形之前注册要验证的对象。虽然此方法与 resolveObject 类似,但在重建整个图后调用这些验证。

用法

public void registerValidation(ObjectInputValidation obj, int prio) throws NotActiveException,  InvalidObjectException

参数

Obj:作为接收验证回调的对象。

prio:as 控制回调的顺序

返回

将 void 作为返回类型。

抛出

非活动异常

无效对象异常

例子1

import java.io.*; 
 class ObjectInputStreamDemo2 extends ObjectInputStream 
{ 
    public ObjectInputStreamDemo2(InputStream instream) throws IOException 
    { 
        super(instream); 
    } 
} 
  public class objectInputStreamRegisterValidationExample1
{ 
    public static void main(String[] args) 
    { 
        String str1 = "javaTpoint"; 
        try
        { 
            FileOutputStream outstream = new FileOutputStream("file1.txt"); 
            ObjectOutputStream instream = new ObjectOutputStream(outstream); 
            ObjectInputStream objInStream  = new ObjectInputStream(new FileInputStream("file1.txt")); 
            instream.writeObject(new Test1()); 
            instream.writeUTF(str1); 
            instream.flush(); 
            Test1 obj = (Test1) objInStream.readObject(); 
            System.out.println("Using readFields():" + obj.str2); 
  
            System.out.println("Valiations:::"); 
            if (Test1.str2.equals("Welcome to javaTpoint"))
            { 
                System.out.println("Welcome to javaTpoint"); 
            } 
            else
            { 
                System.out.println("Something wrong with the validations:"); 
            } 
        } 
  
        catch (Exception excpt) 
        { 
            System.out.println("Error"); 
            excpt.printStackTrace(); 
        } 
    } 
  
    static class Test1 implements Serializable, ObjectInputValidation 
    { 
        static String str2 = "Welcome to javaTpoint"; 
        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."); 
        } 
    } 
}

输出:

Using readFields():Welcome to javaTpoint
Valiations:::
Welcome to javaTpoint

例子2

import java.io.*; 
  
 class ObjectInputStreamDemo3 extends ObjectInputStream 
{ 
    public ObjectInputStreamDemo3(InputStream instream) throws IOException 
    { 
        super(instream); 
    } 
} 
  public class objectInputStreamRegisterValidationExample2
{ 
    public static void main(String[] args) 
    { 
      
        try
        { 
         
            FileOutputStream outstream = new FileOutputStream("file1.txt"); 
            ObjectOutputStream instream = new ObjectOutputStream(outstream); 
  
            ObjectInputStream objInStream  = new ObjectInputStream(new FileInputStream("file1.txt")); 
  
            instream.writeObject(new Test1()); 
            instream.flush(); 
  
            Test1 obj = (Test1) objInStream.readObject(); 

            if (Test1.str2.equals("Welcome to javaTpoint"))
            { 
                System.out.println("JavaTpoint"); 
            }     
        } 
  
        catch (Exception excpt) 
        { 
            System.out.println("Error"); 
            excpt.printStackTrace(); 
        } 
    } 
  
    static class Test1 implements Serializable, ObjectInputValidation 
    { 
        static String str2 = "Welcome to javaTpoint"; 
        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."); 
        } 
    } 
}

输出:

JavaTpoint




相关用法


注:本文由纯净天空筛选整理自 Java ObjectInputStream registerValidation() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。