Class URLStreams

java.lang.Object
org.codehaus.groovy.util.URLStreams

public class URLStreams extends Object
Utilities for opening URL connections and reading last-modified times with Groovy-specific defaults.

Caching is disabled so file: and jar: URLs do not pin a file handle. On Windows an open handle prevents the file from being deleted or replaced.

  • Method Details

    • openUncachedStream

      public static InputStream openUncachedStream(URL url) throws IOException
      Opens an InputStream reading from the given URL without caching the connection. This prevents file descriptor leaks when reading from file-system URLs.
      Parameters:
      url - the URL to connect to
      Returns:
      an input stream reading from the URL connection
      Throws:
      IOException
    • getLastModified

      public static long getLastModified(URL url) throws IOException
      Last-modified time of url for source-freshness checks used by GroovyClassLoader and ClassNodeResolver.

      file: URLs use File.lastModified() because URLConnection.getLastModified() often reports -1. The path mapping includes the historical Windows form file://c|/... where | stood for :. Other protocols use getUncachedLastModified(URL).

      Parameters:
      url - the source URL
      Returns:
      the last-modified time in milliseconds since the epoch, or 0 if it is not known
      Throws:
      IOException - if a non-file: URL cannot be opened
    • getUncachedLastModified

      public static long getUncachedLastModified(URL url) throws IOException
      Returns URLConnection.getLastModified() for url without caching the connection, then closes it.

      jar: URLs otherwise keep a cached JarFile open after URLConnection.getInputStream() is closed, which on Windows locks the JAR until the JVM exits. A failure to open the stream (missing JAR entry, unreachable URL) is propagated so callers can treat the source as unreadable; a failure to close after a successful open is ignored because last-modified was already obtained.

      Parameters:
      url - the URL whose last-modified time is required
      Returns:
      the last-modified time in milliseconds since the epoch, or 0 if it is not known
      Throws:
      IOException - if the connection or stream cannot be opened