@Beta public final class Resources extends Object
URL
parameters, they
are usually not appropriate for HTTP or other non-classpath resources.
All method parameters must be non-null unless documented otherwise.
Modifier and Type | Method and Description |
---|---|
static ByteSource |
asByteSource(URL url)
Returns a
ByteSource that reads from the given URL. |
static CharSource |
asCharSource(URL url,
Charset charset)
Returns a
CharSource that reads from the given URL using the given
character set. |
static void |
copy(URL from,
OutputStream to)
Copies all bytes from a URL to an output stream.
|
static URL |
getResource(Class<?> contextClass,
String resourceName)
Given a
resourceName that is relative to contextClass ,
returns a URL pointing to the named resource. |
static URL |
getResource(String resourceName)
|
static InputSupplier<InputStream> |
newInputStreamSupplier(URL url)
Deprecated.
Use
asByteSource(URL) instead. This method is
scheduled for removal in Guava 18.0. |
static InputSupplier<InputStreamReader> |
newReaderSupplier(URL url,
Charset charset)
Deprecated.
Use
asCharSource(URL, Charset) instead. This method
is scheduled for removal in Guava 18.0. |
static List<String> |
readLines(URL url,
Charset charset)
Reads all of the lines from a URL.
|
static <T> T |
readLines(URL url,
Charset 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[] |
toByteArray(URL url)
Reads all bytes from a URL into a byte array.
|
static String |
toString(URL url,
Charset charset)
Reads all characters from a URL into a
String , using the given
character set. |
@Deprecated public static InputSupplier<InputStream> newInputStreamSupplier(URL url)
asByteSource(URL)
instead. This method is
scheduled for removal in Guava 18.0.InputStream
that
read from the given URL.url
- the URL to read frompublic static ByteSource asByteSource(URL url)
ByteSource
that reads from the given URL.@Deprecated public static InputSupplier<InputStreamReader> newReaderSupplier(URL url, Charset charset)
asCharSource(URL, Charset)
instead. This method
is scheduled for removal in Guava 18.0.InputStreamReader
that read a URL using the given character set.url
- the URL to read fromcharset
- the charset used to decode the input stream; see Charsets
for helpful predefined constantspublic static CharSource asCharSource(URL url, Charset charset)
CharSource
that reads from the given URL using the given
character set.public static byte[] toByteArray(URL url) throws IOException
url
- the URL to read fromIOException
- if an I/O error occurspublic static String toString(URL url, Charset charset) throws IOException
String
, using the given
character set.url
- the URL to read fromcharset
- the charset used to decode the input stream; see Charsets
for helpful predefined constantsIOException
- if an I/O error occurs.public static <T> T readLines(URL url, Charset charset, LineProcessor<T> callback) throws IOException
url
- the URL to read fromcharset
- the charset used to decode the input stream; see Charsets
for helpful predefined constantscallback
- the LineProcessor to use to handle the linesIOException
- if an I/O error occurspublic static List<String> readLines(URL url, Charset charset) throws IOException
This method returns a mutable List
. For an
ImmutableList
, use
Resources.asCharSource(url, charset).readLines()
.
url
- the URL to read fromcharset
- the charset used to decode the input stream; see Charsets
for helpful predefined constantsList
containing all the linesIOException
- if an I/O error occurspublic static void copy(URL from, OutputStream to) throws IOException
from
- the URL to read fromto
- the output streamIOException
- if an I/O error occurspublic static URL getResource(String resourceName)
URL
pointing to resourceName
if the resource is
found using the context class
loader. 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.
IllegalArgumentException
- if the resource is not foundpublic static URL getResource(Class<?> contextClass, String resourceName)
resourceName
that is relative to contextClass
,
returns a URL
pointing to the named resource.IllegalArgumentException
- if the resource is not foundCopyright © 2010-2014. All Rights Reserved.