2 * Copyright (C) 2012 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 import java.io.ByteArrayInputStream;
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.io.ObjectInputStream;
21 import java.io.ObjectOutputStream;
22 import java.io.Serializable;
25 * Exercise serialization.
29 public static void main(String[] args) {
30 testObjectSerialization();
33 static void testObjectSerialization() {
37 serialData = createStream();
38 checkStream(serialData);
39 } catch (IOException ioe) {
40 throw new RuntimeException(ioe);
44 static byte[] createStream() throws IOException {
45 ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
46 ObjectOutputStream objStream = new ObjectOutputStream(byteStream);
48 Sub sub = new Sub('X');
49 objStream.writeObject(sub);
50 byte[] bytes = byteStream.toByteArray();
57 static void checkStream(byte[] input) throws IOException {
58 ByteArrayInputStream byteStream = new ByteArrayInputStream(input);
59 ObjectInputStream objStream = new ObjectInputStream(byteStream);
63 sub = (Sub) objStream.readObject();
64 } catch (ClassNotFoundException cnfe) {
65 throw new RuntimeException(cnfe);
75 class Base implements Serializable {
76 private static final long serialVersionUID = 12345;
84 two = Integer.valueOf(2);
89 class Sub extends Base {
90 private static final long serialVersionUID = 54321;
94 private Byte six = 26;
95 Character seven = '7';
98 public Character thing;
100 public Sub(char thing) {
109 public void check() {
110 System.out.println("one=" + one + " two=" + two + " three=" + three
111 + " four=" + four + " five=" + five + " six=" + six
112 + " seven=" + seven + " eight=" + eight + " nine=" + nine
113 + " thing=" + thing);