Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/Serializable/classDescHooks/ExternLoopback.java
41154 views
1
/*
2
* Copyright (c) 2001, 2019, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/* @test
25
* @bug 4461737
26
* @summary Verify that serialization functions properly for externalizable
27
* classes if ObjectInputStream.readClassDescriptor() returns a local
28
* class descriptor.
29
*/
30
31
import java.io.*;
32
import java.util.*;
33
34
class LoopbackOutputStream extends ObjectOutputStream {
35
LinkedList<ObjectStreamClass> descs;
36
37
LoopbackOutputStream(OutputStream out, LinkedList<ObjectStreamClass> descs)
38
throws IOException
39
{
40
super(out);
41
this.descs = descs;
42
}
43
44
protected void writeClassDescriptor(ObjectStreamClass desc)
45
throws IOException
46
{
47
descs.add(desc);
48
}
49
}
50
51
class LoopbackInputStream extends ObjectInputStream {
52
LinkedList<ObjectStreamClass> descs;
53
54
LoopbackInputStream(InputStream in, LinkedList<ObjectStreamClass> descs) throws IOException {
55
super(in);
56
this.descs = descs;
57
}
58
59
protected ObjectStreamClass readClassDescriptor()
60
throws IOException, ClassNotFoundException
61
{
62
return descs.removeFirst();
63
}
64
}
65
66
public class ExternLoopback implements Externalizable {
67
private static final long serialVersionUID = 1L;
68
69
String a, b, c;
70
71
public ExternLoopback() {
72
}
73
74
ExternLoopback(String a, String b, String c) {
75
this.a = a;
76
this.b = b;
77
this.c = c;
78
}
79
80
public void writeExternal(ObjectOutput out) throws IOException {
81
out.writeBoolean(false);
82
out.writeObject(a);
83
out.writeObject(b);
84
out.writeObject(c);
85
}
86
87
public void readExternal(ObjectInput in)
88
throws IOException, ClassNotFoundException
89
{
90
in.readBoolean();
91
a = (String) in.readObject();
92
b = (String) in.readObject();
93
c = (String) in.readObject();
94
}
95
96
public boolean equals(Object obj) {
97
if (!(obj instanceof ExternLoopback)) {
98
return false;
99
}
100
ExternLoopback other = (ExternLoopback) obj;
101
return streq(a, other.a) && streq(b, other.b) && streq(c, other.c);
102
}
103
104
public int hashCode() {
105
return a.hashCode();
106
}
107
108
static boolean streq(String s1, String s2) {
109
return (s1 != null) ? s1.equals(s2) : (s2 == null);
110
}
111
112
public static void main(String[] args) throws Exception {
113
ExternLoopback lb = new ExternLoopback("foo", "bar", "baz");
114
LinkedList<ObjectStreamClass> descs = new LinkedList<>();
115
ByteArrayOutputStream bout = new ByteArrayOutputStream();
116
LoopbackOutputStream lout = new LoopbackOutputStream(bout, descs);
117
lout.writeObject(lb);
118
lout.close();
119
120
LoopbackInputStream lin = new LoopbackInputStream(
121
new ByteArrayInputStream(bout.toByteArray()), descs);
122
ExternLoopback lbcopy = (ExternLoopback) lin.readObject();
123
if (!lb.equals(lbcopy)) {
124
throw new Error();
125
}
126
}
127
}
128
129