Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/Serializable/classDescHooks/ClassDescHooks.java
41154 views
1
/*
2
* Copyright (c) 1999, 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 4227189
26
* @summary Ensure that class descriptor read, write hooks exist, are backwards
27
* compatible, and work as advertised.
28
*/
29
30
import java.io.*;
31
import java.util.*;
32
33
class Foo implements Serializable {
34
private static final long serialVersionUID = 1L;
35
Short s = (short) 1;
36
Integer i = 2;
37
Long l = 3L;
38
39
public boolean equals(Object obj) {
40
if (obj instanceof Foo) {
41
Foo ofoo = (Foo) obj;
42
return s.equals(ofoo.s) && i.equals(ofoo.i) && l.equals(ofoo.l);
43
}
44
return false;
45
}
46
47
public int hashCode() {
48
return i;
49
}
50
}
51
52
class CustomOutputStream extends ObjectOutputStream {
53
54
boolean hookCalled = false;
55
56
CustomOutputStream(OutputStream out) throws IOException {
57
super(out);
58
useProtocolVersion(PROTOCOL_VERSION_2);
59
}
60
61
protected void writeClassDescriptor(ObjectStreamClass desc)
62
throws IOException
63
{
64
writeUTF(desc.getName());
65
hookCalled = true;
66
}
67
}
68
69
class CustomInputStream extends ObjectInputStream {
70
71
boolean hookCalled = false;
72
73
CustomInputStream(InputStream in) throws IOException {
74
super(in);
75
}
76
77
protected ObjectStreamClass readClassDescriptor()
78
throws IOException, ClassNotFoundException
79
{
80
hookCalled = true;
81
return ObjectStreamClass.lookup(Class.forName(readUTF()));
82
}
83
}
84
85
public class ClassDescHooks implements ObjectStreamConstants {
86
public static void main(String[] args) throws Exception {
87
ByteArrayOutputStream bout;
88
ByteArrayInputStream bin;
89
ObjectOutputStream oout;
90
ObjectInputStream oin;
91
FileInputStream fin;
92
File foof;
93
CustomOutputStream cout;
94
CustomInputStream cin;
95
96
// test for backwards compatibility
97
bout = new ByteArrayOutputStream();
98
foof = new File(System.getProperty("test.src", "."), "Foo.ser");
99
fin = new FileInputStream(foof);
100
try {
101
while (fin.available() > 0)
102
bout.write(fin.read());
103
} finally {
104
fin.close();
105
}
106
byte[] buf1 = bout.toByteArray();
107
108
bout = new ByteArrayOutputStream();
109
oout = new ObjectOutputStream(bout);
110
Foo foo = new Foo();
111
oout.writeObject(foo);
112
oout.flush();
113
byte[] buf2 = bout.toByteArray();
114
115
if (! Arrays.equals(buf1, buf2))
116
throw new Error("Incompatible stream format (write)");
117
118
Foo foocopy;
119
fin = new FileInputStream(foof);
120
try {
121
oin = new ObjectInputStream(fin);
122
foocopy = (Foo) oin.readObject();
123
if (! foo.equals(foocopy))
124
throw new Error("Incompatible stream format (read)");
125
} finally {
126
fin.close();
127
}
128
129
// make sure write hook not called when old protocol in use
130
bout = new ByteArrayOutputStream();
131
cout = new CustomOutputStream(bout);
132
cout.useProtocolVersion(PROTOCOL_VERSION_1);
133
cout.writeObject(foo);
134
if (cout.hookCalled)
135
throw new Error("write descriptor hook should not be called");
136
137
// write custom class descriptor representations
138
bout = new ByteArrayOutputStream();
139
cout = new CustomOutputStream(bout);
140
cout.writeObject(foo);
141
cout.flush();
142
bin = new ByteArrayInputStream(bout.toByteArray());
143
cin = new CustomInputStream(bin);
144
foocopy = (Foo) cin.readObject();
145
if (! cout.hookCalled)
146
throw new Error("write descriptor hook never called");
147
if (! cin.hookCalled)
148
throw new Error("read descriptor hook never called");
149
if (! foo.equals(foocopy))
150
throw new Error("serialization failed when hooks active");
151
}
152
}
153
154