MCPcopy Create free account
hub / github.com/antlr/codebuff / Resources

Class Resources

corpus/java/training/guava/io/Resources.java:46–214  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected