aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/java/com/libmailcore/NativeObject.java
blob: 9e92cbd7df4f8d708224fcc404486727d87efc5f (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
package com.libmailcore;

import java.util.Map;
import java.io.Serializable;
import java.io.IOException;

/** Native C++ Object wrapper. */
public class NativeObject implements Cloneable, Serializable {
    protected void finalize() throws Throwable
    {
        super.finalize();
        unsetupNative();
    }

    protected native void initWithNative(long nativeHandle);
    private native void unsetupNative();
    /** Returns a string representing the object. */
    public native String toString();
    /** Create a copy of the object. */
    public native Object clone() throws CloneNotSupportedException;

    private long nativeHandle;

    private static final long serialVersionUID = 1L;

    protected void writeObject(java.io.ObjectOutputStream out) throws IOException {
        byte[] data = serializableData();
        out.writeInt(data.length);
        out.write(data, 0, data.length);
    }

    protected void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
        int len = in.readInt();
        byte[] data = new byte[len];
        in.read(data, 0, len);
    }

    private native byte[] serializableData();
    private native void importSerializableData(byte[] data);

    static {
        MainThreadUtils.singleton();
    }
}