OptimizedObjectInputStream#available() doesn't work as expected?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

OptimizedObjectInputStream#available() doesn't work as expected?

Pavel Pereslegin
Hello Igniters!

When deserializing Externalizable cache entry it's impossible to use
ObjectInput#available method for checking is there anything left to
read in buffer. This is because OptimizedObjectInputStream#available
always returns -1 (hard-coded).

Does anyone know the reasons why this method is not fully implemented?
Is it acceptable to extend GridDataInput with method available() and
implement it by calling available() from underlying input stream?

I've faced with such problem when I tried to put additional field into
GridCacheSetHeader class. It should be able to read both the old and
new versions of this class, so, currently, I have to catch
EOFException to safely read the old one.

@Override public void readExternal(ObjectInput in) throws IOException {
    oldField = in.readInt();
    ...
    try {
        newField = in.readInt();
    }
    catch (EOFException e) {
        newField = DEFAULT_VALUE;
    }
}

With available() method this code will look like

@Override public void readExternal(ObjectInput in) throws IOException {
    oldField = in.readInt();
    ...
    if (in.available() > 0)
        newField = in.readInt();
    else
        newField = DEFAULT_VALUE;
}

Which case is preferred – to catch EOFException or to implement
available() at GridDataInput which looks obsolete?
Reply | Threaded
Open this post in threaded view
|

Re: OptimizedObjectInputStream#available() doesn't work as expected?

Anton Vinogradov-2
Folks,

I checked and it's possible to use available() directly from stream
encapsulated at GridDataInput, but I'm not sure it's a good idea to update
almost obsolete classes.

Catching EOFException hack looks as reasonable solution in current case.

Thoughts?


чт, 31 мая 2018 г. в 11:39, Pavel Pereslegin <[hidden email]>:

> Hello Igniters!
>
> When deserializing Externalizable cache entry it's impossible to use
> ObjectInput#available method for checking is there anything left to
> read in buffer. This is because OptimizedObjectInputStream#available
> always returns -1 (hard-coded).
>
> Does anyone know the reasons why this method is not fully implemented?
> Is it acceptable to extend GridDataInput with method available() and
> implement it by calling available() from underlying input stream?
>
> I've faced with such problem when I tried to put additional field into
> GridCacheSetHeader class. It should be able to read both the old and
> new versions of this class, so, currently, I have to catch
> EOFException to safely read the old one.
>
> @Override public void readExternal(ObjectInput in) throws IOException {
>     oldField = in.readInt();
>     ...
>     try {
>         newField = in.readInt();
>     }
>     catch (EOFException e) {
>         newField = DEFAULT_VALUE;
>     }
> }
>
> With available() method this code will look like
>
> @Override public void readExternal(ObjectInput in) throws IOException {
>     oldField = in.readInt();
>     ...
>     if (in.available() > 0)
>         newField = in.readInt();
>     else
>         newField = DEFAULT_VALUE;
> }
>
> Which case is preferred – to catch EOFException or to implement
> available() at GridDataInput which looks obsolete?
>