Class Resources


  • @Beta
    @GwtIncompatible
    public final class Resources
    extends 
    Provides utility methods for working with resources in the classpath. Note that even though these methods use parameters, they are usually not appropriate for HTTP or other non-classpath resources.

    All method parameters must be non-null unless documented otherwise.

    Since:
    1.0
    Author:
    Chris Nokleberg, Ben Yu, Colin Decker
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static ByteSource  url)
      Returns a ByteSource that reads from the given URL.
      static CharSource  url,  charset)
      Returns a CharSource that reads from the given URL using the given character set.
      static void  from,  to)
      Copies all bytes from a URL to an output stream.
      static <?> contextClass,  resourceName)
      Given a resourceName that is relative to contextClass, returns a URL pointing to the named resource.
      static  resourceName)
      Returns a URL pointing to resourceName if the resource is found using the .
      static <>  url,  charset)
      Reads all of the lines from a URL.
      static <T> T  url,  charset, LineProcessor<T> callback)
      Streams lines from a URL, stopping when our callback returns false, or we have read all of the lines.
      static byte[]  url)
      Reads all bytes from a URL into a byte array.
      static  url,  charset)
      Reads all characters from a URL into a , using the given character set.
      • Methods inherited from class java.lang.

        , , , , , , , , , ,
    • Method Detail

      • asByteSource

        public static  url)
        Returns a ByteSource that reads from the given URL.
        Since:
        14.0
      • asCharSource

        public static  url,
                                               charset)
        Returns a CharSource that reads from the given URL using the given character set.
        Since:
        14.0
      • toByteArray

        public static byte[]  url)
                                  throws 
        Reads all bytes from a URL into a byte array.
        Parameters:
        url - the URL to read from
        Returns:
        a byte array containing all the bytes from the URL
        Throws:
        - if an I/O error occurs
      • toString

        public static   url,
                                       charset)
                               throws 
        Reads all characters from a URL into a , using the given character set.
        Parameters:
        url - the URL to read from
        charset - the charset used to decode the input stream; see Charsets for helpful predefined constants
        Returns:
        a string containing all the characters from the URL
        Throws:
        - if an I/O error occurs.
      • readLines

        public static <T> T  url,
                                       charset,
                                      LineProcessor<T> callback)
                               throws 
        Streams lines from a URL, stopping when our callback returns false, or we have read all of the lines.
        Parameters:
        url - the URL to read from
        charset - the charset used to decode the input stream; see Charsets for helpful predefined constants
        callback - the LineProcessor to use to handle the lines
        Returns:
        the output of processing the lines
        Throws:
        - if an I/O error occurs
      • readLines

        public static <>  url,
                                              charset)
                                      throws 
        Reads all of the lines from a URL. The lines do not include line-termination characters, but do include other leading and trailing whitespace.

        This method returns a mutable List. For an ImmutableList, use Resources.asCharSource(url, charset).readLines().

        Parameters:
        url - the URL to read from
        charset - the charset used to decode the input stream; see Charsets for helpful predefined constants
        Returns:
        a mutable containing all the lines
        Throws:
        - if an I/O error occurs
      • copy

        public static void  from,
                                 to)
                         throws 
        Copies all bytes from a URL to an output stream.
        Parameters:
        from - the URL to read from
        to - the output stream
        Throws:
        - if an I/O error occurs
      • getResource

        public static   resourceName)
        Returns a URL pointing to resourceName if the resource is found using the . In simple environments, the context class loader will find resources from the class path. In environments where different threads can have different class loaders, for example app servers, the context class loader will typically have been set to an appropriate loader for the current thread.

        In the unusual case where the context class loader is null, the class loader that loaded this class (Resources) will be used instead.

        Throws:
        - if the resource is not found
      • getResource

        public static  <?> contextClass,
                                       resourceName)
        Given a resourceName that is relative to contextClass, returns a URL pointing to the named resource.
        Throws:
        - if the resource is not found