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

SocketInputStream.available() should check for eof

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 9, 12
    • Fix Version/s: 12
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b16
    • Verification:
      Verified

      Description

      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 ===

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: