Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/io/Serializable/classDescHooks/ExternLoopback.java
38828 views
1
/*
2
* Copyright (c) 2001, 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 descs;
36
37
LoopbackOutputStream(OutputStream out, LinkedList 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 descs;
53
54
LoopbackInputStream(InputStream in, LinkedList descs) throws IOException {
55
super(in);
56
this.descs = descs;
57
}
58
59
protected ObjectStreamClass readClassDescriptor()
60
throws IOException, ClassNotFoundException
61
{
62
return (ObjectStreamClass) descs.removeFirst();
63
}
64
}
65
66
public class ExternLoopback implements Externalizable {
67
68
String a, b, c;
69
70
public ExternLoopback() {
71
}
72
73
ExternLoopback(String a, String b, String c) {
74
this.a = a;
75
this.b = b;
76
this.c = c;
77
}
78
79
public void writeExternal(ObjectOutput out) throws IOException {
80
out.writeBoolean(false);
81
out.writeObject(a);
82
out.writeObject(b);
83
out.writeObject(c);
84
}
85
86
public void readExternal(ObjectInput in)
87
throws IOException, ClassNotFoundException
88
{
89
in.readBoolean();
90
a = (String) in.readObject();
91
b = (String) in.readObject();
92
c = (String) in.readObject();
93
}
94
95
public boolean equals(Object obj) {
96
if (!(obj instanceof ExternLoopback)) {
97
return false;
98
}
99
ExternLoopback other = (ExternLoopback) obj;
100
return streq(a, other.a) && streq(b, other.b) && streq(c, other.c);
101
}
102
103
static boolean streq(String s1, String s2) {
104
return (s1 != null) ? s1.equals(s2) : (s2 == null);
105
}
106
107
public static void main(String[] args) throws Exception {
108
ExternLoopback lb = new ExternLoopback("foo", "bar", "baz");
109
LinkedList descs = new LinkedList();
110
ByteArrayOutputStream bout = new ByteArrayOutputStream();
111
LoopbackOutputStream lout = new LoopbackOutputStream(bout, descs);
112
lout.writeObject(lb);
113
lout.close();
114
115
LoopbackInputStream lin = new LoopbackInputStream(
116
new ByteArrayInputStream(bout.toByteArray()), descs);
117
ExternLoopback lbcopy = (ExternLoopback) lin.readObject();
118
if (!lb.equals(lbcopy)) {
119
throw new Error();
120
}
121
}
122
}
123
124