Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/io/Externalizable/definesWriteObject/DefinesWriteObject.java
41153 views
1
/*
2
* Copyright (c) 1998, 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
/*
25
* @test
26
* @bug 4151469
27
* @summary Write and read an Externalizable class that defines writeObject.
28
* There was some confusion over writeObject method needing
29
* to be defined by an Externalizable class. It does not
30
* need to exist, but Externalizable class should work correctly
31
* if the member exists.
32
*/
33
34
import java.io.*;
35
36
public class DefinesWriteObject implements Externalizable {
37
38
private int intData = 4;
39
private Object objData = new String("hello");
40
41
public DefinesWriteObject() {
42
}
43
44
public DefinesWriteObject(int i, Object o) {
45
intData = i;
46
objData = o;
47
}
48
49
/**
50
* There was some confusion over writeObject method needing
51
* to be defined by an Externalizable class. It does not
52
* need to exist, but Externalizable class should work correctly
53
* if the member exists.
54
*/
55
private void writeObject(ObjectOutputStream out) throws IOException {
56
}
57
58
/**
59
* @serialData Writes an integer, Object.
60
*/
61
public void writeExternal(ObjectOutput out)
62
throws IOException
63
{
64
out.writeInt(intData);
65
out.writeObject(objData);
66
}
67
68
public void readExternal(ObjectInput in)
69
throws IOException, ClassNotFoundException
70
{
71
intData = in.readInt();
72
objData = in.readObject();
73
}
74
75
public static void main(String args[])
76
throws IOException, ClassNotFoundException
77
{
78
DefinesWriteObject obj1 = new DefinesWriteObject(5, "GoodBye");
79
DefinesWriteObject obj2 = new DefinesWriteObject(6, "AuRevoir");
80
81
ByteArrayOutputStream baos = new ByteArrayOutputStream();
82
ObjectOutputStream oos = new ObjectOutputStream(baos);
83
oos.writeObject(obj1);
84
oos.writeObject(obj2);
85
oos.close();
86
87
ByteArrayInputStream bais =
88
new ByteArrayInputStream(baos.toByteArray());
89
ObjectInputStream ois = new ObjectInputStream(bais);
90
DefinesWriteObject readObject1 = (DefinesWriteObject)ois.readObject();
91
DefinesWriteObject readObject2 = (DefinesWriteObject)ois.readObject();
92
ois.close();
93
94
// verify that deserialize data matches objects serialized.
95
if (obj1.intData != readObject1.intData ||
96
obj2.intData != readObject2.intData) {
97
throw new Error("Unexpected mismatch between integer data written and read.");
98
}
99
if ( ! ((String)obj1.objData).equals((String)readObject1.objData) ||
100
! ((String)obj2.objData).equals((String)readObject2.objData)) {
101
throw new Error("Unexpected mismatch between String data written and read.");
102
}
103
}
104
};
105
106