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

import java.io.*;
import java.util.*;

class ObjectOut extends DataOutputStream
{
    private Vector writtenObjects = new Vector();

    public ObjectOut(OutputStream os) {
        super(os);
    }

    public void writeObject(Marshalable obj) throws IOException {
        if (obj == null) {
            writeInt(-1);
        } else {
            int id = 0;
            int size = writtenObjects.size();
            for (; id < size; ++id) {
                if (writtenObjects.elementAt(id) == obj)  break;
            }
            if (id < size) {
                /* this object is already written to this stream */
                writeInt(-2);
                writeInt(id);
            } else {
                /* this object is not regisered in the table */
                writeInt(id);
                writtenObjects.addElement(obj);
                obj.writeObject(this);
            }
        }
    }

}