Class ByteArrayLocator
- java.lang.Object
-
- net.sf.jguiraffe.locators.AbstractLocator
-
- net.sf.jguiraffe.locators.AbstractStreamLocator
-
- net.sf.jguiraffe.locators.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 ofgetInputStream()
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 Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected URL
createURL(URLStreamHandler streamHandler)
Creates a URL representing the data of this locator.boolean
equals(Object obj)
Compares this object with another one.InputStream
getInputStream()
Returns an input stream for the data of this locator.static ByteArrayLocator
getInstance(byte[] data)
Creates an instance ofByteArrayLocator
that is initialized with the specified data.static ByteArrayLocator
getInstance(String data)
Creates an instance ofByteArrayLocator
that is initialized with the data of the specified string.int
hashCode()
Returns a hash code for this object.String
toString()
Returns a string representation for this object.-
Methods inherited from class net.sf.jguiraffe.locators.AbstractStreamLocator
getURL
-
Methods inherited from class net.sf.jguiraffe.locators.AbstractLocator
getFile
-
-
-
-
Method Detail
-
getInstance
public static ByteArrayLocator getInstance(byte[] data)
Creates an instance ofByteArrayLocator
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 ofByteArrayLocator
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 interfaceLocator
- Overrides:
getInputStream
in classAbstractLocator
- 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.
-
hashCode
public int hashCode()
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.
-
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 classAbstractStreamLocator
- 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
-
-