summaryrefslogtreecommitdiffstats
path: root/tests/093-serialization/src/Main.java
blob: ca3dc9fd0ed91a1b7a49cbda2c9a93715fe09092 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * Exercise serialization.
 */
public class Main {

    public static void main(String[] args) {
        testObjectSerialization();
    }

    static void testObjectSerialization() {
        byte[] serialData;

        try {
            serialData = createStream();
            checkStream(serialData);
        } catch (IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }

    static byte[] createStream() throws IOException {
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        ObjectOutputStream objStream = new ObjectOutputStream(byteStream);

        Sub sub = new Sub('X');
        objStream.writeObject(sub);
        byte[] bytes = byteStream.toByteArray();

        objStream.close();
        byteStream.close();
        return bytes;
    }

    static void checkStream(byte[] input) throws IOException {
        ByteArrayInputStream byteStream = new ByteArrayInputStream(input);
        ObjectInputStream objStream = new ObjectInputStream(byteStream);

        Sub sub;
        try {
            sub = (Sub) objStream.readObject();
        } catch (ClassNotFoundException cnfe) {
            throw new RuntimeException(cnfe);
        }

        objStream.close();
        byteStream.close();

        sub.check();
    }
}

class Base implements Serializable {
    private static final long serialVersionUID = 12345;

    Boolean one;
    Integer two;
    String three;

    public Base() {
        one = true;
        two = Integer.valueOf(2);
        three = "three";
    }
}

class Sub extends Base {
    private static final long serialVersionUID = 54321;

    Double four;
    Float five;
    private Byte six = 26;
    Character seven = '7';
    Short eight;
    long nine;
    public Character thing;

    public Sub(char thing) {
        four = 4.0;
        five = 5.0f;
        six = 6;
        eight = 8;
        nine = 9;
        this.thing = thing;
    }

    public void check() {
        System.out.println("one=" + one + " two=" + two + " three=" + three
            + " four=" + four + " five=" + five + " six=" + six
            + " seven=" + seven + " eight=" + eight + " nine=" + nine
            + " thing=" + thing);
    }
}