org.apache.tools.ant.types

Class Resource

public class Resource extends DataType implements Cloneable, Comparable, ResourceCollection

Describes a "File-like" resource (File, ZipEntry, etc.). This class is meant to be used by classes needing to record path and date/time information about a file, a zip entry or some similar resource (URL, archive in a version control repository, ...).

Since: Ant 1.5.2

See Also: Touchable

Field Summary
protected static intMAGIC
Magic number
static longUNKNOWN_DATETIME
Constant unknown datetime for getLastModified
static longUNKNOWN_SIZE
Constant unknown size
Constructor Summary
Resource()
Default constructor.
Resource(String name)
Only sets the name.
Resource(String name, boolean exists, long lastmodified)
Sets the name, lastmodified flag, and exists flag.
Resource(String name, boolean exists, long lastmodified, boolean directory)
Sets the name, lastmodified flag, exists flag, and directory flag.
Resource(String name, boolean exists, long lastmodified, boolean directory, long size)
Sets the name, lastmodified flag, exists flag, directory flag, and size.
Method Summary
Objectclone()
Clone this Resource.
intcompareTo(Object other)
Delegates to a comparison of names.
booleanequals(Object other)
Implement basic Resource equality.
InputStreamgetInputStream()
Get an InputStream for the Resource.
longgetLastModified()
Tells the modification time in milliseconds since 01.01.1970 .
protected static intgetMagicNumber(byte[] seed)
Create a "magic number" for use in hashCode calculations.
StringgetName()
Name attribute will contain the path of a file relative to the root directory of its fileset or the recorded path of a zip entry.
OutputStreamgetOutputStream()
Get an OutputStream for the Resource.
longgetSize()
Get the size of this Resource.
inthashCode()
Get the hash code for this Resource.
booleanisDirectory()
Tells if the resource is a directory.
booleanisExists()
The exists attribute tells whether a resource exists.
booleanisFilesystemOnly()
Fulfill the ResourceCollection contract.
Iteratoriterator()
Fulfill the ResourceCollection contract.
voidsetDirectory(boolean directory)
Set the directory attribute.
voidsetExists(boolean exists)
Set the exists attribute.
voidsetLastModified(long lastmodified)
Set the last modification attribute.
voidsetName(String name)
Set the name of this Resource.
voidsetRefid(Reference r)
Overrides the base version.
voidsetSize(long size)
Set the size of this Resource.
intsize()
Fulfill the ResourceCollection contract.
StringtoLongString()
Get a long String representation of this Resource.
StringtoString()
Get the string representation of this Resource.

Field Detail

MAGIC

protected static final int MAGIC
Magic number

UNKNOWN_DATETIME

public static final long UNKNOWN_DATETIME
Constant unknown datetime for getLastModified

UNKNOWN_SIZE

public static final long UNKNOWN_SIZE
Constant unknown size

Constructor Detail

Resource

public Resource()
Default constructor.

Resource

public Resource(String name)
Only sets the name.

This is a dummy, used for not existing resources.

Parameters: name relative path of the resource. Expects "/" to be used as the directory separator.

Resource

public Resource(String name, boolean exists, long lastmodified)
Sets the name, lastmodified flag, and exists flag.

Parameters: name relative path of the resource. Expects "/" to be used as the directory separator. exists if true, this resource exists. lastmodified the last modification time of this resource.

Resource

public Resource(String name, boolean exists, long lastmodified, boolean directory)
Sets the name, lastmodified flag, exists flag, and directory flag.

Parameters: name relative path of the resource. Expects "/" to be used as the directory separator. exists if true the resource exists lastmodified the last modification time of the resource directory if true, this resource is a directory

Resource

public Resource(String name, boolean exists, long lastmodified, boolean directory, long size)
Sets the name, lastmodified flag, exists flag, directory flag, and size.

Parameters: name relative path of the resource. Expects "/" to be used as the directory separator. exists if true the resource exists lastmodified the last modification time of the resource directory if true, this resource is a directory size the size of this resource.

Method Detail

clone

public Object clone()
Clone this Resource.

Returns: copy of this.

compareTo

public int compareTo(Object other)
Delegates to a comparison of names.

Parameters: other the object to compare to.

Returns: a negative integer, zero, or a positive integer as this Resource is less than, equal to, or greater than the specified Resource.

Since: Ant 1.6

equals

public boolean equals(Object other)
Implement basic Resource equality.

Parameters: other the object to check against.

Returns: true if the specified Object is equal to this Resource.

Since: Ant 1.7

getInputStream

public InputStream getInputStream()
Get an InputStream for the Resource.

Returns: an InputStream containing this Resource's content.

Throws: IOException if unable to provide the content of this Resource as a stream. UnsupportedOperationException if InputStreams are not supported for this Resource type.

Since: Ant 1.7

getLastModified

public long getLastModified()
Tells the modification time in milliseconds since 01.01.1970 .

Returns: 0 if the resource does not exist to mirror the behavior of java.io.File File.

getMagicNumber

protected static int getMagicNumber(byte[] seed)
Create a "magic number" for use in hashCode calculations.

Parameters: seed byte[] to seed with.

Returns: a magic number as int.

getName

public String getName()
Name attribute will contain the path of a file relative to the root directory of its fileset or the recorded path of a zip entry.

example for a file with fullpath /var/opt/adm/resource.txt in a file set with root dir /var/opt it will be adm/resource.txt.

"/" will be used as the directory separator.

Returns: the name of this resource.

getOutputStream

public OutputStream getOutputStream()
Get an OutputStream for the Resource.

Returns: an OutputStream to which content can be written.

Throws: IOException if unable to provide the content of this Resource as a stream. UnsupportedOperationException if OutputStreams are not supported for this Resource type.

Since: Ant 1.7

getSize

public long getSize()
Get the size of this Resource.

Returns: the size, as a long, 0 if the Resource does not exist (for compatibility with java.io.File), or UNKNOWN_SIZE if not known.

Since: Ant 1.6.3

hashCode

public int hashCode()
Get the hash code for this Resource.

Returns: hash code as int.

Since: Ant 1.7

isDirectory

public boolean isDirectory()
Tells if the resource is a directory.

Returns: boolean flag indicating if the resource is a directory.

isExists

public boolean isExists()
The exists attribute tells whether a resource exists.

Returns: true if this resource exists.

isFilesystemOnly

public boolean isFilesystemOnly()
Fulfill the ResourceCollection contract.

Returns: whether this Resource is a FileResource.

Since: Ant 1.7

iterator

public Iterator iterator()
Fulfill the ResourceCollection contract.

Returns: an Iterator of Resources.

Since: Ant 1.7

setDirectory

public void setDirectory(boolean directory)
Set the directory attribute.

Parameters: directory if true, this resource is a directory.

setExists

public void setExists(boolean exists)
Set the exists attribute.

Parameters: exists if true, this resource exists.

setLastModified

public void setLastModified(long lastmodified)
Set the last modification attribute.

Parameters: lastmodified the modification time in milliseconds since 01.01.1970.

setName

public void setName(String name)
Set the name of this Resource.

Parameters: name relative path of the resource. Expects "/" to be used as the directory separator.

setRefid

public void setRefid(Reference r)
Overrides the base version.

Parameters: r the Reference to set.

setSize

public void setSize(long size)
Set the size of this Resource.

Parameters: size the size, as a long.

Since: Ant 1.6.3

size

public int size()
Fulfill the ResourceCollection contract.

Returns: the size of this ResourceCollection.

Since: Ant 1.7

toLongString

public final String toLongString()
Get a long String representation of this Resource. This typically should be the value of toString() prefixed by a type description.

Returns: this Resource formatted as a long String.

Since: Ant 1.7

toString

public String toString()
Get the string representation of this Resource.

Returns: this Resource formatted as a String.

Since: Ant 1.7