Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/Serializable/oldTests/AnnotateClass.java
41153 views
1
/*
2
* Copyright (c) 2005, 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
* @summary it is new version of old test which was
26
* /src/share/test/serialization/subtest.java
27
* This test verifies of invocation
28
* annotateClass/replaceObject methods
29
*/
30
31
import java.io.*;
32
33
public class AnnotateClass {
34
public static void main (String argv[]) {
35
System.err.println("\nRegression test for verification " +
36
"of invocation annotateClass/replaceObject " +
37
"methods \n");
38
try {
39
FileOutputStream ostream = new FileOutputStream("subtest1.tmp");
40
try {
41
TestOutputStream p = new TestOutputStream(ostream);
42
p.writeObject(System.out);
43
p.writeObject(System.err);
44
p.writeObject(new PrintStream(ostream));
45
p.flush();
46
} finally {
47
ostream.close();
48
}
49
50
FileInputStream istream = new FileInputStream("subtest1.tmp");
51
try {
52
TestInputStream q = new TestInputStream(istream);
53
54
PrintStream out = (PrintStream)q.readObject();
55
PrintStream err = (PrintStream)q.readObject();
56
Object other = q.readObject();
57
if (out != System.out) {
58
System.err.println(
59
"\nTEST FAILED: System.out not read correctly");
60
throw new Error();
61
}
62
if (err != System.err) {
63
System.err.println(
64
"\nTEST FAILED: System.err not read correctly");
65
throw new Error();
66
}
67
if (other != null) {
68
System.err.println(
69
"\nTEST FAILED: Non-system PrintStream should have " +
70
"been written/read as null");
71
throw new Error();
72
}
73
} finally {
74
istream.close();
75
}
76
77
System.err.println("\nTEST PASSED");
78
} catch (Exception e) {
79
System.err.print("TEST FAILED: ");
80
e.printStackTrace();
81
throw new Error();
82
}
83
}
84
}
85
86
87
88
/** ObjectOutputStream is extended to test the annotateClass()
89
* and replaceObject() subclassable methods.
90
* In annotateClass a magic string is written to the stream
91
* so that it can be verified in ObjectInputStream.
92
* replaceObject is used to subsititute a handle object for
93
* one of the standard PrintStreams (stdout or stderr).
94
*/
95
class TestOutputStream extends ObjectOutputStream {
96
/* Construct a new test stream */
97
TestOutputStream(OutputStream out) throws IOException {
98
super(out);
99
enableReplaceObject(true);
100
}
101
102
/* When any class is written, add a "magic" string
103
* that must be verified by the TestInputStream.
104
*/
105
protected void annotateClass(Class<?> cl) throws IOException {
106
this.writeUTF("magic");
107
}
108
109
/* For each object of type PrintStream, substitute
110
* a StdStream handle object that encodes which
111
* of the standard print streams is being written.
112
* Other objects are written as themselves.
113
*/
114
protected Object replaceObject(Object obj)
115
{
116
/* For PrintStreams, like stdout and stderr, encode */
117
if (obj instanceof PrintStream) {
118
return new StdStream((PrintStream)obj);
119
}
120
return obj;
121
}
122
}
123
124
/** Reverse the effects of TestOutputStream.
125
*/
126
class TestInputStream extends ObjectInputStream {
127
128
TestInputStream(InputStream in) throws IOException {
129
super(in);
130
enableResolveObject(true);
131
}
132
133
/** Verify that the magic string was written to the stream
134
* Also use the default classname->class resolution.
135
*/
136
protected Class<?> resolveClass(ObjectStreamClass classdesc)
137
throws ClassNotFoundException, IOException
138
{
139
try {
140
String s = readUTF();
141
if (!(s.equals("magic"))) {
142
System.err.println(
143
"\nTEST FAILED: Bad magic number");
144
throw new Error();
145
}
146
} catch (IOException ee) {
147
System.err.println(
148
"\nTEST FAILED: I/O Exception");
149
throw new Error();
150
}
151
return super.resolveClass(classdesc);
152
}
153
154
/** If the object in the stream is a StdStream,
155
* get the mapping of it to the local System printstream and
156
* return it.
157
* Other objects are returned as themselves.
158
*/
159
protected Object resolveObject(Object obj) {
160
if (obj instanceof StdStream) {
161
return ((StdStream)obj).getStream();
162
}
163
return obj;
164
}
165
}
166
167
/* A holder class to map between standard print streams (stdout, stderr)
168
* and a small integer.
169
*/
170
class StdStream implements java.io.Serializable {
171
private static final long serialVersionUID = 1L;
172
private int stream = 0;
173
174
public StdStream(PrintStream s) {
175
if (s == System.out) {
176
stream = 1;
177
} else if (s == System.err) {
178
stream = 2;
179
}
180
}
181
182
public PrintStream getStream() {
183
if (stream == 1) {
184
return System.out;
185
} else if (stream == 2) {
186
return System.err;
187
} else {
188
return null;
189
}
190
}
191
}
192
193