RFR: 8189366: SocketInputStream.available() should check for eof
Daniel Fuchs
daniel.fuchs at oracle.com
Fri Oct 12 11:58:45 UTC 2018
On 12/10/2018 12:01, Chris Hegarty wrote:
> That buys us little more than we had prior to this change,
> since impl.available will still call into native before
> checking the EOF status.
>
> If we want to keep this, then we need:
>
> public int available() throws IOException {
> if (impl.isClosedOrPending()) {
> throw new SocketException("Socket closed");
> }
>
> if (eof) {
> return 0;
> } else {
> return impl.available();
> }
> }
>
Hmmm...
I thought impl.available() was going to throw the
exception?
If you don't want to penalize the regular case where
eof is false, and the impl is supposed to throw the
exception, and you want to avoid to go back to the
native impl when eof has been reached, then maybe you
need:
public int available() throws IOException {
if (eof) {
if (impl.isClosedOrPending()) {
throw new SocketException("Socket closed");
}
return 0;
} else {
return impl.available();
}
}
and that's assuming the that the exception that the impl would
throw is exactly new SocketException("Socket closed");
Can't help feeling that
int available = impl.available();
return eof ? 0 : available;
addresses the issue of available potentially returning garbage
after EOF while being much less risky...
cheers,
-- daniel
More information about the net-dev
mailing list