Hello Myron,

Your implementation of Externalizable interface is incorrect. The number of
bytes that can be read from the object input stream  passed to the
readExternal() method is not limited, so you need to make sure that you do
not read more bytes than the number of bytes written.

The correct implementation of Externalizable for your case is below:

@Override public void writeExternal(ObjectOutput out) throws IOException {
    out.writeInt(data.length);
    out.write(data);
}

@Override public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
    int len = in.readInt();

    byte[] buffer = new byte[len];

    int total = 0;

    while (total < len) {
        int remaining = len - total;

        int read0 = in.read(buffer, total, remaining);
        total += read0;
    }

    data = buffer;
}

Reply via email to