oranjestad.commons.io
Class URLResource

java.lang.Object
  extended by oranjestad.commons.io.AbstractResource
      extended by oranjestad.commons.io.URLResource
All Implemented Interfaces:
Resource
Direct Known Subclasses:
ClassPathResource

public class URLResource
extends AbstractResource

Resource over a URL object. URL's are typically used as read only references, but URLs can supported write semantics. This resource will attempt to support writes if the underlying protocol supports them.

Usage


 URLResource resource = new URLResource( new URL("..."));
 

Notes

This resource supports ant style property expansion. An ant property reference ${property name} will be expanded before converting strings to URLs.

Since:
1.3
Author:
Bryant Harris

Constructor Summary
protected URLResource()
          Internal constructor used by subclasses.
  URLResource(java.lang.String urlString)
          Creates a URL resource based on the supplied URL string.
  URLResource(java.net.URL theURL)
          Creates a URL resource over the supplied URL.
 
Method Summary
 boolean canReopen()
          Flag that lets you check if this resource supports reopening of the InputStream.
 boolean equals(java.lang.Object o)
           
 boolean exists()
          Attempts to open a connection to the URL, if the connection is succesful then the resource is considered to exist.
 java.io.InputStream getInputStream()
          An open input stream at the beginning of the resource.
 java.io.OutputStream getOutputStream()
          Attempts to obtain an output stream to the resource identified by the URL.
 int hashCode()
           
 boolean isReadOnly()
          Flag that lets you check if this resource supports writes.
 void setReadOnly(boolean flag)
          Sets the read only flag.
 java.lang.String toString()
           
 
Methods inherited from class oranjestad.commons.io.AbstractResource
expand
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

URLResource

public URLResource(java.net.URL theURL)
Creates a URL resource over the supplied URL.

Parameters:
theURL - The URL to read/write from.
Since:
1.3

URLResource

public URLResource(java.lang.String urlString)
            throws java.net.MalformedURLException
Creates a URL resource based on the supplied URL string.

Parameters:
urlString - The URL string (can include ant style properties).
Throws:
java.net.MalformedURLException - If the string specifies an unknown protocol
Since:
1.3

URLResource

protected URLResource()
Internal constructor used by subclasses.

Since:
1.3
Method Detail

setReadOnly

public void setReadOnly(boolean flag)

Sets the read only flag. This flag in no way alters the underlying URL object but instead instructs this resource to not attempt ever write to this URL.

When true, this flag delegates to the actual URL protocol, which itself may not support writes.

Parameters:
flag - Whether or not to attempt writes (true no writes, false will attempt writes).
Since:
1.3

getInputStream

public java.io.InputStream getInputStream()
                                   throws java.io.IOException
Description copied from interface: Resource

An open input stream at the beginning of the resource. If the resource doesn not support reopening, this method might throw an IllegalStateException. Otherwise subsequent calls to this method return a new input stream set to the beginning of the data stream.

Returns:
The input stream over the URL.
Throws:
java.io.IOException - If an I/O related error occurs.
Since:
1.3

getOutputStream

public java.io.OutputStream getOutputStream()
                                     throws java.io.IOException,
                                            java.lang.IllegalStateException
Attempts to obtain an output stream to the resource identified by the URL. This method might fail if the underlying URL protocol does not support writes.

Returns:
An open output stream (only at the beginning if the resource has not been previously opened and remains unclosed).
Throws:
java.io.IOException - If an I/O related error occurs.
java.lang.IllegalStateException

isReadOnly

public boolean isReadOnly()
Description copied from interface: Resource

Flag that lets you check if this resource supports writes.

Returns:
true if this resource is configured to be read only, or if the underlying protocol indicates it is not read only.
Since:
1.3

canReopen

public boolean canReopen()
Description copied from interface: Resource

Flag that lets you check if this resource supports reopening of the InputStream. Some resources can only be read once.

Returns:
true
Since:
1.3

exists

public boolean exists()
Attempts to open a connection to the URL, if the connection is succesful then the resource is considered to exist.

Returns:
true if the associated url can be connected to, otherwise false.
Since:
1.3

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
The external form of the URL.
Since:
1.3

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object
Returns:
The URL's hashcode.
Since:
1.3

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object
Parameters:
o - The object to compare against.
Returns:
true if o is a URLResource over an equal URL object, otherwise false.
Since:
1.3


Copyright © 2008. All Rights Reserved.