Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Properties/PropertiesSerialization.java
41152 views
1
/*
2
* Copyright (c) 2016, 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
import java.io.ByteArrayInputStream;
25
import java.io.ByteArrayOutputStream;
26
import java.io.IOException;
27
import java.io.ObjectInputStream;
28
import java.io.ObjectOutputStream;
29
import java.util.Base64;
30
import java.util.Properties;
31
32
/**
33
* @test
34
* @bug 8029891
35
* @summary tests the compatibility of Properties serial form
36
* @run main PropertiesSerialization read
37
*
38
* To update this test in case the serial form of Properties changes, run this
39
* test with the 'write' flag, and copy the resulting output back into this
40
* file, replacing the existing String declaration(s).
41
*/
42
public class PropertiesSerialization {
43
private static final Properties TEST_PROPS;
44
static {
45
TEST_PROPS = new Properties();
46
TEST_PROPS.setProperty("one", "two");
47
TEST_PROPS.setProperty("buckle", "shoe");
48
TEST_PROPS.setProperty("three", "four");
49
TEST_PROPS.setProperty("shut", "door");
50
}
51
52
/**
53
* Base64 encoded string for Properties object
54
* Java version: 1.8.0
55
**/
56
private static final String TEST_SER_BASE64 =
57
"rO0ABXNyABRqYXZhLnV0aWwuUHJvcGVydGllczkS0HpwNj6YAgABTAAIZGVmYXVs"
58
+ "dHN0ABZMamF2YS91dGlsL1Byb3BlcnRpZXM7eHIAE2phdmEudXRpbC5IYXNodGFi"
59
+ "bGUTuw8lIUrkuAMAAkYACmxvYWRGYWN0b3JJAAl0aHJlc2hvbGR4cD9AAAAAAAAI"
60
+ "dwgAAAALAAAABHQAA29uZXQAA3R3b3QABHNodXR0AARkb29ydAAGYnVja2xldAAE"
61
+ "c2hvZXQABXRocmVldAAEZm91cnhw";
62
63
public static void main(String[] args) throws IOException,
64
ClassNotFoundException {
65
if (args.length == 0) {
66
System.err.println("Run with 'read' or 'write'");
67
System.err.println(" read mode: normal test mode.");
68
System.err.println(" Confirms that serial stream can");
69
System.err.println(" be deserialized as expected.");
70
System.err.println(" write mode: meant for updating the test,");
71
System.err.println(" should the serial form change.");
72
System.err.println(" Test output should be pasted");
73
System.err.println(" back into the test source.");
74
return;
75
}
76
77
Properties deserializedObject;
78
if ("read".equals(args[0])) {
79
ByteArrayInputStream bais = new
80
ByteArrayInputStream(Base64.getDecoder().decode(TEST_SER_BASE64));
81
try (ObjectInputStream ois = new ObjectInputStream(bais)) {
82
deserializedObject = (Properties) ois.readObject();
83
}
84
if (!TEST_PROPS.equals(deserializedObject)) {
85
throw new RuntimeException("deserializedObject not equals()");
86
}
87
System.out.println("Test passed");
88
} else if ("write".equals(args[0])) {
89
System.out.println("\nTo update the test, paste the following back "
90
+ "into the test code:\n");
91
ByteArrayOutputStream baos = new ByteArrayOutputStream();
92
ObjectOutputStream oos = new ObjectOutputStream(baos);
93
oos.writeObject(TEST_PROPS);
94
oos.flush();
95
oos.close();
96
97
byte[] byteArray = baos.toByteArray();
98
// Check that the Properties deserializes correctly
99
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
100
ObjectInputStream ois = new ObjectInputStream(bais);
101
Properties deser = (Properties)ois.readObject();
102
if (!TEST_PROPS.equals(deser)) {
103
throw new RuntimeException("write: Deserialized != original");
104
}
105
106
// Now get a Base64 string representation of the serialized bytes.
107
final String base64 = Base64.getEncoder().encodeToString(byteArray);
108
// Check that we can deserialize the Base64 string we just computed.
109
ByteArrayInputStream bais2 =
110
new ByteArrayInputStream(Base64.getDecoder().decode(base64));
111
ObjectInputStream ois2 = new ObjectInputStream(bais2);
112
Properties deser2 = (Properties)ois2.readObject();
113
if (!TEST_PROPS.equals(deser2)) {
114
throw new RuntimeException("write: Deserialized base64 != "
115
+ "original");
116
}
117
System.out.println(dumpBase64SerialStream(base64));
118
}
119
}
120
121
private static final String INDENT = " ";
122
/* Based on:
123
* java/util/logging/HigherResolutionTimeStamps/SerializeLogRecored.java
124
*/
125
private static String dumpBase64SerialStream(String base64) {
126
// Generates the Java Pseudo code that can be cut & pasted into
127
// this test (see Jdk8SerializedLog and Jdk9SerializedLog below)
128
final StringBuilder sb = new StringBuilder();
129
sb.append(INDENT).append(" /**").append('\n');
130
sb.append(INDENT).append(" * Base64 encoded string for Properties object\n");
131
sb.append(INDENT).append(" * Java version: ")
132
.append(System.getProperty("java.version")).append('\n');
133
sb.append(INDENT).append(" **/").append('\n');
134
sb.append(INDENT).append(" private static final String TEST_SER_BASE64 = ")
135
.append("\n").append(INDENT).append(" ");
136
final int last = base64.length() - 1;
137
for (int i=0; i<base64.length();i++) {
138
if (i%64 == 0) sb.append("\"");
139
sb.append(base64.charAt(i));
140
if (i%64 == 63 || i == last) {
141
sb.append("\"");
142
if (i == last) sb.append(";\n");
143
else sb.append("\n").append(INDENT).append(" + ");
144
}
145
}
146
return sb.toString();
147
}
148
}
149
150