Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/rmi/MarshalledObject.java
38829 views
1
/*
2
* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package java.rmi;
27
28
import java.io.ByteArrayInputStream;
29
import java.io.ByteArrayOutputStream;
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.io.ObjectInputStream;
33
import java.io.ObjectOutputStream;
34
import java.io.ObjectStreamConstants;
35
import java.io.OutputStream;
36
import java.io.Serializable;
37
import java.security.AccessController;
38
import java.security.PrivilegedAction;
39
40
import sun.rmi.server.MarshalInputStream;
41
import sun.rmi.server.MarshalOutputStream;
42
43
import sun.misc.ObjectInputFilter;
44
45
/**
46
* A <code>MarshalledObject</code> contains a byte stream with the serialized
47
* representation of an object given to its constructor. The <code>get</code>
48
* method returns a new copy of the original object, as deserialized from
49
* the contained byte stream. The contained object is serialized and
50
* deserialized with the same serialization semantics used for marshaling
51
* and unmarshaling parameters and return values of RMI calls: When the
52
* serialized form is created:
53
*
54
* <ul>
55
* <li> classes are annotated with a codebase URL from where the class
56
* can be loaded (if available), and
57
* <li> any remote object in the <code>MarshalledObject</code> is
58
* represented by a serialized instance of its stub.
59
* </ul>
60
*
61
* <p>When copy of the object is retrieved (via the <code>get</code> method),
62
* if the class is not available locally, it will be loaded from the
63
* appropriate location (specified the URL annotated with the class descriptor
64
* when the class was serialized.
65
*
66
* <p><code>MarshalledObject</code> facilitates passing objects in RMI calls
67
* that are not automatically deserialized immediately by the remote peer.
68
*
69
* @param <T> the type of the object contained in this
70
* <code>MarshalledObject</code>
71
*
72
* @author Ann Wollrath
73
* @author Peter Jones
74
* @since 1.2
75
*/
76
public final class MarshalledObject<T> implements Serializable {
77
/**
78
* @serial Bytes of serialized representation. If <code>objBytes</code> is
79
* <code>null</code> then the object marshalled was a <code>null</code>
80
* reference.
81
*/
82
private byte[] objBytes = null;
83
84
/**
85
* @serial Bytes of location annotations, which are ignored by
86
* <code>equals</code>. If <code>locBytes</code> is null, there were no
87
* non-<code>null</code> annotations during marshalling.
88
*/
89
private byte[] locBytes = null;
90
91
/**
92
* @serial Stored hash code of contained object.
93
*
94
* @see #hashCode
95
*/
96
private int hash;
97
98
/** Filter used when creating the instance from a stream; may be null. */
99
private transient ObjectInputFilter objectInputFilter = null;
100
101
/** Indicate compatibility with 1.2 version of class. */
102
private static final long serialVersionUID = 8988374069173025854L;
103
104
/**
105
* Creates a new <code>MarshalledObject</code> that contains the
106
* serialized representation of the current state of the supplied object.
107
* The object is serialized with the semantics used for marshaling
108
* parameters for RMI calls.
109
*
110
* @param obj the object to be serialized (must be serializable)
111
* @exception IOException if an <code>IOException</code> occurs; an
112
* <code>IOException</code> may occur if <code>obj</code> is not
113
* serializable.
114
* @since 1.2
115
*/
116
public MarshalledObject(T obj) throws IOException {
117
if (obj == null) {
118
hash = 13;
119
return;
120
}
121
122
ByteArrayOutputStream bout = new ByteArrayOutputStream();
123
ByteArrayOutputStream lout = new ByteArrayOutputStream();
124
MarshalledObjectOutputStream out =
125
new MarshalledObjectOutputStream(bout, lout);
126
out.writeObject(obj);
127
out.flush();
128
objBytes = bout.toByteArray();
129
// locBytes is null if no annotations
130
locBytes = (out.hadAnnotations() ? lout.toByteArray() : null);
131
132
/*
133
* Calculate hash from the marshalled representation of object
134
* so the hashcode will be comparable when sent between VMs.
135
*/
136
int h = 0;
137
for (int i = 0; i < objBytes.length; i++) {
138
h = 31 * h + objBytes[i];
139
}
140
hash = h;
141
}
142
143
/**
144
* Reads in the state of the object and saves the stream's
145
* serialization filter to be used when the object is deserialized.
146
*
147
* @param stream the stream
148
* @throws IOException if an I/O error occurs
149
* @throws ClassNotFoundException if a class cannot be found
150
*/
151
private void readObject(ObjectInputStream stream)
152
throws IOException, ClassNotFoundException {
153
stream.defaultReadObject(); // read in all fields
154
objectInputFilter = ObjectInputFilter.Config.getObjectInputFilter(stream);
155
}
156
157
/**
158
* Returns a new copy of the contained marshalledobject. The internal
159
* representation is deserialized with the semantics used for
160
* unmarshaling parameters for RMI calls.
161
*
162
* @return a copy of the contained object
163
* @exception IOException if an <code>IOException</code> occurs while
164
* deserializing the object from its internal representation.
165
* @exception ClassNotFoundException if a
166
* <code>ClassNotFoundException</code> occurs while deserializing the
167
* object from its internal representation.
168
* could not be found
169
* @since 1.2
170
*/
171
public T get() throws IOException, ClassNotFoundException {
172
if (objBytes == null) // must have been a null object
173
return null;
174
175
ByteArrayInputStream bin = new ByteArrayInputStream(objBytes);
176
// locBytes is null if no annotations
177
ByteArrayInputStream lin =
178
(locBytes == null ? null : new ByteArrayInputStream(locBytes));
179
MarshalledObjectInputStream in =
180
new MarshalledObjectInputStream(bin, lin, objectInputFilter);
181
@SuppressWarnings("unchecked")
182
T obj = (T) in.readObject();
183
in.close();
184
return obj;
185
}
186
187
/**
188
* Return a hash code for this <code>MarshalledObject</code>.
189
*
190
* @return a hash code
191
*/
192
public int hashCode() {
193
return hash;
194
}
195
196
/**
197
* Compares this <code>MarshalledObject</code> to another object.
198
* Returns true if and only if the argument refers to a
199
* <code>MarshalledObject</code> that contains exactly the same
200
* serialized representation of an object as this one does. The
201
* comparison ignores any class codebase annotation, meaning that
202
* two objects are equivalent if they have the same serialized
203
* representation <i>except</i> for the codebase of each class
204
* in the serialized representation.
205
*
206
* @param obj the object to compare with this <code>MarshalledObject</code>
207
* @return <code>true</code> if the argument contains an equivalent
208
* serialized object; <code>false</code> otherwise
209
* @since 1.2
210
*/
211
public boolean equals(Object obj) {
212
if (obj == this)
213
return true;
214
215
if (obj != null && obj instanceof MarshalledObject) {
216
MarshalledObject<?> other = (MarshalledObject<?>) obj;
217
218
// if either is a ref to null, both must be
219
if (objBytes == null || other.objBytes == null)
220
return objBytes == other.objBytes;
221
222
// quick, easy test
223
if (objBytes.length != other.objBytes.length)
224
return false;
225
226
//!! There is talk about adding an array comparision method
227
//!! at 1.2 -- if so, this should be rewritten. -arnold
228
for (int i = 0; i < objBytes.length; ++i) {
229
if (objBytes[i] != other.objBytes[i])
230
return false;
231
}
232
return true;
233
} else {
234
return false;
235
}
236
}
237
238
/**
239
* This class is used to marshal objects for
240
* <code>MarshalledObject</code>. It places the location annotations
241
* to one side so that two <code>MarshalledObject</code>s can be
242
* compared for equality if they differ only in location
243
* annotations. Objects written using this stream should be read back
244
* from a <code>MarshalledObjectInputStream</code>.
245
*
246
* @see java.rmi.MarshalledObject
247
* @see MarshalledObjectInputStream
248
*/
249
private static class MarshalledObjectOutputStream
250
extends MarshalOutputStream
251
{
252
/** The stream on which location objects are written. */
253
private ObjectOutputStream locOut;
254
255
/** <code>true</code> if non-<code>null</code> annotations are
256
* written.
257
*/
258
private boolean hadAnnotations;
259
260
/**
261
* Creates a new <code>MarshalledObjectOutputStream</code> whose
262
* non-location bytes will be written to <code>objOut</code> and whose
263
* location annotations (if any) will be written to
264
* <code>locOut</code>.
265
*/
266
MarshalledObjectOutputStream(OutputStream objOut, OutputStream locOut)
267
throws IOException
268
{
269
super(objOut);
270
this.useProtocolVersion(ObjectStreamConstants.PROTOCOL_VERSION_2);
271
this.locOut = new ObjectOutputStream(locOut);
272
hadAnnotations = false;
273
}
274
275
/**
276
* Returns <code>true</code> if any non-<code>null</code> location
277
* annotations have been written to this stream.
278
*/
279
boolean hadAnnotations() {
280
return hadAnnotations;
281
}
282
283
/**
284
* Overrides MarshalOutputStream.writeLocation implementation to write
285
* annotations to the location stream.
286
*/
287
protected void writeLocation(String loc) throws IOException {
288
hadAnnotations |= (loc != null);
289
locOut.writeObject(loc);
290
}
291
292
293
public void flush() throws IOException {
294
super.flush();
295
locOut.flush();
296
}
297
}
298
299
/**
300
* The counterpart to <code>MarshalledObjectOutputStream</code>.
301
*
302
* @see MarshalledObjectOutputStream
303
*/
304
private static class MarshalledObjectInputStream
305
extends MarshalInputStream
306
{
307
/**
308
* The stream from which annotations will be read. If this is
309
* <code>null</code>, then all annotations were <code>null</code>.
310
*/
311
private ObjectInputStream locIn;
312
313
/**
314
* Creates a new <code>MarshalledObjectInputStream</code> that
315
* reads its objects from <code>objIn</code> and annotations
316
* from <code>locIn</code>. If <code>locIn</code> is
317
* <code>null</code>, then all annotations will be
318
* <code>null</code>.
319
*/
320
MarshalledObjectInputStream(InputStream objIn, InputStream locIn,
321
ObjectInputFilter filter)
322
throws IOException
323
{
324
super(objIn);
325
this.locIn = (locIn == null ? null : new ObjectInputStream(locIn));
326
if (filter != null) {
327
AccessController.doPrivileged(new PrivilegedAction<Void>() {
328
@Override
329
public Void run() {
330
ObjectInputFilter.Config.setObjectInputFilter(MarshalledObjectInputStream.this, filter);
331
if (MarshalledObjectInputStream.this.locIn != null) {
332
ObjectInputFilter.Config.setObjectInputFilter(MarshalledObjectInputStream.this.locIn, filter);
333
}
334
return null;
335
}
336
});
337
}
338
}
339
340
/**
341
* Overrides MarshalInputStream.readLocation to return locations from
342
* the stream we were given, or <code>null</code> if we were given a
343
* <code>null</code> location stream.
344
*/
345
protected Object readLocation()
346
throws IOException, ClassNotFoundException
347
{
348
return (locIn == null ? null : locIn.readObject());
349
}
350
}
351
352
}
353
354