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. All method parameters must be non-null unless documented otherwise. @author Chris Nok
| 44 | |
| 45 | |
| 46 | @Beta |
| 47 | @GwtIncompatible |
| 48 | public final class Resources { |
| 49 | private Resources() {} |
| 50 | |
| 51 | /** |
| 52 | * Returns a {@link ByteSource} that reads from the given URL. |
| 53 | * |
| 54 | * @since 14.0 |
| 55 | */ |
| 56 | |
| 57 | |
| 58 | public static ByteSource asByteSource(URL url) { |
| 59 | return new UrlByteSource(url); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * A byte source that reads from a URL using {@link URL#openStream()}. |
| 64 | */ |
| 65 | |
| 66 | private static final class UrlByteSource extends ByteSource { |
| 67 | private final URL url; |
| 68 | private UrlByteSource(URL url) { |
| 69 | this.url = checkNotNull(url); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public InputStream openStream() throws IOException { |
| 74 | return url.openStream(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public String toString() { |
| 79 | return "Resources.asByteSource(" + url + ")"; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns a {@link CharSource} that reads from the given URL using the given character set. |
| 85 | * |
| 86 | * @since 14.0 |
| 87 | */ |
| 88 | |
| 89 | |
| 90 | public static CharSource asCharSource(URL url, Charset charset) { |
| 91 | return asByteSource(url).asCharSource(charset); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Reads all bytes from a URL into a byte array. |
| 96 | * |
| 97 | * @param url the URL to read from |
| 98 | * @return a byte array containing all the bytes from the URL |
| 99 | * @throws IOException if an I/O error occurs |
| 100 | */ |
| 101 | |
| 102 | |
| 103 | public static byte[] toByteArray(URL url) throws IOException { |
nothing calls this directly
no outgoing calls
no test coverage detected