1741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes/*
2741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * Copyright (C) 2012 The Android Open Source Project
3741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes *
4741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
5741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * you may not use this file except in compliance with the License.
6741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * You may obtain a copy of the License at
7741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes *
8741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
9741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes *
10741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * Unless required by applicable law or agreed to in writing, software
11741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * See the License for the specific language governing permissions and
14741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * limitations under the License.
15741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes */
16741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
17741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.io.ByteArrayInputStream;
18741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.io.ByteArrayOutputStream;
19741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.io.IOException;
20741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.io.ObjectInputStream;
21741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.io.ObjectOutputStream;
22741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesimport java.io.Serializable;
23741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
24741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes/**
25741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes * Exercise serialization.
26741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes */
27741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughespublic class Main {
28741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
29741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    public static void main(String[] args) {
30741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        testObjectSerialization();
31741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    }
32741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
33741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    static void testObjectSerialization() {
34741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        byte[] serialData;
35741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
36741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        try {
37741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            serialData = createStream();
38741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            checkStream(serialData);
39741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        } catch (IOException ioe) {
40741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            throw new RuntimeException(ioe);
41741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        }
42741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    }
43741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
44741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    static byte[] createStream() throws IOException {
45741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
46741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        ObjectOutputStream objStream = new ObjectOutputStream(byteStream);
47741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
48741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        Sub sub = new Sub('X');
49741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        objStream.writeObject(sub);
501441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers        Inner inner = new Inner(0xCAFEF00D);
511441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers        objStream.writeObject(inner);
52741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        byte[] bytes = byteStream.toByteArray();
53741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
54741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        objStream.close();
55741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        byteStream.close();
56741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        return bytes;
57741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    }
58741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
59741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    static void checkStream(byte[] input) throws IOException {
60741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        ByteArrayInputStream byteStream = new ByteArrayInputStream(input);
61741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        ObjectInputStream objStream = new ObjectInputStream(byteStream);
62741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
63741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        Sub sub;
641441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers        Inner inner;
65741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        try {
66741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            sub = (Sub) objStream.readObject();
671441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers            inner = (Inner) objStream.readObject();
68741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        } catch (ClassNotFoundException cnfe) {
69741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            throw new RuntimeException(cnfe);
70741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        }
71741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
72741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        objStream.close();
73741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        byteStream.close();
74741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
75741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        sub.check();
761441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers        inner.check();
771441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers    }
781441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers
791441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers    static class Inner implements Serializable {
801441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers        private static final long serialVersionUID = 319009;
811441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers        private final int x;
821441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers        public Inner (int x) {
831441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers            this.x = x;
841441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers        }
851441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers
861441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers        public void check() {
871441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers            System.out.println("x=" + Integer.toHexString(x));
881441f6f3ea53ce07d119b32eadaf322f71e07676Ian Rogers        }
89741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    }
90741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes}
91741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
92741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesclass Base implements Serializable {
93741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    private static final long serialVersionUID = 12345;
94741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
95741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    Boolean one;
96741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    Integer two;
97741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    String three;
98741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
99741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    public Base() {
100741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        one = true;
101741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        two = Integer.valueOf(2);
102741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        three = "three";
103741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    }
104741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes}
105741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
106741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughesclass Sub extends Base {
107741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    private static final long serialVersionUID = 54321;
108741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
109741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    Double four;
110741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    Float five;
111741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    private Byte six = 26;
112741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    Character seven = '7';
113741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    Short eight;
114741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    long nine;
115741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    public Character thing;
116741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
117741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    public Sub(char thing) {
118741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        four = 4.0;
119741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        five = 5.0f;
120741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        six = 6;
121741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        eight = 8;
122741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        nine = 9;
123741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        this.thing = thing;
124741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    }
125741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes
126741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    public void check() {
127741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes        System.out.println("one=" + one + " two=" + two + " three=" + three
128741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            + " four=" + four + " five=" + five + " six=" + six
129741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            + " seven=" + seven + " eight=" + eight + " nine=" + nine
130741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes            + " thing=" + thing);
131741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes    }
132741b5b7ef4c7fd4a786364bbf60d515489caff47Elliott Hughes}
133