Package com.google.common.io
Class Resources
- java.lang.Object
-
- com.google.common.io.Resources
-
@Beta public final class Resources extends java.lang.Object
Provides utility methods for working with resources in the classpath. Note that even though these methods useURLparameters, 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
-
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static ByteSourceasByteSource(java.net.URL url)Returns aByteSourcethat reads from the given URL.static CharSourceasCharSource(java.net.URL url, java.nio.charset.Charset charset)Returns aCharSourcethat reads from the given URL using the given character set.static voidcopy(java.net.URL from, java.io.OutputStream to)Copies all bytes from a URL to an output stream.static java.net.URLgetResource(java.lang.Class<?> contextClass, java.lang.String resourceName)Given aresourceNamethat is relative tocontextClass, returns aURLpointing to the named resource.static java.net.URLgetResource(java.lang.String resourceName)Returns aURLpointing toresourceNameif the resource is found using the context class loader.static InputSupplier<java.io.InputStream>newInputStreamSupplier(java.net.URL url)Deprecated.UseasByteSource(URL)instead.static InputSupplier<java.io.InputStreamReader>newReaderSupplier(java.net.URL url, java.nio.charset.Charset charset)Deprecated.UseasCharSource(URL, Charset)instead.static java.util.List<java.lang.String>readLines(java.net.URL url, java.nio.charset.Charset charset)Reads all of the lines from a URL.static <T> TreadLines(java.net.URL url, java.nio.charset.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(java.net.URL url)Reads all bytes from a URL into a byte array.static java.lang.StringtoString(java.net.URL url, java.nio.charset.Charset charset)Reads all characters from a URL into aString, using the given character set.
-
-
-
Method Detail
-
newInputStreamSupplier
@Deprecated public static InputSupplier<java.io.InputStream> newInputStreamSupplier(java.net.URL url)
Deprecated.UseasByteSource(URL)instead. This method is scheduled for removal in Guava 18.0.Returns a factory that will supply instances ofInputStreamthat read from the given URL.- Parameters:
url- the URL to read from- Returns:
- the factory
-
asByteSource
public static ByteSource asByteSource(java.net.URL url)
Returns aByteSourcethat reads from the given URL.- Since:
- 14.0
-
newReaderSupplier
@Deprecated public static InputSupplier<java.io.InputStreamReader> newReaderSupplier(java.net.URL url, java.nio.charset.Charset charset)
Deprecated.UseasCharSource(URL, Charset)instead. This method is scheduled for removal in Guava 18.0.Returns a factory that will supply instances ofInputStreamReaderthat read a URL using the given character set.- Parameters:
url- the URL to read fromcharset- the charset used to decode the input stream; seeCharsetsfor helpful predefined constants- Returns:
- the factory
-
asCharSource
public static CharSource asCharSource(java.net.URL url, java.nio.charset.Charset charset)
Returns aCharSourcethat reads from the given URL using the given character set.- Since:
- 14.0
-
toByteArray
public static byte[] toByteArray(java.net.URL url) throws java.io.IOExceptionReads 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:
java.io.IOException- if an I/O error occurs
-
toString
public static java.lang.String toString(java.net.URL url, java.nio.charset.Charset charset) throws java.io.IOExceptionReads all characters from a URL into aString, using the given character set.- Parameters:
url- the URL to read fromcharset- the charset used to decode the input stream; seeCharsetsfor helpful predefined constants- Returns:
- a string containing all the characters from the URL
- Throws:
java.io.IOException- if an I/O error occurs.
-
readLines
public static <T> T readLines(java.net.URL url, java.nio.charset.Charset charset, LineProcessor<T> callback) throws java.io.IOExceptionStreams lines from a URL, stopping when our callback returns false, or we have read all of the lines.- Parameters:
url- the URL to read fromcharset- the charset used to decode the input stream; seeCharsetsfor helpful predefined constantscallback- the LineProcessor to use to handle the lines- Returns:
- the output of processing the lines
- Throws:
java.io.IOException- if an I/O error occurs
-
readLines
public static java.util.List<java.lang.String> readLines(java.net.URL url, java.nio.charset.Charset charset) throws java.io.IOExceptionReads 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 anImmutableList, useResources.asCharSource(url, charset).readLines().- Parameters:
url- the URL to read fromcharset- the charset used to decode the input stream; seeCharsetsfor helpful predefined constants- Returns:
- a mutable
Listcontaining all the lines - Throws:
java.io.IOException- if an I/O error occurs
-
copy
public static void copy(java.net.URL from, java.io.OutputStream to) throws java.io.IOExceptionCopies all bytes from a URL to an output stream.- Parameters:
from- the URL to read fromto- the output stream- Throws:
java.io.IOException- if an I/O error occurs
-
getResource
public static java.net.URL getResource(java.lang.String resourceName)
Returns aURLpointing toresourceNameif 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.- Throws:
java.lang.IllegalArgumentException- if the resource is not found
-
getResource
public static java.net.URL getResource(java.lang.Class<?> contextClass, java.lang.String resourceName)Given aresourceNamethat is relative tocontextClass, returns aURLpointing to the named resource.- Throws:
java.lang.IllegalArgumentException- if the resource is not found
-
-