ignite-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexey Goncharuk <alexey.goncha...@gmail.com>
Subject Re: Externalizable in cache
Date Mon, 15 Feb 2016 17:44:30 GMT
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;
}

Mime
View raw message