views:

55

answers:

0

I'm wondering if it's possible to de-serialize a generic type into an instance of that type. If it is possible, does Java take into account the generic type's custom de-deserialization (if any)?

In my case, I'm trying to implement a List that is backed by a file which contains the serialized form of the elements, and needs to instantiate a generic type from a byte[], e.g.:

class FileBackedList<V extends Serializable> implements List<V> {
    // ...

    public V get(int index) {
        byte[] value = readFromFile(index);
        // ???????
        // I'm pretty certain this doesn't work
        return (V)(new ObjectInputStream(new ByteArrayInputStream(value)).readObject());
    }

    private byte[] readFromFile(int index) {
        // read bytes at line 'index'
    }
}

Is there any way this type of thing could work?

Edit: And if it does work, am I definitely going to invoke the correct readObject(ObjectInputStream in) for type V if it has a custom one?

By the way, I don't want to serialize the entire list.