Class ByteArrayLocator

  • All Implemented Interfaces:
    Locator

    public final class ByteArrayLocator
    extends AbstractStreamLocator

    A specialized Locator implementation that provides access to data stored in memory as a byte array.

    This locator can be initialized either with a byte array or with a string. This data is kept in memory. The locator returns a stream or an URL providing access to exactly this data.

    This class is especially useful for providing access to small amounts of data to clients that can deal with locators. It can also be of value for unit tests supporting an easy way of defining test data.

    Instances are created using one of the static getInstance() factory methods. This class is thread-safe and can be used concurrently by multiple threads. Each invocation of getInputStream() returns a new stream instance initialized with the data of this locator.

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

      • getInstance

        public static ByteArrayLocator getInstance​(byte[] data)
        Creates an instance of ByteArrayLocator that is initialized with the specified data.
        Parameters:
        data - the data for the locator (must not be null)
        Returns:
        the locator instance pointing to this data
        Throws:
        IllegalArgumentException - if the data is null
      • getInstance

        public static ByteArrayLocator getInstance​(String data)
        Creates an instance of ByteArrayLocator that is initialized with the data of the specified string.
        Parameters:
        data - the data for the locator (must not be null)
        Returns:
        the locator instance pointing to this data
        Throws:
        IllegalArgumentException - if the data is null
      • getInputStream

        public InputStream getInputStream()
                                   throws IOException
        Returns an input stream for the data of this locator. This implementation creates a stream that allows reading the data this locator points to in memory.
        Specified by:
        getInputStream in interface Locator
        Overrides:
        getInputStream in class AbstractLocator
        Returns:
        the input stream for this locator
        Throws:
        IOException - if an error occurs
      • equals

        public boolean equals​(Object obj)
        Compares this object with another one. Two instances of this class are considered equal if and only if the data arrays they point to are equal.
        Overrides:
        equals in class Object
        Parameters:
        obj - the object to compare to
        Returns:
        a flag whether these objects are equal
      • hashCode

        public int hashCode()
        Returns a hash code for this object.
        Overrides:
        hashCode in class Object
        Returns:
        a hash code for this object
      • toString

        public String toString()
        Returns a string representation for this object. This string will contain (at least parts of) the data this locator points to. If the data array contains too many elements, only an excerpt of the data is printed out.
        Overrides:
        toString in class Object
        Returns:
        a string for this object
      • createURL

        protected URL createURL​(URLStreamHandler streamHandler)
                         throws MalformedURLException
        Creates a URL representing the data of this locator. This implementation creates a URL with the protocol 'data' and a hash value of the data as host.
        Specified by:
        createURL in class AbstractStreamLocator
        Parameters:
        streamHandler - the stream handler to use for this URL
        Returns:
        the URL representing the data of this locator
        Throws:
        MalformedURLException - if the URL cannot be created