net.sf.jasperreports.engine.util

## Class JRLoader

- java.lang.Object
- - net.sf.jasperreports.engine.util.JRLoader

-     public final class JRLoader
      extends java.lang.Object

  Utility class that helps load serialized objects found in various locations such as files, URLs, and input streams.

  Many JasperReports processes, like report compilation, report filling and exporting, often work with serialized objects. Sometimes it is useful to manually load those serialized objects before submitting them to the desired JasperReport process.

  The most interesting method exposed by this class is `loadObjectFromLocation(String)`. When calling this method to load an object from the supplied location, the program first tries to interpret the location as a valid URL. If this fails, then the program assumes that the supplied location is the name of a file on disk and tries to read from it. If no file is found at that location, it will try to locate a resource through the classpath that would correspond to the location. Only after this third try fails an exception is thrown.

  Version:
    $Id: JRLoader.java 7017 2014-04-15 10:18:40Z shertage $
  Author:
    Teodor Danciu (teodord@users.sourceforge.net)

- - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected static void` | `collectResources(java.lang.String resourceName, java.lang.ClassLoader classLoader, java.util.Map<java.net.URL,ClassLoaderResource> resources)`
    `protected static void` | `collectResources(java.lang.String resourceName, java.lang.ClassLoader classLoader, java.util.Set<java.net.URL> resources)`
    `static java.util.List<ClassLoaderResource>` | `getClassLoaderResources(java.lang.String resource)` Scans the context classloader and the classloader of this class for all resources that have a specified name, and returns a list of `ClassLoaderResource` objects for the found resources.
    `static java.io.InputStream` | `getFileInputStream(java.lang.String filename)` Tries to open a file for reading.
    `static java.io.InputStream` | `getInputStream(java.io.File file)`
    `static java.io.InputStream` | `getInputStream(java.net.URL url)`
    `static java.io.InputStream` | `getLocationInputStream(java.lang.String location)` Tries to open an input stream for a location.
    `static java.net.URL` | `getResource(java.lang.String resource)` Returns the resource URL for a specified resource name.
    `static java.io.InputStream` | `getResourceInputStream(java.lang.String resource)` Tries to open an input stream for a resource.
    `static java.util.List<java.net.URL>` | `getResources(java.lang.String resource)` Scans the context classloader and the classloader of this class for all resources that have a specified name, and returns a list of `URL`s for the found resources.
    `static java.io.InputStream` | `getURLInputStream(java.lang.String spec)` Tries to open an input stream for an URL.
    `static byte[]` | `loadBytes(java.io.File file)`
    `static byte[]` | `loadBytes(java.io.InputStream is)`
    `static byte[]` | `loadBytes(java.net.URL url)`
    `static byte[]` | `loadBytesFromResource(java.lang.String resourceName)`
    `static byte[]` | `loadBytesFromResource(java.lang.String resourceName, java.lang.ClassLoader classLoader)`
    `static JasperPrint` | `loadJasperPrint(java.io.File file, JRVirtualizer virtualizer)` Loads a JasperPrint object from a file, optionally using a virtualizer for the object.
    `static JasperPrint` | `loadJasperPrint(java.io.InputStream is, JRVirtualizer virtualizer)` Loads a JasperPrint object from a stream, optionally using a virtualizer for the object.
    `static JasperPrint` | `loadJasperPrint(java.net.URL url, JRVirtualizer virtualizer)` Loads a JasperPrint object from a URL, optionally using a virtualizer for the object.
    `static JasperPrint` | `loadJasperPrintFromFile(java.lang.String fileName, JRVirtualizer virtualizer)` Loads a JasperPrint object from a file, optionally using a virtualizer for the object.
    `static java.lang.Object` | `loadObject(java.io.File file)`
    `static java.lang.Object` | `loadObject(java.io.InputStream is)`
    `static java.lang.Object` | `loadObject(JasperReportsContext jasperReportsContext, java.io.File file)`
    `static java.lang.Object` | `loadObject(JasperReportsContext jasperReportsContext, java.io.InputStream is)`
    `static java.lang.Object` | `loadObject(JasperReportsContext jasperReportsContext, java.net.URL url)`
    `static java.lang.Object` | `loadObject(java.net.URL url)`
    `static java.lang.Object` | `loadObjectFromFile(java.lang.String fileName)`
    `static java.io.InputStream` | `loadToMemoryInputStream(java.io.InputStream is)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### loadObjectFromFile

          public static java.lang.Object loadObjectFromFile(java.lang.String fileName)
                                                     throws JRException

      Throws:
        `JRException`

    - #### loadObject

          public static java.lang.Object loadObject(java.io.File file)
                                             throws JRException

      Throws:
        `JRException`

    - #### loadObject

          public static java.lang.Object loadObject(JasperReportsContext jasperReportsContext,
                                    java.io.File file)
                                             throws JRException

      Throws:
        `JRException`

    - #### loadObject

          public static java.lang.Object loadObject(java.net.URL url)
                                             throws JRException

      Throws:
        `JRException`

    - #### loadObject

          public static java.lang.Object loadObject(JasperReportsContext jasperReportsContext,
                                    java.net.URL url)
                                             throws JRException

      Throws:
        `JRException`

    - #### loadObject

          public static java.lang.Object loadObject(java.io.InputStream is)
                                             throws JRException

      Throws:
        `JRException`

    - #### loadObject

          public static java.lang.Object loadObject(JasperReportsContext jasperReportsContext,
                                    java.io.InputStream is)
                                             throws JRException

      Throws:
        `JRException`

    - #### getInputStream

          public static java.io.InputStream getInputStream(java.io.File file)
                                                    throws JRException

      Throws:
        `JRException`

    - #### getInputStream

          public static java.io.InputStream getInputStream(java.net.URL url)
                                                    throws JRException

      Throws:
        `JRException`

    - #### loadBytes

          public static byte[] loadBytes(java.io.File file)
                                  throws JRException

      Throws:
        `JRException`

    - #### loadBytes

          public static byte[] loadBytes(java.net.URL url)
                                  throws JRException

      Throws:
        `JRException`

    - #### loadBytes

          public static byte[] loadBytes(java.io.InputStream is)
                                  throws JRException

      Throws:
        `JRException`

    - #### loadToMemoryInputStream

          public static java.io.InputStream loadToMemoryInputStream(java.io.InputStream is)
                                                             throws JRException

      Throws:
        `JRException`

    - #### loadBytesFromResource

          public static byte[] loadBytesFromResource(java.lang.String resourceName)
                                              throws JRException

      Throws:
        `JRException`

    - #### loadBytesFromResource

          public static byte[] loadBytesFromResource(java.lang.String resourceName,
                                     java.lang.ClassLoader classLoader)
                                              throws JRException

      Throws:
        `JRException`

    - #### getLocationInputStream

          public static java.io.InputStream getLocationInputStream(java.lang.String location)
                                                            throws JRException

      Tries to open an input stream for a location.

      The method tries to interpret the location as a file name, a resource name or an URL. If any of these succeed, an input stream is created and returned.

      Parameters:
        `location` - the location
      Returns:
        an input stream if the location is an existing file name, a resource name on the classpath or an URL or `null` otherwise.
      Throws:
        `JRException`

    - #### getFileInputStream

          public static java.io.InputStream getFileInputStream(java.lang.String filename)
                                                        throws JRException

      Tries to open a file for reading.
      Parameters:
        `filename` - the file name
      Returns:
        an input stream for the file or `null` if the file was not found
      Throws:
        `JRException`

    - #### getResourceInputStream

          public static java.io.InputStream getResourceInputStream(java.lang.String resource)

      Tries to open an input stream for a resource.
      Parameters:
        `resource` - the resource name
      Returns:
        an input stream for the resource or `null` if the resource was not found

    - #### getResources

          public static java.util.List<java.net.URL> getResources(java.lang.String resource)

      Scans the context classloader and the classloader of this class for all resources that have a specified name, and returns a list of `URL`s for the found resources.
      Parameters:
        `resource` - the resource names
      Returns:
        a list of `URL`s of resources with the specified name; the list is empty if no resources have been found for the name
      See Also:
        `ClassLoader.getResources(String)`

    - #### collectResources

          protected static void collectResources(java.lang.String resourceName,
                              java.lang.ClassLoader classLoader,
                              java.util.Set<java.net.URL> resources)

    - #### getClassLoaderResources

          public static java.util.List<ClassLoaderResource> getClassLoaderResources(java.lang.String resource)

      Scans the context classloader and the classloader of this class for all resources that have a specified name, and returns a list of `ClassLoaderResource` objects for the found resources.

      The returned list contains the URLs of the resources, and for each resource the highest classloader in the classloader hierarchy on which the resource was found.

      Parameters:
        `resource` - the resource names
      Returns:
        a list of resources with the specified name; the list is empty if no resources have been found for the name
      See Also:
        `ClassLoader.getResources(String)`

    - #### collectResources

          protected static void collectResources(java.lang.String resourceName,
                              java.lang.ClassLoader classLoader,
                              java.util.Map<java.net.URL,ClassLoaderResource> resources)

    - #### getResource

          public static java.net.URL getResource(java.lang.String resource)

      Returns the resource URL for a specified resource name.
      Parameters:
        `resource` - the resource name
      Returns:
        the URL of the resource having the specified name, or `null` if none found
      See Also:
        `ClassLoader.getResource(String)`

    - #### getURLInputStream

          public static java.io.InputStream getURLInputStream(java.lang.String spec)
                                                       throws JRException

      Tries to open an input stream for an URL.
      Parameters:
        `spec` - the string to parse as an URL
      Returns:
        an input stream for the URL or null if `spec` is not a valid URL
      Throws:
        `JRException`

    - #### loadJasperPrintFromFile

          public static JasperPrint loadJasperPrintFromFile(java.lang.String fileName,
                                            JRVirtualizer virtualizer)
                                                     throws JRException

      Loads a JasperPrint object from a file, optionally using a virtualizer for the object.
      Parameters:
        `fileName` - the file name
        `virtualizer` - the virtualizer
      Returns:
        a JasperPrint object
      Throws:
        `JRException`
      See Also:
        `JRVirtualizationHelper.setThreadVirtualizer(JRVirtualizer)`

    - #### loadJasperPrint

          public static JasperPrint loadJasperPrint(java.io.File file,
                                    JRVirtualizer virtualizer)
                                             throws JRException

      Loads a JasperPrint object from a file, optionally using a virtualizer for the object.
      Parameters:
        `file` - the file
        `virtualizer` - the virtualizer
      Returns:
        a JasperPrint object
      Throws:
        `JRException`
      See Also:
        `JRVirtualizationHelper.setThreadVirtualizer(JRVirtualizer)`

    - #### loadJasperPrint

          public static JasperPrint loadJasperPrint(java.net.URL url,
                                    JRVirtualizer virtualizer)
                                             throws JRException

      Loads a JasperPrint object from a URL, optionally using a virtualizer for the object.
      Parameters:
        `url` - the URL
        `virtualizer` - the virtualizer
      Returns:
        a JasperPrint object
      Throws:
        `JRException`
      See Also:
        `JRVirtualizationHelper.setThreadVirtualizer(JRVirtualizer)`

    - #### loadJasperPrint

          public static JasperPrint loadJasperPrint(java.io.InputStream is,
                                    JRVirtualizer virtualizer)
                                             throws JRException

      Loads a JasperPrint object from a stream, optionally using a virtualizer for the object.
      Parameters:
        `is` - the stream
        `virtualizer` - the virtualizer
      Returns:
        a JasperPrint object
      Throws:
        `JRException`
      See Also:
        `JRVirtualizationHelper.setThreadVirtualizer(JRVirtualizer)`

© 2001-2010 Jaspersoft Corporation www.jaspersoft.com