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

Reader around URLConnection throws NullpointerException on close()

    XMLWordPrintable

Details

    • x86
    • windows_95

    Description



      Name: nt126004 Date: 01/15/2002


      FULL PRODUCT VERSION :
      java version "1.4.0-beta3"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
      Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)


      FULL OPERATING SYSTEM VERSION :
      Windows 95. [Version 4.00.1111]

      ADDITIONAL OPERATING SYSTEMS :
      Solaris 2.8


      A DESCRIPTION OF THE PROBLEM :
      If an InputStream from a URLConnection as provided by 2 supplied testcases is read then a NullPointer Exception is thrown on close(). This only happens if a page is read that has an incorrect content-length in the header. In comparison Java 1.1 exhibits correct behavior.


      REGRESSION. Last worked in version 1.1.8

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1.Start any servlet engine, any JSDK with BugServlet installed.
      2.Run Test
      3.check console output of Test

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      In the console window, you should see:
      java.version = 1.4.0-beta3
      reading
      reading
      reading
      After while() loop
      After close()


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.version = 1.4.0-beta3
      reading
      After while() loop
      Exception in thread "main" java.lang.NullPointerException
              at sun.net.www.http.KeepAliveStream.close(Unknown Source)
              at java.io.FilterInputStream.close(Unknown Source)
              at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.close(Unknown
      Source)
              at sun.nio.cs.StreamDecoder$CharsetSD.implClose(Unknown Source)
              at sun.nio.cs.StreamDecoder.close(Unknown Source)
              at java.io.InputStreamReader.close(Unknown Source)
              at java.io.BufferedReader.close(Unknown Source)
              at Test.main(Test.java:144)


      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      First the test servlet BugServlet that provides the ouptput
      which
      is input for this testcase:


      import java.io.*;
      import java.util.*;

      import javax.servlet.*;
      import javax.servlet.http.*;

      public class BugServlet extends HttpServlet {

          public void service (HttpServletRequest req,
      HttpServletResponse res)
          throws ServletException, IOException
          {
          
          String
      outputString="<HTML><BODY><H1>Hello</H1></BODY></HTML>";
          res.setContentType("text/html");
          res.setContentLength(outputString.length()+5);
          PrintWriter out = res.getWriter ();
          out.print(outputString);
          out.flush();
          out.close();
          }//End service()

      }//End class BugServlet


      The above servlet creates an output whose content-length
      header is set incorrectly
      (5 bytes too long in this case).
      However, if the corresponding URL is opened from within a
      web browser,
      the expected output "Hello" is displayed.

      Next the class Test that demonstrates the error.

      With Java 1.4.0-beta3, it throws NullPointerexception.
      With Java 1.3, it freezes on startup with 100% CPU utilization.
      With Java 1.1, it runs ok (takes a little while).


      The URL:

      http://localhost/servlet/BugServlet

      assumes that a servlet engine is running on the local
      machine on port 80.
      Please feel free to adapt it to your requirements.

      The URL

      http://www.google.com/addurl?q=http%3A%2F%2Fwww.javasoft.com

      is provided as a REAL WORLD example (subject to change).
      It causes the same lock-up bahaviour for apparently the same
      reason.


      The error is particularily critical for Java based web
      crawler type of applications.

      */



      import java.net.*;
      import java.io.*;

      public class URLTest {

          static public void main(String args[]) {
              
              System.out.println("java.version =
      "+System.getProperty("java.version"));

              try{
                  //Only "get" methot supported on this URL
      //The following google URL still fails on 2001-12-30
                  URL submURLObj = new
      URL("http://www.google.com/addurl?q=http%3A%2F%2Fwww.javasoft.com");
      //The following servlet URL always fails with the supplied
      test servlet
      // URL submURLObj = new
      URL("http://localhost/servlet/BugServlet");
                  URLConnection conn = submURLObj.openConnection();
          
                  BufferedReader buf_reader = new BufferedReader(
                                  new
      InputStreamReader(conn.getInputStream()));
          
                  StringBuffer returnBuf = new StringBuffer();
                  String str = null;
                  while( (str=buf_reader.readLine()) != null){
                      System.out.println("reading");
                  }
          
                  System.out.println("After while() loop");
                  // NullPointerException on close() below:
                  buf_reader.close();
                  System.out.println("After close()");

              }catch(MalformedURLException e1){
                  e1.printStackTrace();
              }
              catch(IOException e2){
                  e2.printStackTrace();
              }
              System.exit(0);
          }//End main()
              
      }//End class Test

      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      Catch NullPointerException on close().
      There may be side effects of this bug (memory leaks etc)
      that I am not aware of and which are not covered by this
      workaround
      (Review ID: 137813)
      ======================================================================

      Attachments

        Issue Links

          Activity

            People

              ywangsunw Yingxian Wang (Inactive)
              nthompsosunw Nathanael Thompson (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: