?? main.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 + -