KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > io > NbMarshalledObject


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.util.io;
21
22 import java.io.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.io.Serializable JavaDoc;
28 import java.util.Arrays JavaDoc;
29
30 /**
31  * Object that holds serialized reference to another object.
32  * Inspired by java.rmi.MarshalledObject but modified to
33  * work with NetBeans and its modules. So no annotations are
34  * stored with the bytestream and when the object
35  * is deserialized it is assumed to be produced by
36  * some installed module.
37  */

38 public final class NbMarshalledObject implements Serializable JavaDoc {
39     /** serial version UID */
40     private static final long serialVersionUID = 7842398740921434354L;
41     private final static char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; // NOI18N
42

43     /**
44      * @serial Bytes of serialized representation. If <code>objBytes</code> is
45      * <code>null</code> then the object marshalled was a <code>null</code>
46      * reference.
47      */

48     private byte[] objBytes = null;
49
50     /**
51      * @serial Stored hash code of contained object.
52      *
53      * @see #hashCode
54      */

55     private int hash;
56
57     /**
58     * Creates a new <code>NbMarshalledObject</code> that contains the
59     * serialized representation of the provided object.
60     *
61     * @param obj the object to be serialized (must be serializable)
62     * @exception IOException the object is not serializable
63     */

64     public NbMarshalledObject(Object JavaDoc obj) throws IOException JavaDoc {
65         if (obj == null) {
66             hash = 17;
67
68             return;
69         }
70
71         ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
72         ObjectOutputStream JavaDoc out = new NbObjectOutputStream(bout);
73         out.writeObject(obj);
74         out.flush();
75         objBytes = bout.toByteArray();
76
77         int h = 0;
78
79         for (int i = 0; i < objBytes.length; i++) {
80             h = (37 * h) + objBytes[i];
81         }
82
83         hash = h;
84     }
85
86     /**
87     * Returns a new copy of the contained marshalledobject.
88     * The object is deserialized by NbObjectInputStream, so it
89     * is assumed that it can be located in some module.
90     *
91     * @return a copy of the contained object
92     * @exception IOException on any I/O problem
93     * @exception ClassNotFoundException if the class of the object cannot be found
94     */

95     public Object JavaDoc get() throws IOException JavaDoc, ClassNotFoundException JavaDoc {
96         if (objBytes == null) { // must have been a null object
97

98             return null;
99         }
100
101         ByteArrayInputStream JavaDoc bin = new ByteArrayInputStream JavaDoc(objBytes);
102         ObjectInputStream JavaDoc ois = new NbObjectInputStream(bin);
103
104         try {
105             return ois.readObject();
106         } catch (RuntimeException JavaDoc weird) {
107             // Probably need to know what the bad ser actually was.
108
StringBuffer JavaDoc buf = new StringBuffer JavaDoc((objBytes.length * 2) + 20);
109             buf.append("Bad ser data: "); // NOI18N
110

111             for (int i = 0; i < objBytes.length; i++) {
112                 int b = objBytes[i];
113
114                 if (b < 0) {
115                     b += 256;
116                 }
117
118                 buf.append(HEX[b / 16]);
119                 buf.append(HEX[b % 16]);
120             }
121
122             IOException JavaDoc ioe = new IOException JavaDoc(weird.toString() + ": " + buf); // NOI18N
123
ioe.initCause(weird);
124             throw ioe;
125         } finally {
126             ois.close();
127         }
128     }
129
130     /**
131     * @return a hash code
132     */

133     public int hashCode() {
134         return hash;
135     }
136
137     /** Two objects are equal if the hold the same serialized
138     * representation.
139     *
140     * @param obj the object to compare with this <code>MarshalledObject</code>
141     * @return <code>true</code> if the objects are serialized into the same bytestreams
142      */

143     public boolean equals(Object JavaDoc obj) {
144         if (obj == this) {
145             return true;
146         }
147
148         if ((obj != null) && obj instanceof NbMarshalledObject) {
149             NbMarshalledObject other = (NbMarshalledObject) obj;
150
151             return Arrays.equals(objBytes, other.objBytes);
152         } else {
153             return false;
154         }
155     }
156 }
157
Popular Tags