net.sf.jguiraffe.locators
Class ByteArrayLocator

java.lang.Object
  extended by net.sf.jguiraffe.locators.AbstractLocator
      extended by net.sf.jguiraffe.locators.AbstractStreamLocator
          extended by net.sf.jguiraffe.locators.ByteArrayLocator
All Implemented Interfaces:
Locator

public 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 156 2009-03-03 21:04:47Z oheger $
Author:
Oliver Heger

Method Summary
protected  java.net.URL createURL(java.net.URLStreamHandler streamHandler)
          Creates a URL representing the data of this locator.
 boolean equals(java.lang.Object obj)
          Compares this object with another one.
 java.io.InputStream getInputStream()
          Returns an input stream for the data of this locator.
static ByteArrayLocator getInstance(byte[] data)
          Creates an instance of ByteArrayLocator that is initialized with the specified data.
static ByteArrayLocator getInstance(java.lang.String data)
          Creates an instance of ByteArrayLocator that is initialized with the data of the specified string.
 int hashCode()
          Returns a hash code for this object.
 java.lang.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
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

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:
java.lang.IllegalArgumentException - if the data is null

getInstance

public static ByteArrayLocator getInstance(java.lang.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:
java.lang.IllegalArgumentException - if the data is null

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.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:
java.io.IOException - if an error occurs

equals

public boolean equals(java.lang.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 java.lang.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 java.lang.Object
Returns:
a hash code for this object

toString

public java.lang.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 java.lang.Object
Returns:
a string for this object

createURL

protected java.net.URL createURL(java.net.URLStreamHandler streamHandler)
                          throws java.net.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:
java.net.MalformedURLException - if the URL cannot be created


Copyright © 2009 The JGUIraffe Team. All Rights Reserved.