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

provide lastModified information for file URLs

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.1.3, 1.1.6, 1.2.2, 1.3.0_01
    • core-libs
    • beta2
    • generic, x86, sparc
    • generic, solaris_2.6, windows_nt



      Name: bk70084 Date: 02/04/98


      It would be useful if the URLConnection.getLastModified() method would return information if
      the URL from which it was created was a file URL of the form "file:/tmp/myfile".

      Currently the URLConnection.getLastModified() method always returns 0 on Solaris. This can
      be seen by running the following application


      //---------------Main.java----------------------
      import java.net.*;
      import java.io.*;

      public class Main
          {
          public static void main(String[] argv)
              {
              try
                  {
                  URL url = new URL("file:/tmp/t");
                  URLConnection uc = url.openConnection();
                  System.out.println("uc <"+uc+">");
                  System.out.println("uc.getContent <"+uc.getContent()+">");
                  System.out.println("uc.getLastModified <"+uc.getLastModified()+">");
                  }
                  catch (Throwable t) {t.printStackTrace();}
              }
          }

      Create a /tmp/t


      uc <sun.net.www.protocol.file.FileURLConnection:file:/tmp/t>
      uc.getContent <java.io.BufferedInputStream@1dae0faf>
      uc.getLastModified <0>
      (Review ID: 24476)
      ======================================================================

      Name: rlT66838 Date: 08/23/99


      URL url = new URL("file:/somefile");
      URLConnection con = url.openConnection();
      System.out.println(con.getLastModified());

      this won't result in the correct date, but 1.1.1970.
      same with a URL describing a file in a jar file (e.g. jar:file:/somejarfile.jar!/somefile)


      Of course a file doesn't have a header, but think for a minute and then tell me, if it is desirable being able to access the LastModified-date without having to
      use the java.io.File-Object?! Instead you could use URLConnection even, when you don't know which protocol you access your resource through. God! There's
      already a method to access LastModified - why doesn't it work?

      Take this case: For some reason I have to access a resource through an URL. It is unknown which protocol this URL denotes. But it is important that the
      referenced resource was not changed since a certain date. The way it works now looks something like this:

      boolean isModified(URL aURL, long aTime) {
          if ("file".equals(aURL.getProtocol())) {
              File theFile = new File(aURL.getFile());
              return theFile.lastModified > aTime;
          }
          else {
              URLConnection con = aURL.openConnection();
              long theLastModified = con.getLastModified();
             return theLastModified <= 0 || theLastModified > aTime;
          }
      }

      }

      Now tell me: Isn't this senseless?

      URL handling should be more abstract than protocols. Discussing whether there are "real" headers or not misses the point. It's information we are talking about.
      The way to get it (headers or file attributes) is irrelevant. That's why we have such nice abstractions like URL and URLConnection in the first place!
      Seen this way you might agree that even the Date header could be handled correctly, i.e. returning the time of access.
      Of course you run into trouble if you try to be consistent and want to produce artificial headers in order to being able to provide the information from
      getLastModified() in form of headers, too. But do you really have to do that?

      Cheers,

      -hendrik
      (Review ID: 93185)
      ======================================================================

            alanb Alan Bateman
            bklocksunw Brian Klock (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: