de.jreality.util
Class Input

java.lang.Object
  extended by de.jreality.util.Input

public final class Input
extends java.lang.Object

Abstraction of an input source, whether given as a file on the local file system, a URL, or as a Java resource. Technically an instance covers an InputStream.

Author:
Steffen Weissman TODO: document this.

Constructor Summary
Input(java.io.File file)
           
Input(java.lang.String description, java.io.InputStream is)
           
Input(java.net.URL url)
           
 
Method Summary
 Input copy()
          Get a fresh version of this Input - i.e.
 java.lang.String getContentAsString()
           
 java.lang.String getDescription()
           
static Input getInput(java.io.File file)
          factory method for creating an Input.
static Input getInput(java.lang.String resourceName)
          searches for the given resource name as follows: if resourceName contains :// we try to load it as a URL if resourceName is an absolute filename the corresponding Input is created resourceName is searched in the classpath resourceName is searched relative to System.getProperty(SystemProperties.JREALITY_DATA) resourceName is searched relative to the current dir
static Input getInput(java.lang.String description, java.io.InputStream in)
          factory method for creating an Input.
static Input getInput(java.net.URL url)
          factory method for creating an Input.
 java.io.InputStream getInputStream()
           
 java.io.Reader getReader()
          Creates a Reader for this Input
 java.io.Reader getReader(java.lang.String encoding)
          Creates a Reader for this Input with given encoding.
 Input getRelativeInput(java.lang.String name)
          Tries to resolve a Resource relative to this Input.
 Input resolveInput(java.lang.String name)
          Tries to create an Input for the given name as follows: if there is a System resource with the given name, this is returned.
 java.io.File toFile()
          returns a File for this Input if possible.
 java.lang.String toString()
           
 java.net.URL toURL()
          returns a URL for this Input if possible.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Input

public Input(java.net.URL url)
      throws java.io.IOException
Throws:
java.io.IOException

Input

public Input(java.io.File file)
      throws java.io.FileNotFoundException
Throws:
java.io.FileNotFoundException

Input

public Input(java.lang.String description,
             java.io.InputStream is)
Method Detail

getInputStream

public java.io.InputStream getInputStream()
Returns:
the InputStream for this Input

getReader

public java.io.Reader getReader()
Creates a Reader for this Input

Returns:
a reader for the covered InputStream

getReader

public java.io.Reader getReader(java.lang.String encoding)
                         throws java.io.UnsupportedEncodingException
Creates a Reader for this Input with given encoding.

Parameters:
encoding - the encoding to use for the created Reader
Returns:
a reader for the covered InputStream with the given encoding
Throws:
java.io.UnsupportedEncodingException

getRelativeInput

public Input getRelativeInput(java.lang.String name)
                       throws java.io.IOException
Tries to resolve a Resource relative to this Input. This works only for Files and URLs.

Parameters:
name - the relative name for the resource.
Returns:
an Input for the relative resource.
Throws:
java.io.IOException - if no such relative resource was found.

resolveInput

public Input resolveInput(java.lang.String name)
                   throws java.io.IOException
Tries to create an Input for the given name as follows:
  • if there is a System resource with the given name, this is returned.
  • if the given name is absolute, the corresponding Input is returned.
  • if the input can be resolved relative to an underlying file or URL, this Input is returned.
  • Last try is to find the resource relative to the current directory.

    Parameters:
    name - the name of the resource.
    Returns:
    an Input for the given name.
    Throws:
    java.io.IOException - if sth. goes wrong.

  • toFile

    public java.io.File toFile()
                        throws java.lang.UnsupportedOperationException
    returns a File for this Input if possible.

    Returns:
    the File of this Input.
    Throws:
    java.lang.UnsupportedOperationException - if this Input is not from a file.

    toURL

    public java.net.URL toURL()
                       throws java.net.MalformedURLException
    returns a URL for this Input if possible.

    Returns:
    the URL of this Input.
    Throws:
    java.net.MalformedURLException - if this Input can't be converted to a URL.

    getDescription

    public java.lang.String getDescription()

    toString

    public java.lang.String toString()
    Overrides:
    toString in class java.lang.Object

    getInput

    public static Input getInput(java.net.URL url)
                          throws java.io.IOException
    factory method for creating an Input.

    Parameters:
    url - the URL to create the Input for.
    Returns:
    an Input that covers the given URL.
    Throws:
    java.io.IOException

    getInput

    public static Input getInput(java.io.File file)
                          throws java.io.IOException
    factory method for creating an Input.

    Parameters:
    file - the file to create the Input for.
    Returns:
    an Input that covers the given File.
    Throws:
    java.io.IOException

    getInput

    public static Input getInput(java.lang.String description,
                                 java.io.InputStream in)
                          throws java.io.IOException
    factory method for creating an Input.

    Parameters:
    in - the InputStream to create the Input for.
    description - a String describing the type of input.
    Returns:
    an Input that covers the given InputStream.
    Throws:
    java.io.IOException

    getInput

    public static Input getInput(java.lang.String resourceName)
                          throws java.io.IOException
    searches for the given resource name as follows:
  • if resourceName contains :// we try to load it as a URL
  • if resourceName is an absolute filename the corresponding Input is created
  • resourceName is searched in the classpath
  • resourceName is searched relative to System.getProperty(SystemProperties.JREALITY_DATA)
  • resourceName is searched relative to the current dir

    Parameters:
    resourceName - the name of the resource to look for
    Returns:
    an Input for the given resource name.
    Throws:
    java.io.IOException

  • copy

    public Input copy()
               throws java.io.IOException
    Get a fresh version of this Input - i.e. used to re-read files. Workes only for files and URLs.

    Returns:
    an Input for the same content, but not-yet used InputStream.
    Throws:
    java.io.IOException

    getContentAsString

    public java.lang.String getContentAsString()
                                        throws java.io.IOException
    Throws:
    java.io.IOException