summaryrefslogtreecommitdiff
path: root/tutorial/examples/serialize/OIOTest.java
blob: 717707fa81a7747d07d016ef3b48827b86ee38c7 (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
package examples.serialize;

import javax.microedition.midlet.MIDlet;
import java.io.*;

public class OIOTest extends MIDlet
{

    public void startApp() {
        try {
            main();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void main() throws IOException {
        ColoredPoint p = new ColoredPoint();
        p.point.x = 10;  p.point.y = 20;

        /* Writes objects */
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOut oos = new ObjectOut(baos);

        oos.writeObject(p);
        p.color.g = (byte)2;
        oos.writeObject(p);
        oos.flush();

        byte[] buf = baos.toByteArray();

        /* Reads objects */
        ByteArrayInputStream bais = new ByteArrayInputStream(buf);
        ObjectIn ois = new ObjectIn(bais);

        ColoredPoint p2 = (ColoredPoint) ois.readObject();
        ColoredPoint p3 = (ColoredPoint) ois.readObject();

        System.out.println("p2.color.r = " + p2.color.r);
        System.out.println("p2.color.g = " + p2.color.g);
        System.out.println("p2.color.b = " + p2.color.b);
        System.out.println("p2.point.x = " + p2.point.x);
        System.out.println("p2.point.y = " + p2.point.y);
        System.out.println("p3.color.r = " + p3.color.r);
        System.out.println("p3.color.g = " + p3.color.g);
        System.out.println("p3.color.b = " + p3.color.b);

    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

}