⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.java

📁 《精通RMI:Java与EJB企业级应用开发》书籍的源代码. 本书是讲述RMI技术的经典著作
💻 JAVA
字号:
/*
 * Copyright 1999 by dreamBean Software,
 * All rights reserved.
 */
package masteringrmi.marshaling;

import java.io.*;

/**
 *   Showcase marshalling semantics
 *      
 *   @author Rickard 謆erg (rickard@dreambean.com)
 *   @version $Revision:$
 */
public class Main
{
   // Static --------------------------------------------------------
   public static void main(String[] args)
      throws Exception
   {
      new Main().run();
   }
   
   // Public  -------------------------------------------------------
   public void run()
   {
      try
      {
         // Create streams to serialize object
         ByteArrayOutputStream bout = new ByteArrayOutputStream();
         ObjectOutputStream out = new ObjectOutputStream(bout);
         
         // Create some objects and change some values
         A object = new A();
         object.foo = 11;
         object.bar = "Goodbye World...";
         object.baz = new B();
         object.baz.lastRead = -1;
         
         // Serialize the object
         out.writeObject(object);
         
         // Create streams to deserialize object
         ByteArrayInputStream bin = 
            new ByteArrayInputStream(bout.toByteArray());
         ObjectInputStream in = new ObjectInputStream(bin);
         
         // Deserialize the object
         object = (A)in.readObject();
         
         // Test object values
         System.out.println("11="+object.foo); // Should print 11
         System.out.println("null="+object.bar); // Should print null
         System.out.println("1="+object.baz.serializationCount); // Should print 1
         
      } catch (Exception e)
      {
         e.printStackTrace();
      }
   }
    
   // Inner classes -------------------------------------------------
   static class A
      implements java.io.Serializable // A is hence serializable
   {
      // Static values are not serialized
      static long someValue = 1; 
      
      // This attribute is serialized
      int foo = 10; 
      
      // Transient attributes are not serialized
      transient String bar = "Hello World!"; 
      
      // If baz points to a B instance,that object is also serialized
      B baz;
   }
   
   static class B
      extends A // Since A is serializable, so is B
   {
      // This is transient -> not serialized
      transient long lastRead; 
      int serializationCount = 0; 
      
      private void readObject(ObjectInputStream in)
         throws IOException, ClassNotFoundException
      {
         // First call the default algorithm in order
         // to restore the correct value of serializationCount
         in.defaultReadObject();
         
         // Increase the count
         serializationCount++;
         
         // Set the transient value
         lastRead = System.currentTimeMillis();
      }
   }
      
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -