Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8189366

SocketInputStream.available() should check for eof

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 12
    • 9, 12
    • core-libs
    • b16
    • Verified

      A colleague reports:


      OpenJDK 9's SocketInputStream.available() currently delegates to the impl even when eof is reached, even though in that case read() immediately returns -1 without delegating to the impl. This can lead to available() returning values other than 0 which is incorrect.

      Here's a change that fixes SocketInputStream.available() to always return 0 in the case of eof, regardless of impl:

      === cut ===
      diff -r 65464a307408 src/java.base/share/classes/java/net/SocketInputStream.java
      --- a/src/java.base/share/classes/java/net/SocketInputStream.java Thu Aug 03 18:56:59 2017 +0000
      +++ b/src/java.base/share/classes/java/net/SocketInputStream.java Fri Oct 06 17:03:43 2017 +0100
      @@ -256,7 +256,11 @@
            * @return the number of immediately available bytes
            */
           public int available() throws IOException {
      - return impl.available();
      + if (eof) {
      + return 0;
      + } else {
      + return impl.available();
      + }
           }
       
           /**
      === cut ===

            vtewari Vyom Tewari
            martin Martin Buchholz
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: