Class AbstractStreamLocator

  • All Implemented Interfaces:
    Locator
    Direct Known Subclasses:
    ByteArrayLocator

    public abstract class AbstractStreamLocator
    extends AbstractLocator

    An abstract base class for Locator implementations that mainly operate on streams.

    The Locator interface requires that the getURL() method has to be implemented in a meaningful way. However, there are some use cases where data is available only as a stream (e.g. in some binary form), and it is not obvious how a URL can be constructed to point to this data. This class is designed to support such use cases.

    The basic idea is that this class provides a specialized implementation of the abstract java.net.URLStreamHandler class. This implementation can create java.net.URLConnection objects whose getInputStream() method returns the input stream provided by the concrete Locator implementation.

    Concrete subclasses have to implement the getInputStream() method to return the stream they point to. They also have to provide an implementation of the createURL() method. This method is passed a URLStreamHandler object as described above. An implementation of createURL() can create a URL with an arbitrary protocol and other components as it sees fit. As long as the URLStreamHandler is used when creating the URL it is guaranteed that calls to openConnection() or openStream() on the URL object return the stream of the Locator.

    Version:
    $Id: AbstractStreamLocator.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • AbstractStreamLocator

        protected AbstractStreamLocator()
        Creates a new instance of AbstractStreamLocator.
    • Method Detail

      • getURL

        public URL getURL()
        Returns the URL managed by this locator. On first access this implementation delegates to createURL() for creating the URL. Then the URL is cached and directly returned on subsequent requests. Note that a non-blocking algorithm is used for lazily creating the URL. So on concurrent access it may happen that createURL() is invoked multiple times. However, it is guaranteed that this method always returns the same URL.
        Returns:
        the URL managed by this locator
      • createURL

        protected abstract URL createURL​(URLStreamHandler streamHandler)
                                  throws MalformedURLException
        Creates the URL managed by this locator. This method is called by getURL() to initialize the URL. An implementation is free to create whatever URL it likes, but it should use the specified URLStreamHandler. This handler ensures that reading from the URL is delegated to the stream managed by this locator.
        Parameters:
        streamHandler - the stream handler to use when creating the URL
        Returns:
        the URL to be returned by getURL()
        Throws:
        MalformedURLException - if the URL cannot be created