95 lines
3.2 KiB
Java
95 lines
3.2 KiB
Java
|
package com.google.common.io;
|
||
|
|
||
|
import com.google.common.base.MoreObjects;
|
||
|
import com.google.common.base.Preconditions;
|
||
|
import com.google.common.collect.Lists;
|
||
|
import java.io.IOException;
|
||
|
import java.io.InputStream;
|
||
|
import java.io.OutputStream;
|
||
|
import java.net.URL;
|
||
|
import java.nio.charset.Charset;
|
||
|
import java.util.List;
|
||
|
|
||
|
/* loaded from: classes2.dex */
|
||
|
public final class Resources {
|
||
|
private Resources() {
|
||
|
}
|
||
|
|
||
|
public static ByteSource asByteSource(URL url) {
|
||
|
return new UrlByteSource(url);
|
||
|
}
|
||
|
|
||
|
/* JADX INFO: Access modifiers changed from: package-private */
|
||
|
/* loaded from: classes2.dex */
|
||
|
public static final class UrlByteSource extends ByteSource {
|
||
|
private final URL url;
|
||
|
|
||
|
private UrlByteSource(URL url) {
|
||
|
this.url = (URL) Preconditions.checkNotNull(url);
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.io.ByteSource
|
||
|
public final InputStream openStream() throws IOException {
|
||
|
return this.url.openStream();
|
||
|
}
|
||
|
|
||
|
public final String toString() {
|
||
|
String valueOf = String.valueOf(this.url);
|
||
|
StringBuilder sb = new StringBuilder(String.valueOf(valueOf).length() + 24);
|
||
|
sb.append("Resources.asByteSource(");
|
||
|
sb.append(valueOf);
|
||
|
sb.append(")");
|
||
|
return sb.toString();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public static CharSource asCharSource(URL url, Charset charset) {
|
||
|
return asByteSource(url).asCharSource(charset);
|
||
|
}
|
||
|
|
||
|
public static byte[] toByteArray(URL url) throws IOException {
|
||
|
return asByteSource(url).read();
|
||
|
}
|
||
|
|
||
|
public static String toString(URL url, Charset charset) throws IOException {
|
||
|
return asCharSource(url, charset).read();
|
||
|
}
|
||
|
|
||
|
public static <T> T readLines(URL url, Charset charset, LineProcessor<T> lineProcessor) throws IOException {
|
||
|
return (T) asCharSource(url, charset).readLines(lineProcessor);
|
||
|
}
|
||
|
|
||
|
public static List<String> readLines(URL url, Charset charset) throws IOException {
|
||
|
return (List) readLines(url, charset, new LineProcessor<List<String>>() { // from class: com.google.common.io.Resources.1
|
||
|
final List<String> result = Lists.newArrayList();
|
||
|
|
||
|
@Override // com.google.common.io.LineProcessor
|
||
|
public boolean processLine(String str) {
|
||
|
this.result.add(str);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
@Override // com.google.common.io.LineProcessor
|
||
|
public List<String> getResult() {
|
||
|
return this.result;
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
public static void copy(URL url, OutputStream outputStream) throws IOException {
|
||
|
asByteSource(url).copyTo(outputStream);
|
||
|
}
|
||
|
|
||
|
public static URL getResource(String str) {
|
||
|
URL resource = ((ClassLoader) MoreObjects.firstNonNull(Thread.currentThread().getContextClassLoader(), Resources.class.getClassLoader())).getResource(str);
|
||
|
Preconditions.checkArgument(resource != null, "resource %s not found.", str);
|
||
|
return resource;
|
||
|
}
|
||
|
|
||
|
public static URL getResource(Class<?> cls, String str) {
|
||
|
URL resource = cls.getResource(str);
|
||
|
Preconditions.checkArgument(resource != null, "resource %s relative to %s not found.", str, cls.getName());
|
||
|
return resource;
|
||
|
}
|
||
|
}
|