Top Description Inners Constructors Methods
com.google.common.io

public final Class Resources

extends Object
Class Inheritance
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull

Provides utility methods for working with resources in the classpath. Note that even though these methods use URL parameters, they are usually not appropriate for HTTP or other non-classpath resources.
Authors
Chris Nokleberg, Ben Yu, Colin Decker
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Resources.UrlByteSource

A byte source that reads from a URL using URL#openStream().

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static ByteSource
asByteSource(URL url)

Returns a ByteSource that reads from the given URL.

public static CharSource
asCharSource(URL url, Charset charset)

Returns a CharSource that reads from the given URL using the given character set.

public static void
copy(URL
the URL to read from
from
,
OutputStream
the output stream
to
)

Copies all bytes from a URL to an output stream.

public static URL
getResource(String resourceName)

Returns a URL pointing to resourceName if the resource is found using the context class loader.

public static URL
getResource(Class<?> contextClass, String resourceName)

Given a resourceName that is relative to contextClass, returns a URL pointing to the named resource.

public static <T extends @Nullable Object> T

Returns:

the output of processing the lines
readLines
(URL
the URL to read from
url
,
Charset
the charset used to decode the input stream; see Charsets for helpful predefined constants
charset
,
LineProcessor<T>
the LineProcessor to use to handle the lines
callback
)

Streams lines from a URL, stopping when our callback returns false, or we have read all of the lines.

public static List<String>

Returns:

a mutable List containing all the lines
readLines
(URL
the URL to read from
url
,
Charset
the charset used to decode the input stream; see Charsets for helpful predefined constants
charset
)

Reads all of the lines from a URL.

public static byte[]

Returns:

a byte array containing all the bytes from the URL
toByteArray
(URL
the URL to read from
url
)

Reads all bytes from a URL into a byte array.

public static String

Returns:

a string containing all the characters from the URL
toString
(URL
the URL to read from
url
,
Charset
the charset used to decode the input stream; see Charsets for helpful predefined constants
charset
)

Reads all characters from a URL into a String, using the given character set.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Resourcesback to summary
private Resources()

Method Detail

asByteSourceback to summary
public static ByteSource asByteSource(URL url)

Returns a ByteSource that reads from the given URL.

Since
14.0
asCharSourceback to summary
public static CharSource asCharSource(URL url, Charset charset)

Returns a CharSource that reads from the given URL using the given character set.

Since
14.0
copyback to summary
public static void copy(URL from, OutputStream to) throws IOException

Copies all bytes from a URL to an output stream.

Parameters
from:URL

the URL to read from

to:OutputStream

the output stream

Exceptions
IOException:
if an I/O error occurs
getResourceback to summary
public static URL getResource(String resourceName)

Returns a 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.

Annotations
@CanIgnoreReturnValue
Exceptions
IllegalArgumentException:
if the resource is not found
getResourceback to summary
public static URL getResource(Class<?> contextClass, String resourceName)

Given a resourceName that is relative to contextClass, returns a URL pointing to the named resource.

Annotations
@CanIgnoreReturnValue
Exceptions
IllegalArgumentException:
if the resource is not found
readLinesback to summary
public static <T extends @Nullable Object> T readLines(URL url, Charset charset, LineProcessor<T> callback) throws IOException

Streams lines from a URL, stopping when our callback returns false, or we have read all of the lines.

Parameters
url:URL

the URL to read from

charset:Charset

the charset used to decode the input stream; see Charsets for helpful predefined constants

callback:LineProcessor<T>

the LineProcessor to use to handle the lines

Returns:T

the output of processing the lines

Annotations
@CanIgnoreReturnValue
@ParametricNullness
Exceptions
IOException:
if an I/O error occurs
readLinesback to summary
public static List<String> readLines(URL url, Charset charset) throws IOException

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:URL

the URL to read from

charset:Charset

the charset used to decode the input stream; see Charsets for helpful predefined constants

Returns:List<String>

a mutable List containing all the lines

Exceptions
IOException:
if an I/O error occurs
toByteArrayback to summary
public static byte[] toByteArray(URL url) throws IOException

Reads all bytes from a URL into a byte array.

Parameters
url:URL

the URL to read from

Returns:byte[]

a byte array containing all the bytes from the URL

Exceptions
IOException:
if an I/O error occurs
toStringback to summary
public static String toString(URL url, Charset charset) throws IOException

Reads all characters from a URL into a String, using the given character set.

Parameters
url:URL

the URL to read from

charset:Charset

the charset used to decode the input stream; see Charsets for helpful predefined constants

Returns:String

a string containing all the characters from the URL

Exceptions
IOException:
if an I/O error occurs.
com.google.common.io back to summary

private final Class Resources.UrlByteSource

extends ByteSource
Class Inheritance

A byte source that reads from a URL using URL#openStream().

Field Summary

Modifier and TypeField and Description
private final URL

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public InputStream
openStream()

Implements abstract com.google.common.io.ByteSource.openStream.

Opens a new InputStream for reading from this source.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from com.google.common.io.ByteSource:
asCharSourceconcatconcatconcatcontentEqualscopyTocopyToemptyhashisEmptyopenBufferedStreamreadreadsizesizeIfKnownslicewrap

Field Detail

urlback to summary
private final URL url

Constructor Detail

UrlByteSourceback to summary
private UrlByteSource(URL url)

Method Detail

openStreamback to summary
public InputStream openStream() throws IOException

Implements abstract com.google.common.io.ByteSource.openStream.

Doc from com.google.common.io.ByteSource.openStream.

Opens a new InputStream for reading from this source. This method returns a new, independent stream each time it is called.

The caller is responsible for ensuring that the returned stream is closed.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs while opening the stream
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override