KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > dnd > SerializationTester


1 /*
2  * @(#)SerializationTester.java 1.5 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.dnd;
9
10 import java.io.OutputStream JavaDoc;
11 import java.io.ObjectOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.Serializable JavaDoc;
14
15 /**
16  * Tests if an object can truly be serialized by serializing it to a null
17  * OutputStream.
18  *
19  * @version 1.5, 12/19/03
20  * @since 1.4
21  */

22 final class SerializationTester {
23     private static ObjectOutputStream JavaDoc stream;
24     static {
25         try {
26             stream = new ObjectOutputStream JavaDoc(new OutputStream JavaDoc() {
27                     public void write(int b) {}
28                 });
29         } catch (IOException JavaDoc cannotHappen) {
30         }
31     }
32
33     static boolean test(Object JavaDoc obj) {
34         if (!(obj instanceof Serializable JavaDoc)) {
35             return false;
36         }
37
38         try {
39             stream.writeObject(obj);
40         } catch (IOException JavaDoc e) {
41             return false;
42         } finally {
43             // Fix for 4503661.
44
// Reset the stream so that it doesn't keep a reference to the
45
// written object.
46
try {
47                 stream.reset();
48             } catch (IOException JavaDoc e) {
49                 // Ignore the exception.
50
}
51         }
52         return true;
53     }
54
55     private SerializationTester() {}
56 }
57
Popular Tags