01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: *
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package java.rmi;
20:
21: import java.io.IOException;
22: import java.io.Serializable;
23: import java.io.ByteArrayOutputStream;
24: import java.util.Arrays;
25:
26: import org.apache.harmony.rmi.MarshalledObjectInputStream;
27: import org.apache.harmony.rmi.MarshalledObjectOutputStream;
28:
29: public final class MarshalledObject implements Serializable {
30: private static final long serialVersionUID = 8988374069173025854L;
31:
32: private final byte[] objBytes;
33:
34: private final byte[] locBytes;
35:
36: private final int hash;
37:
38: public MarshalledObject(Object obj) throws IOException {
39: ByteArrayOutputStream objStream = new ByteArrayOutputStream();
40: MarshalledObjectOutputStream moStream = new MarshalledObjectOutputStream(
41: objStream);
42: moStream.writeObject(obj);
43: moStream.flush();
44: objBytes = objStream.toByteArray();
45: locBytes = moStream.getLocBytes();
46:
47: // calculate hash code
48: int hash = 0;
49:
50: for (int i = 0; i < objBytes.length; ++i) {
51: hash = hash * 31 + objBytes[i];
52: }
53: this .hash = hash;
54: }
55:
56: @Override
57: public boolean equals(Object obj) {
58: if (!(obj instanceof MarshalledObject)) {
59: return false;
60: }
61: MarshalledObject anotherObj = (MarshalledObject) obj;
62: return (hash == anotherObj.hash)
63: || (Arrays.equals(objBytes, anotherObj.objBytes));
64: }
65:
66: public Object get() throws IOException, ClassNotFoundException {
67: if (objBytes == null) {
68: return null;
69: }
70: MarshalledObjectInputStream moin = new MarshalledObjectInputStream(
71: objBytes, locBytes);
72: return moin.readObject();
73: }
74:
75: @Override
76: public int hashCode() {
77: return hash;
78: }
79: }
|